def test_build_endpoint() -> None:
    sample_data = [
        {'url': 'https://test.plentymarkets-cloud01.com',
         'route': '/rest/orders'},
        {'url': 'https://test.plentymarkets-cloud01.com',
         'route': '/rest/orders'},
        {'url': 'invalid.com',
         'route': '/rest/orders'},
        {'url': 'https://test.plentymarkets-cloud01.com',
         'route': '/rest/invalid'},
        {'url': '',
         'route': '/rest/orders'},
        {'url': 'https://test.plentymarkets-cloud01.com',
         'route': ''}
    ]

    expected = ['https://test.plentymarkets-cloud01.com/rest/orders',
                'https://test.plentymarkets-cloud01.com/rest/orders', '', '',
                '', '']
    result = []

    for sample in sample_data:
        result.append(build_endpoint(url=sample['url'],
                                     route=sample['route']))

    assert expected == result
示例#2
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 __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