Beispiel #1
0
    def get(self, api, params, doseq=0):
        if not (api in self.api_info):
            raise Exception("no such api")
        api_info = self.api_info[api]

        r = self.prepare_request(api_info, params, doseq)

        SignerV4.sign(r, self.service_info.credentials)

        url = r.build(doseq)
        resp = self.session.get(url, headers=r.headers,
                                timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
        if resp.status_code == 200:
            return resp.text
        else:
            raise Exception(resp.text)
Beispiel #2
0
    def json(self, api, params, body):
        if not (api in self.api_info):
            raise Exception("no such api")
        api_info = self.api_info[api]
        r = self.prepare_request(api_info, params)
        r.headers['Content-Type'] = 'application/json'
        r.body = body

        SignerV4.sign(r, self.service_info.credentials)

        url = r.build()
        resp = self.session.post(url, headers=r.headers, data=r.body,
                                 timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
        if resp.status_code == 200:
            return json.dumps(resp.json())
        else:
            raise Exception(resp.text)
Beispiel #3
0
    def post(self, api, params, form):
        if not (api in self.api_info):
            raise Exception("no such api")
        api_info = self.api_info[api]
        r = self.prepare_request(api_info, params)
        r.headers['Content-Type'] = 'application/x-www-form-urlencoded'
        r.form = self.merge(api_info.form, form)
        r.body = urlencode(r.form, True)
        SignerV4.sign(r, self.service_info.credentials)

        url = r.build()

        resp = self.session.post(url, headers=r.headers, data=r.form,
                                 timeout=(self.service_info.connection_timeout, self.service_info.socket_timeout))
        if resp.status_code == 200:
            return resp.text
        else:
            raise Exception(resp.text)
    async def async_get(self, api, params):
        if not (api in self.api_info):
            raise Exception("no such api")
        api_info = self.api_info[api]
        r = self.prepare_request(api_info, params)
        timeout = aiohttp.ClientTimeout(
            connect=self.service_info.connection_timeout,
            sock_connect=self.service_info.socket_timeout)

        SignerV4.sign(r, self.service_info.credentials)

        url = r.build()
        async with aiohttp.request("GET",
                                   url,
                                   headers=r.headers,
                                   timeout=timeout) as r:
            resp = await r.text(encoding="utf-8")
            if r.status == 200:
                return resp
            else:
                raise Exception(resp)
Beispiel #5
0
    def get_sign_url(self, api, params):
        if not (api in self.api_info):
            raise Exception("no such api")
        api_info = self.api_info[api]

        mquery = self.merge(api_info.query, params)
        r = Request()
        r.set_shema(self.service_info.scheme)
        r.set_method(api_info.method)
        r.set_path(api_info.path)
        r.set_query(mquery)

        return SignerV4.sign_url(r, self.service_info.credentials)
    async def async_json(self, api, params, body):
        if not (api in self.api_info):
            raise Exception("no such api")
        api_info = self.api_info[api]
        r = self.prepare_request(api_info, params)
        r.headers['Content-Type'] = 'application/json'
        r.body = body
        timeout = aiohttp.ClientTimeout(
            connect=self.service_info.connection_timeout,
            sock_connect=self.service_info.socket_timeout)

        SignerV4.sign(r, self.service_info.credentials)

        url = r.build()
        async with aiohttp.request("POST",
                                   url,
                                   headers=r.headers,
                                   data=r.body,
                                   timeout=timeout) as r:
            resp = await r.text(encoding="utf-8")
            if r.status == 200:
                return resp
            else:
                raise Exception(resp)
Beispiel #7
0
# coding: utf-8
import datetime

from volcengine.auth.SignerV4 import SignerV4
from volcengine.auth.SignParam import SignParam
from volcengine.Credentials import Credentials
from collections import OrderedDict

if __name__ == '__main__':
    sign = SignerV4()

    param = SignParam()
    param.path = '/'
    param.method = 'GET'
    param.host = 'open.volcengineapi.com'
    param.body = ''
    param.date = datetime.datetime.utcfromtimestamp(1640712206)
    query = OrderedDict()
    query['Action'] = 'ListUsers'
    query['Version'] = '2018-01-01'
    query['Limit'] = '5'
    query['Offset'] = '0'

    param.query = query
    header = OrderedDict()
    header['Host'] = 'open.volcengineapi.com'
    param.header_list = header

    cren = Credentials('ak', 'sk', 'iam', 'cn-north-1')
    result = sign.sign_only(param, cren)