Example #1
0
    async def save(self, idempotency_key=None):
        updated_params = self.serialize(None)
        headers = populate_headers(idempotency_key)

        if updated_params:
            self.refresh_from(await self.request("post", self.instance_url(), updated_params, headers))
        else:
            logger.debug("Trying to save already saved object %r", self)

        return self
Example #2
0
    async def save(self, idempotency_key=None):
        updated_params = self.serialize(None)
        headers = populate_headers(idempotency_key)

        if updated_params:
            self.refresh_from(await self.request('post', self.instance_url(),
                                                 updated_params, headers))
        else:
            logger.debug('Trying to save already saved object %r', self)

        return self
Example #3
0
    async def request_raw(self, method, url, params=None, supplied_headers=None):
        """
        Mechanism for issuing an API call
        """
        from aiostripe import api_version

        if self.api_key:
            my_api_key = self.api_key
        else:
            from aiostripe import api_key
            my_api_key = api_key

        if my_api_key is None:
            raise error.AuthenticationError('No API key provided. (HINT: set your API key using '
                                            '"aiostripe.api_key = <API-KEY>"). You can generate API keys from the Stripe '
                                            'web interface.  See https://stripe.com/api for details, or email '
                                            '[email protected] if you have any questions.')

        abs_url = '%s%s' % (self.api_base, url)

        encoded_params = urllib.parse.urlencode(list(_api_encode(params or {})))

        if method == 'get' or method == 'delete':
            if params:
                abs_url = _build_api_url(abs_url, encoded_params)

            post_data = None
        elif method == 'post':
            if supplied_headers is not None and supplied_headers.get('Content-Type') == 'multipart/form-data':
                generator = MultipartDataGenerator()
                generator.add_params(params or {})
                post_data = generator.get_post_data()
                supplied_headers['Content-Type'] = 'multipart/form-data; boundary=%s' % generator.boundary
            else:
                post_data = encoded_params
        else:
            raise error.APIConnectionError('Unrecognized HTTP method %r.  This may indicate a bug in the Stripe '
                                           'bindings.  Please contact [email protected] for assistance.' % method)

        ua = {
            'bindings_version': version.VERSION,
            'lang': 'python',
            'publisher': 'stripe',
            'httplib': self._client.name,
        }
        for attr, func in [['lang_version', platform.python_version],
                           ['platform', platform.platform],
                           ['uname', lambda: ' '.join(platform.uname())]]:
            try:
                val = func()
            except Exception as e:
                val = '!! %s' % e

            ua[attr] = val

        headers = {
            'X-Stripe-Client-User-Agent': json.dumps(ua),
            'User-Agent': 'Stripe/v1 PythonBindings/%s' % version.VERSION,
            'Authorization': 'Bearer %s' % my_api_key,
        }

        if self.stripe_account:
            headers['Stripe-Account'] = self.stripe_account

        if method == 'post':
            headers['Content-Type'] = 'application/x-www-form-urlencoded'

        if api_version is not None:
            headers['Stripe-Version'] = api_version

        if supplied_headers is not None:
            for key, value in supplied_headers.items():
                headers[key] = value

        rbody, rcode, rheaders = await self._client.request(method, abs_url, headers, post_data)

        logger.info('%s %s %d', method.upper(), abs_url, rcode)
        logger.debug('API request to %s returned (response code, response body) of (%d, %r)', abs_url, rcode, rbody)

        return rbody, rcode, rheaders, my_api_key