Beispiel #1
0
    def send_api_request(self, method, url, params={}, valid_parameters=[], needs_api_key=False):
        """
        Sends the url with parameters to the requested url, validating them
        to make sure that they are what we expect to have passed to us

        :param method: a string, the request method you want to make
        :param params: a dict, the parameters used for the API request
        :param valid_parameters: a list, the list of valid parameters
        :param needs_api_key: a boolean, whether or not your request needs an api key injected

        :returns: a dict parsed from the JSON response
        """
        if needs_api_key:
            params.update({"api_key": self.request.consumer.key})
            valid_parameters.append("api_key")

        files = []
        if "data" in params:
            if isinstance(params["data"], list):
                files = [
                    ("data[" + str(idx) + "]", data, open(data, "rb").read()) for idx, data in enumerate(params["data"])
                ]
            else:
                files = [("data", params["data"], open(params["data"], "rb").read())]
            del params["data"]

        validate_params(valid_parameters, params)
        if method == "get":
            return self.request.get(url, params)
        else:
            return self.request.post(url, params, files)
Beispiel #2
0
    def send_api_request(self, method, url, params={}, valid_parameters=[], needs_api_key=False):
        """
        Sends the url with parameters to the requested url, validating them
        to make sure that they are what we expect to have passed to us

        :param method: a string, the request method you want to make
        :param params: a dict, the parameters used for the API request
        :param valid_parameters: a list, the list of valid parameters
        :param needs_api_key: a boolean, whether or not your request needs an api key injected

        :returns: a dict parsed from the JSON response
        """
        if needs_api_key:
            params.update({'api_key': self.request.consumer.key})
            valid_parameters.append('api_key')

        files = []
        if 'data' in params:
            if isinstance(params['data'], list):
                for idx, data in enumerate(params['data']):
                    with open(data, 'rb') as f:
                        files.append(('data['+str(idx)+']', data, f.read()))
            else:
                with open(params['data'], 'rb') as f:
                    files = [('data', params['data'], f.read())]
            del params['data']

        validate_params(valid_parameters, params)
        if method == "get":
            return self.request.get(url, params)
        else:
            return self.request.post(url, params, files)
Beispiel #3
0
    def send_api_request(self, method, url, params=None, valid_parameters=None, needs_api_key=False):
        """
        Sends the url with parameters to the requested url, validating them
        to make sure that they are what we expect to have passed to us

        :param method: a string, the request method you want to make
        :param params: a dict, the parameters used for the API request
        :param valid_parameters: a list, the list of valid parameters
        :param needs_api_key: a boolean, whether or not your request needs an api key injected

        :returns: a dict parsed from the JSON response
        """
        params = {} if params is None else params
        valid_parameters = [] if valid_parameters is None else valid_parameters
        if needs_api_key:
            params.update({'api_key': self.request.consumer.key})
            valid_parameters.append('api_key')

        files = []
        if 'data' in params:
            if isinstance(params['data'], list):
                for idx, data in enumerate(params['data']):
                    with open(data, 'rb') as f:
                        files.append(('data['+str(idx)+']', data, f.read()))
            else:
                with open(params['data'], 'rb') as f:
                    files = [('data', params['data'], f.read())]
            del params['data']

        validate_params(valid_parameters, params)
        if method == "get":
            return self.request.get(url, params)
        else:
            return self.request.post(url, params, files)
Beispiel #4
0
 def __call__(self, product_id, inventory_info=False, **kwargs):
     if inventory_info:
         required_params = ['color']
         optional_params = ['state']
         validate_params(required_params, optional_params, kwargs)
         return self.get(self.endpoint + str(product_id), kwargs)
     else:
         return self.get(self.endpoint + str(product_id))
Beispiel #5
0
 def create(self, **kwargs):
     required_params = ['test', 'price', 'garment', 'print', 'addresses']
     optional_params = ['print_type', 'personalization', 'address_count', 'extra_screens', 'ship_type']
     validate_params(required_params, optional_params, kwargs)
     return self.post(self.endpoint, kwargs)
Beispiel #6
0
 def __call__(self, params):
     required_params = ['garment', 'print']
     optional_params = ['print_type', 'personalization', 'address_count', 'extra_screens', 'ship_type',
                        'international_garments']
     validate_params(required_params, optional_params, params)
     return self.get(self.endpoint, params)