123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #
- # Copyright 2021 The FATE Authors. All Rights Reserved.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- import functools
- import json
- from base64 import b64encode
- from hmac import HMAC
- from time import time
- from urllib.parse import quote, urlencode
- from uuid import uuid1
- import requests
- from fate_arch.common.base_utils import CustomJSONEncoder
- from fate_flow.settings import CLIENT_AUTHENTICATION, HTTP_APP_KEY, HTTP_SECRET_KEY
- requests.models.complexjson.dumps = functools.partial(json.dumps, cls=CustomJSONEncoder)
- def request(**kwargs):
- sess = requests.Session()
- stream = kwargs.pop('stream', sess.stream)
- timeout = kwargs.pop('timeout', None)
- kwargs['headers'] = {k.replace('_', '-').upper(): v for k, v in kwargs.get('headers', {}).items()}
- prepped = requests.Request(**kwargs).prepare()
- if CLIENT_AUTHENTICATION and HTTP_APP_KEY and HTTP_SECRET_KEY:
- timestamp = str(round(time() * 1000))
- nonce = str(uuid1())
- signature = b64encode(HMAC(HTTP_SECRET_KEY.encode('ascii'), b'\n'.join([
- timestamp.encode('ascii'),
- nonce.encode('ascii'),
- HTTP_APP_KEY.encode('ascii'),
- prepped.path_url.encode('ascii'),
- prepped.body if kwargs.get('json') else b'',
- urlencode(sorted(kwargs['data'].items()), quote_via=quote, safe='-._~').encode('ascii')
- if kwargs.get('data') and isinstance(kwargs['data'], dict) else b'',
- ]), 'sha1').digest()).decode('ascii')
- prepped.headers.update({
- 'TIMESTAMP': timestamp,
- 'NONCE': nonce,
- 'APP-KEY': HTTP_APP_KEY,
- 'SIGNATURE': signature,
- })
- return sess.send(prepped, stream=stream, timeout=timeout)
|