# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/05_core.rq_inventory_upload.ipynb (unless otherwise specified).

__all__ = ['add_new_listings_using_csv', 'update_existing_listings_using_csv', 'delete_listings_using_csv',
           'get_list_of_recent_uploads', 'get_upload']

# Cell
import requests
from typing import Union
import io
from . import *

# Cell


def add_new_listings_using_csv(user: UserWithUserTokenBasedAuthentication,
                               file: io.BufferedReader) -> requests.models.Response:
    """
    Upload a csv file of listings to add to your inventory.

    User Authentication needed.
    """

    url = f"{INVENTORY_UPLOAD_URL}/add"
    params = user.params
    files = {"upload": file}

    return requests.post(url, params=params, files=files)

# Cell


def update_existing_listings_using_csv(user: UserWithUserTokenBasedAuthentication,
                                       file: io.BufferedReader) -> requests.models.Response:
    """
    Upload a CSV file with updated information for existing listings.

    User Authentication needed.
    """

    url = f"{INVENTORY_UPLOAD_URL}/change"
    params = user.params
    files = {"upload": file}

    return requests.post(url, params=params, files=files)

# Cell


def delete_listings_using_csv(user: UserWithUserTokenBasedAuthentication,
                              file: io.BufferedReader) -> requests.models.Response:
    """
    Upload a CSV file with listings to delete from your inventory.

    User Authentication needed.
    """

    url = f"{INVENTORY_UPLOAD_URL}/delete"
    params = user.params
    files = {"upload": file}

    return requests.post(url, params=params, files=files)

# Cell


def get_list_of_recent_uploads(user: UserWithUserTokenBasedAuthentication,
                               page: Union[int, None] = None,
                               per_page: Union[int, None] = None) -> requests.models.Response:
    """
    Get a list of all recent inventory uploads.

    User Authentication needed.
    """

    url = f"{INVENTORY_UPLOAD_URL}"
    headers = user.headers
    params = user.params

    if page:
        params["page"] = max(1, page)
    if per_page:
        params["per_page"] = max(1, per_page)

    return requests.get(url, headers=headers, params=params)

# Cell


def get_upload(user: UserWithUserTokenBasedAuthentication,
               upload_id: int) -> requests.models.Response:
    """
    Get details about the status of an inventory upload.

    User Authentication needed.
    """

    url = f"{INVENTORY_UPLOAD_URL}/{upload_id}"
    headers = user.headers
    params = user.params

    return requests.get(url, headers=headers, params=params)