Beispiel #1
0
def request_post(url,payload=None,timeout=16):
    """For a given url and payload, makes a post request and returns the response.

    :param url: The url to send a post request to.
    :type url: str
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :param timeout: The time for the post to wait for a response. Should be slightly greater than multiples of 3.
    :type timeout: Optional[int]
    :returns: Returns the data from the post request.

    """
    try:
        res = Session.post(url, data=payload, timeout=timeout)
        data = res.json()
        if 'challenge' in data:
            return data
        res.raise_for_status()
        if 'mfa_required' in data:
            mfa_token = input("Please Type In The MFA Code: ")
            payload['mfa_code'] = mfa_token
            res = Session.post(url, data=payload, timeout=timeout)
            while (res.status_code != 200):
                mfa_token = input("That MFA code was not correct. Please Type In Another MFA Code: ")
                payload['mfa_code'] = mfa_token
                res = Session.post(url, data=payload, timeout=timeout)
            data = res.json()
    except (requests.exceptions.HTTPError,AttributeError) as message:
        data = None
        print(message)

    return data
Beispiel #2
0
def request_post(url,payload=None,timeout=16):
    """For a given url and payload, makes a post request and returns the response.

    :param url: The url to send a post request to.
    :type url: str
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :param timeout: The time for the post to wait for a response. Should be slightly greater than multiples of 3.
    :type timeout: Optional[int]
    :returns: Returns the data from the post request.

    """
    try:
        res = Session.post(url, data=payload, timeout=timeout)
        res.raise_for_status()
        data = res.json()
    except (requests.exceptions.HTTPError,AttributeError) as message:
        try:
            print("HTTPError: {}".format(data))
        except:
            print("HTTPError: [fail to print resp.json()]")
        data = None
        print(message)

    return data
Beispiel #3
0
def request_post(url, payload=None, timeout=16, json=False):
    """For a given url and payload, makes a post request and returns the response.

    :param url: The url to send a post request to.
    :type url: str
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :param timeout: The time for the post to wait for a response. Should be slightly greater than multiples of 3.
    :type timeout: Optional[int]
    :param json: This is used for buying and selling options.
    :type json: bool
    :returns: Returns the data from the post request.

    """
    try:
        if json:
            update_session('Content-Type', 'application/json')
            res = Session.post(url, json=payload, timeout=timeout)
            update_session('Content-Type',
                           'application/x-www-form-urlencoded; charset=utf-8')
        else:
            res = Session.post(url, data=payload, timeout=timeout)
        data = res.json()
        if 'challenge' in data:
            return data
        res.raise_for_status()
        if 'mfa_required' in data:
            mfa_token = input("Please Type In The MFA Code: ")
            payload['mfa_code'] = mfa_token
            res = Session.post(url, data=payload, timeout=timeout)
            while (res.status_code != 200):
                mfa_token = input(
                    "That MFA code was not correct. Please Type In Another MFA Code: "
                )
                payload['mfa_code'] = mfa_token
                res = Session.post(url, data=payload, timeout=timeout)
            data = res.json()
    except (requests.exceptions.HTTPError, AttributeError) as message:
        data = None
        print(message)

    return data
Beispiel #4
0
def request_delete(url):
    """For a given url and payload, makes a delete request and returns the response.

    :param url: The url to send a delete request to.
    :type url: str
    :returns: Returns the data from the delete request.

    """
    try:
        res = Session.delete(url)
        res.raise_for_status()
    except:
        data = None
        print("Cound not process delete request.")

    return data
Beispiel #5
0
def request_document(url,payload=None):
    """Using a document url, makes a get request and returnes the session data.

    :param url: The url to send a get request to.
    :type url: str
    :returns: Returns the session.get() data as opppose to session.get().json() data.

    """
    try:
        res = Session.get(url,params=payload)
        res.raise_for_status()
    except requests.exceptions.HTTPError as message:
        print(message)
        return None

    return res
Beispiel #6
0
def request_get(url,dataType='regular',payload=None):
    """For a given url and payload, makes a get request and returns the data.

    :param url: The url to send a get request to.
    :type url: str
    :param dataType: Determines how far to drill down into the data. 'regular' returns the unfiltered data. \
    'results' will return data['results']. 'pagination' will return data['results'] and append it with any \
    data that is in data['next']. 'indexzero' will return data['results'][0].
    :type dataType: Optional[str]
    :param payload: Dictionary of parameters to pass to the url as url/?key1=value1&key2=value2.
    :type payload: Optional[dict]
    :returns: Returns the data from the get request.

    """
    try:
        res = Session.get(url,params=payload)
        res.raise_for_status()
        data = res.json()
    except (requests.exceptions.HTTPError,AttributeError) as message:
        print(message)
        if (dataType == 'results' or dataType == 'pagination'):
            return [None]
        else:
            return None

    if (dataType == 'results'):
        try:
            data = data['results']
        except KeyError as message:
            print("{} is not a key in the dictionary".format(message))
            return [None]
    elif (dataType == 'pagination'):
        counter = 2
        nextData = data
        try:
            data = data['results']
        except KeyError as message:
            print("{} is not a key in the dictionary".format(message))
            return [None]

        if nextData['next']:
            print('Found Additional pages.')
        while nextData['next']:
            try:
                res = Session.get(nextData['next'])
                res.raise_for_status()
                nextData = res.json()
            except:
                print('Additional pages exist but could not be loaded.')
                return(data)
            print('Loading page '+str(counter)+' ...')
            counter += 1
            for item in nextData['results']:
                data.append(item)
    elif (dataType == 'indexzero'):
        try:
            data = data['results'][0]
        except KeyError as message:
            print("{} is not a key in the dictionary".format(message))
            return None
        except IndexError as message:
            return None

    return data