Exemplo n.º 1
0
    def call_api(self, action, params=None, **kwargs):
        """
        调用API的通用方法,有关SSL证书验证问题请参阅

        http://www.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

        :param action: Method Name,
        :param params: Dictionary,form params for api.
        :param timeout: (optional) Float describing the timeout of the request.
        :return:
        """
        if params is None:
            params = {}

        # 使用 urlencode 时设置 doseq 为 True,
        # 这样 {'key1': 'value1', 'key2': ['value2', 'value3']}
        # 就能转成 key1=value1&key2=value2&key2=value3
        # http://stackoverflow.com/questions/2571145/urlencode-an-array-of-values
        r = yield self._http_call(url=self._join_url(
            self.api_host, "%s.%s" % (action, self.response_type)),
                                  method="POST",
                                  body=urlencode(params, True),
                                  headers=self._headers(),
                                  **kwargs)
        raise gen.Return(r)
Exemplo n.º 2
0
    def api_execute(self, path, method, params=None, timeout=None):
        """ Executes the query. """
        url = self._base_url + path
        if (method == self._MGET) or (method == self._MDELETE):
            if params:
                url = url_concat(url, params)
            request = HTTPRequest(url,
                                  method=method,
                                  request_timeout=timeout,
                                  headers=self._get_default_headers(method),
                                  follow_redirects=self.allow_redirect,
                                  ssl_options=self.cert_options)

        elif (method == self._MPUT) or (method == self._MPOST):
            request = HTTPRequest(url,
                                  method=method,
                                  request_timeout=timeout,
                                  headers=self._get_default_headers(method),
                                  body=urlencode(params),
                                  follow_redirects=self.allow_redirect,
                                  ssl_options=self.cert_options)
        else:
            raise etcdexcept.EtcdException(
                'HTTP method {} not supported'.format(method))
        _log.debug("Request %s %s %s" % (path, method, request.body))
        return self.http.fetch(request)
Exemplo n.º 3
0
    def api_execute(self, path, method, params=None, timeout=None):
        """ Executes the query. """
        url = self._base_url + path

        validate_cert = True if self.cert_options else False
        if (method == self._MGET) or (method == self._MDELETE):
            if params:
                url = url_concat(url, params)
            body = None

        elif (method == self._MPUT) or (method == self._MPOST):
            body = urlencode(params)

        else:
            raise etcdexcept.EtcdException(
                    'HTTP method {} not supported'.format(method))
        request = HTTPRequest(url, method=method,
                              request_timeout=timeout,
                              headers=self._get_default_headers(method),
                              follow_redirects=self.allow_redirect,
                              body=body,
                              validate_cert=validate_cert,
                              ca_certs=self.cert_options.get('ca_certs', None),
                              client_key=self.cert_options.get('client_key', None),
                              client_cert=self.cert_options.get('client_cert', None),
                              auth_username=self.username,
                              auth_password=self.password)
        _log.debug("Request %s %s %s" % (path, method, request.body))
        return self.http.fetch(request)
Exemplo n.º 4
0
    def call_api(self, action, params=None, **kwargs):
        """
        调用API的通用方法,有关SSL证书验证问题请参阅

        http://www.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

        :param action: Method Name,
        :param params: Dictionary,form params for api.
        :param timeout: (optional) Float describing the timeout of the request.
        :return:
        """
        if params is None:
            params = {}

        # 使用 urlencode 时设置 doseq 为 True,
        # 这样 {'key1': 'value1', 'key2': ['value2', 'value3']}
        # 就能转成 key1=value1&key2=value2&key2=value3
        # http://stackoverflow.com/questions/2571145/urlencode-an-array-of-values
        r = yield self._http_call(
            url=self._join_url(self.api_host, "%s.%s" % (action, self.response_type)),
            method="POST",
            body=urlencode(params, True),
            headers=self._headers(),
            **kwargs
        )
        raise gen.Return(r)
Exemplo n.º 5
0
def _get_user_worker(token):
    http_client = httpclient.HTTPClient()
    try:
        authHeader = token
        headers = {
            "Accept": "*/*",
            "Content-Type": "application/json; charset=UTF-8",
            "Authorization": authHeader
        }
        params = {'page': 1, 'limit': 10}

        filters = [{
            'property': "user_workers.status",
            'value': 'ACT',
            'operator': "="
        }]
        params['filter'] = json.dumps(filters)
        params['sort'] = "[]"

        strParams = ""
        if params:
            strParams = "?" + httputil.urlencode(params).encode().decode(
                "utf-8")
        url = config.ADMIN_API_URL + config.BASE_API + "/{0}".format(
            "worker") + strParams
        response = http_client.fetch(url, method="GET", headers=headers)
        data = json.loads(response.body.decode("UTF-8"))
        return data.get("rows")
    except httpclient.HTTPError as e:
        logging.exception(e)
    except Exception as e:
        logging.exception(e)
    http_client.close()
Exemplo n.º 6
0
    def ret(self,*args,**kwargs):
        response_field = self.get_argument('recaptcha_response_field')
        challenge_field = self.get_argument('recaptcha_challenge_field')
        privatekey = self.settings['recaptcha_privkey']
        remote_ip = self.request.remote_ip

        postdata = {
            'privatekey':privatekey,
            'challenge':challenge_field,
            'response':response_field,
            'remoteip':remote_ip
        }

                
        request = HTTPRequest(
            "http://www.google.com/recaptcha/api/verify",
            method="POST",
            body=urlencode(postdata)
        )

        client = AsyncHTTPClient()
        response = yield client.fetch(request)
        
        if not response.error:
            [true_or_false,_] = response.body.decode().split('\n',1)
            true_or_false = true_or_false.strip()
            func(self,captcha=(true_or_false == "true"),
                 *args,**kwargs)
        else:
            pass
Exemplo n.º 7
0
def post(url, data=None, json=None, raise_error=True, **kwargs):
    if kwargs.get('validate_cert') is None:
        kwargs['validate_cert'] = False

    if not isinstance(url, HTTPRequest):
        if data:
            request = HTTPRequest(method="POST",
                                  url=url_concat(url,
                                                 kwargs.get("params", None)),
                                  body=urlencode(data),
                                  **kwargs)
        elif json:
            headers = kwargs.get("headers", {})
            headers['Content-Type'] = "application/json"
            request = HTTPRequest(method="POST",
                                  url=url_concat(url,
                                                 kwargs.get("params", None)),
                                  body=json_mod.dumps(json),
                                  **kwargs)
        else:
            request = HTTPRequest(method="POST",
                                  url=url_concat(url,
                                                 kwargs.get("params", None)),
                                  **kwargs)
    else:
        request = url

    return http_client.fetch(request, raise_error=raise_error)
Exemplo n.º 8
0
 async def post(cls,
                url,
                params=None,
                body=None,
                headers=None,
                encode_type='utf-8',
                decode_type='utf-8',
                parse_json=True,
                timeout=30):
     """ HTTP POST 请求
     @param url 请求url
     @param params 请求的uri qurey参数
     @param body 请求的body参数
     @param headers 请求的header参数
     @param encode_type 请求body编码格式,默认使用utf-8编码
     @param decode_type 返回body解码格式,默认使用utf-8解码
     @param parse_json 是否解析返回body为json格式,默认为True
     @param timeout 请求超时时间,默认30秒
     @return data 返回的http body
     """
     if params:
         url = url_concat(url, params)
     if body:
         if not encode_type:
             pass
         elif encode_type == 'utf-8':
             body = json.dumps(body)
         else:
             body = urlencode(body, encoding=encode_type)
     http_client = AsyncHTTPClient()
     response = await http_client.fetch(url,
                                        method='POST',
                                        body=body,
                                        headers=headers,
                                        request_timeout=timeout)
     if response.code not in (200, 201, 202, 203, 204, 205, 206):
         logger.error('url:',
                      url,
                      'post data:',
                      body,
                      'response code:',
                      response.code,
                      'response body:',
                      response.body,
                      caller=cls)
         msg = '请求url失败: {url}'.format(url=url)
         raise exceptions.CustomException(msg=msg)
     if response.body:
         data = response.body
         if decode_type:
             data = data.decode(decode_type)
         if parse_json:
             return json.loads(data)
         else:
             return data
     else:
         return None
Exemplo n.º 9
0
    def render(this, template_name, **kwargs):
        kwargs["template"] = template_name
        if "self" in kwargs:
            kwargs.pop("self")

        # 分页链接保留get查询参数
        page_query = urlencode([(k, v[0]) for k, v in this.request.query_arguments.items() if k != "page"])
        kwargs["page_query"] = "?page=" if len(page_query) == 0 else "?%s&page=" % page_query

        # return super(BaseHandler, self).render(template_name, **kwargs)  # reload会报错
        return RequestHandler.render(this, template_name, **kwargs)
Exemplo n.º 10
0
    async def refresh_auth(self, handler):
        """Refresh authentication if needed

        Checks authentication expiry and refresh it if needed.
        See Spawner.

        If the auth is expired and cannot be refreshed
        without forcing a new login, a few things can happen:

        1. if this is a normal user spawn,
           the user should be redirected to login
           and back to spawn after login.
        2. if this is a spawn via API or other user,
           spawn will fail until the user logs in again.

        Args:
            handler (RequestHandler):
                The handler for the request triggering the spawn.
                May be None
        """
        authenticator = self.authenticator
        if authenticator is None or not authenticator.refresh_pre_spawn:
            # nothing to do
            return

        # refresh auth
        auth_user = await handler.refresh_auth(self, force=True)

        if auth_user:
            # auth refreshed, all done
            return

        # if we got to here, auth is expired and couldn't be refreshed
        self.log.error(
            "Auth expired for %s; cannot spawn until they login again",
            self.name,
        )
        # auth expired, cannot spawn without a fresh login
        # it's the current user *and* spawn via GET, trigger login redirect
        if handler.request.method == 'GET' and handler.current_user is self:
            self.log.info("Redirecting %s to login to refresh auth", self.name)
            url = self.get_login_url()
            next_url = self.request.uri
            sep = '&' if '?' in url else '?'
            url += sep + urlencode(dict(next=next_url))
            self.redirect(url)
            raise web.Finish()
        else:
            # spawn via POST or on behalf of another user.
            # nothing we can do here but fail
            raise web.HTTPError(
                400, "{}'s authentication has expired".format(self.name))
Exemplo n.º 11
0
    async def refresh_auth(self, handler):
        """Refresh authentication if needed

        Checks authentication expiry and refresh it if needed.
        See Spawner.

        If the auth is expired and cannot be refreshed
        without forcing a new login, a few things can happen:

        1. if this is a normal user spawn,
           the user should be redirected to login
           and back to spawn after login.
        2. if this is a spawn via API or other user,
           spawn will fail until the user logs in again.

        Args:
            handler (RequestHandler):
                The handler for the request triggering the spawn.
                May be None
        """
        authenticator = self.authenticator
        if authenticator is None or not authenticator.refresh_pre_spawn:
            # nothing to do
            return

        # refresh auth
        auth_user = await handler.refresh_auth(self, force=True)

        if auth_user:
            # auth refreshed, all done
            return

        # if we got to here, auth is expired and couldn't be refreshed
        self.log.error(
            "Auth expired for %s; cannot spawn until they login again", self.name
        )
        # auth expired, cannot spawn without a fresh login
        # it's the current user *and* spawn via GET, trigger login redirect
        if handler.request.method == 'GET' and handler.current_user is self:
            self.log.info("Redirecting %s to login to refresh auth", self.name)
            url = self.get_login_url()
            next_url = self.request.uri
            sep = '&' if '?' in url else '?'
            url += sep + urlencode(dict(next=next_url))
            self.redirect(url)
            raise web.Finish()
        else:
            # spawn via POST or on behalf of another user.
            # nothing we can do here but fail
            raise web.HTTPError(
                400, "{}'s authentication has expired".format(self.name)
            )
Exemplo n.º 12
0
def register_user(alias):
    register_url = get_url(HTTPS_PROTOCOL, DOMAIN, REGISTER_PATH)
    method = 'POST'
    headers = {"Content-Type": "application/x-www-form-urlencoded"}

    register_params = {'id': 0, 'alias': alias, 'device_type': DEVICE_TYPE}

    register_body = urlencode(register_params)

    http_client = AsyncHTTPClient()
    register_request = HTTPRequest(register_url, method, headers,
                                   register_body)
    yield http_client.fetch(register_request)
Exemplo n.º 13
0
def get_token(alias):
    yield register_user(alias)
    token_url = get_url(HTTPS_PROTOCOL, DOMAIN, TOKEN_PATH)
    method = 'POST'
    headers = {"Content-Type": "application/x-www-form-urlencoded"}

    # формируем запрос на получение токена
    token_params = {'alias': alias, 'app_id': APP_ID}
    token_body = urlencode(token_params)
    token_request = HTTPRequest(token_url, method, headers, token_body)

    http_client = AsyncHTTPClient()

    # получаем токен
    response = yield http_client.fetch(token_request)
    token = json.loads(response.body).get('token', None)
    raise gen.Return(token)
Exemplo n.º 14
0
 async def req(self, method, path, params):
     url = '{}{}'.format(self.url, path)
     data = httputil.urlencode(params)
     try:
         response = await self.client.fetch(url,
                                            method=method,
                                            body=data,
                                            connect_timeout=3,
                                            request_timeout=10)
         result = json.loads(response.body)
         if result['success'] != True:
             logging.error(f'ziteboard error: {result}')
             raise Exception('ziteboard unsuccess')
     except Exception as e:
         logging.error(f'ziteboard error: {e}', exc_info=True)
         raise
     return result
Exemplo n.º 15
0
    def api_execute(self, path, method, params=None, timeout=None):
        """ Executes the query. """
        url = self._base_url + path
        if (method == self._MGET) or (method == self._MDELETE):
            if params:
                url = url_concat(url, params)
            request = HTTPRequest(url, method=method,
                                  request_timeout=timeout,
                                  headers=self._get_default_headers(method),
                                  follow_redirects=self.allow_redirect,
                                  ssl_options=self.cert_options)

        elif (method == self._MPUT) or (method == self._MPOST):
            request = HTTPRequest(url, method=method,
                                  request_timeout=timeout,
                                  headers=self._get_default_headers(method),
                                  body=urlencode(params),
                                  follow_redirects=self.allow_redirect,
                                  ssl_options=self.cert_options)
        else:
            raise etcdexcept.EtcdException(
                    'HTTP method {} not supported'.format(method))
        _log.debug("Request %s %s %s" % (path, method, request.body))
        return self.http.fetch(request)
Exemplo n.º 16
0
 def add_logs(self):
     api_client = httpclient.AsyncHTTPClient()
     yield api_client.fetch(self.url,
                            method='POST',
                            body=httputil.urlencode(self.data))
Exemplo n.º 17
0
 def __init__(self, url, data=None, param=None, **kwargs):
     body = data or {}
     body.update(kwargs.pop('body', {}))
     if param:
         url = url_concat(url, param)
     super().__init__(url, 'POST', body=urlencode(body), **kwargs)