Ejemplo n.º 1
0
    def __plenty_api_request(self,
                             method: str,
                             domain: str,
                             query: dict = None,
                             data: dict = None,
                             path: str = '') -> dict:
        """
            Make a request to the PlentyMarkets API.

            Parameter:
                method      [str]   -   GET/POST
                domain      [str]   -   Orders/Items...
            (Optional)
                query       [dict]  -   Additional options for the request
                data        [dict]  -   Data body for post requests
        """
        route = ''
        endpoint = ''
        raw_response = {}
        response = {}

        route = utils.get_route(domain=domain)
        endpoint = utils.build_endpoint(url=self.url, route=route, path=path)
        if self.debug:
            print(f"DEBUG: Endpoint: {endpoint}")
            print(f"DEBUG: Params: {query}")
        while True:
            if method.lower() == 'get':
                raw_response = requests.get(endpoint,
                                            headers=self.creds,
                                            params=query)

            if method.lower() == 'post':
                raw_response = requests.post(endpoint,
                                             headers=self.creds,
                                             params=query,
                                             json=data)
            if raw_response.status_code != 429:
                break
            print("API:Request throttled, limit for subscription reached")
            time.sleep(3)

        if self.debug:
            print(f"DEBUG: request url: {raw_response.request.url}")
        try:
            response = raw_response.json()
        except simplejson.errors.JSONDecodeError:
            print(f"ERROR: No response for request {method} at {endpoint}")
            return None

        if domain == 'referrer':
            # The referrer request responds with a different format
            return response

        if 'error' in response.keys():
            print(f"ERROR: Request failed:\n{response['error']['message']}")
            return None

        return response
def test_get_route() -> None:
    sample_data = ['order', 'item', 'ITEMS', 'oRdErS', 'wrong', '']
    result = []
    expected = [
        '/rest/orders', '/rest/items', '/rest/items', '/rest/orders', '', ''
    ]

    for domain in sample_data:
        result.append(get_route(domain=domain))

    assert expected == result
Ejemplo n.º 3
0
    def __plenty_api_request(self,
                             method: str,
                             domain: str,
                             query: dict = None,
                             data: dict = None,
                             path: str = '') -> dict:
        """
            Make a request to the PlentyMarkets API.

            Parameter:
                method [str]        -   GET/POST
                domain [str]        -   Orders/Items...
            (Optional)
                query [dict]         -   Additional options for the request
                data  [dict]        -   Data body for post requests
        """
        route = ''
        endpoint = ''
        raw_response = {}
        response = {}

        route = utils.get_route(domain=domain)
        endpoint = utils.build_endpoint(url=self.url, route=route, path=path)
        if self.debug:
            print(f"DEBUG: Endpoint: {endpoint}")
            print(f"DEBUG: Params: {query}")
        if method.lower() == 'get':
            raw_response = requests.get(endpoint, headers=self.creds,
                                        params=query)
        if method.lower() == 'post':
            raw_response = requests.post(endpoint, headers=self.creds,
                                         params=query, data=data)

        try:
            response = raw_response.json()
        except simplejson.errors.JSONDecodeError:
            print(f"ERROR: No response for request {method} at {endpoint}")
            return None

        return response