コード例 #1
0
    def __init__(self, api_key=None, api_base_url=None):
        self.api_key = api_key
        self.api_base_url = api_base_url

        # we create ONE instance of an http handler
        self.authorization = None
        self.httpHandler = new_default_http_client()
コード例 #2
0
    def __init__(self, api_key=None, api_secret=None, host=None, client=None):
        "Provide API Key and secret, or use values from Environment"

        self.api_key = api_key or os.environ.get('CK_API_KEY', None)
        self.api_secret = api_secret or os.environ.get('CK_API_SECRET', None)
        self.host = host or 'https://api.coinkite.com'

        self.client = client or new_default_http_client(verify_ssl_certs=True)
コード例 #3
0
ファイル: swagger.py プロジェクト: phdkiran/taxamo-python
class ApiClient:
    """Generic API client for Swagger client library builds"""

    client = http_client.new_default_http_client()

    def __init__(self, apiKey=None, apiServer=None):
        if apiKey == None:
            raise Exception('You must pass an apiKey when instantiating the '
                            'APIClient')
        self.apiKey = apiKey
        self.apiServer = apiServer
        # self.cookie = None

    def callAPI(self,
                resourcePath,
                method,
                queryParams,
                postData,
                headerParams=None):

        url = self.apiServer + resourcePath
        headers = {}
        if headerParams:
            for param, value in headerParams.iteritems():
                headers[param] = value

        #headers['Content-type'] = 'application/json'
        headers['Token'] = self.apiKey

        # if self.cookie:
        #     headers['Cookie'] = self.cookie

        data = None

        if queryParams:
            # Need to remove None values, these should not be sent
            sentQueryParams = {}
            for param, value in queryParams.items():
                if value != None:
                    sentQueryParams[param] = value
            url = url + '?' + urllib.urlencode(sentQueryParams)

        if method in ['GET']:

            #Options to add statements later on and for compatibility
            pass

        elif method in ['POST', 'PUT', 'DELETE']:

            if postData:
                headers['Content-type'] = 'application/json'
                data = self.sanitizeForSerialization(postData)
                data = json.dumps(data)

        else:
            raise Exception('Method ' + method + ' is not recognized.')

        # request = MethodRequest(method=method, url=url, headers=headers,
        #                         data=data)

        # Make the request
        try:
            resp_body, resp_code = self.client.request(method=method,
                                                       url=url,
                                                       headers=headers,
                                                       post_data=data)
        except urllib2.HTTPError, err:
            if err.code == 400:
                raise Exception(err.read())
            else:
                raise err

        # if 'Set-Cookie' in response.headers:
        #     self.cookie = response.headers['Set-Cookie']
        # string = response.read()

        try:
            if hasattr(resp_body, 'decode'):
                resp_body = resp_body.decode('utf-8')
            data = json.loads(resp_body)
        except ValueError:  # PUT requests don't return anything
            data = None

        return data