本文整理汇总了Python中requests.utils.to_native_string方法的典型用法代码示例。如果您正苦于以下问题:Python utils.to_native_string方法的具体用法?Python utils.to_native_string怎么用?Python utils.to_native_string使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.utils
的用法示例。
在下文中一共展示了utils.to_native_string方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from requests import utils [as 别名]
# 或者: from requests.utils import to_native_string [as 别名]
def __call__(self, request):
timestamp = str(int(time.time()))
message = timestamp + request.method + request.path_url + (request.body or '')
secret = self.api_secret
if not isinstance(message, bytes):
message = message.encode()
if not isinstance(secret, bytes):
secret = secret.encode()
signature = hmac.new(secret, message, hashlib.sha256).hexdigest()
request.headers.update({
to_native_string('CB-VERSION'): self.api_version,
to_native_string('CB-ACCESS-KEY'): self.api_key,
to_native_string('CB-ACCESS-SIGN'): signature,
to_native_string('CB-ACCESS-TIMESTAMP'): timestamp,
})
return request
示例2: __call__
# 需要导入模块: from requests import utils [as 别名]
# 或者: from requests.utils import to_native_string [as 别名]
def __call__(self, r):
r = super(MKMOAuth1, self).__call__(r)
r.prepare_headers(r.headers)
correct_signature = self.decode_signature(r.headers)
r.headers.__setitem__("Authorization", correct_signature)
r.url = to_native_string(r.url)
return r
示例3: __call__
# 需要导入模块: from requests import utils [as 别名]
# 或者: from requests.utils import to_native_string [as 别名]
def __call__(self, r):
"""Add OAuth parameters to the request.
Parameters may be included from the body if the content-type is
urlencoded, if no content type is set a guess is made.
"""
# Overwriting url is safe here as request will not modify it past
# this point.
log.debug('Signing request %s using client %s', r, self.client)
content_type = r.headers.get('Content-Type', '')
if (not content_type and extract_params(r.body)
or self.client.signature_type == SIGNATURE_TYPE_BODY):
content_type = CONTENT_TYPE_FORM_URLENCODED
if not isinstance(content_type, unicode):
content_type = content_type.decode('utf-8')
is_form_encoded = (CONTENT_TYPE_FORM_URLENCODED in content_type)
log.debug('Including body in call to sign: %s',
is_form_encoded or self.force_include_body)
if is_form_encoded:
r.headers['Content-Type'] = CONTENT_TYPE_FORM_URLENCODED
r.url, headers, r.body = self.client.sign(
unicode(r.url), unicode(r.method), r.body or '', r.headers)
elif self.force_include_body:
# To allow custom clients to work on non form encoded bodies.
r.url, headers, r.body = self.client.sign(
unicode(r.url), unicode(r.method), r.body or '', r.headers)
else:
# Omit body data in the signing of non form-encoded requests
r.url, headers, _ = self.client.sign(
unicode(r.url), unicode(r.method), None, r.headers)
r.prepare_headers(headers)
r.url = to_native_string(r.url)
log.debug('Updated url: %s', r.url)
log.debug('Updated headers: %s', headers)
log.debug('Updated body: %r', r.body)
return r
示例4: upload_image
# 需要导入模块: from requests import utils [as 别名]
# 或者: from requests.utils import to_native_string [as 别名]
def upload_image(self, subreddit, image_path, name=None,
header=False, upload_as=None):
"""Upload an image to the subreddit.
:param image_path: A path to the jpg or png image you want to upload.
:param name: The name to provide the image. When None the name will be
filename less any extension.
:param header: When True, upload the image as the subreddit header.
:param upload_as: Must be `'jpg'`, `'png'` or `None`. When None, this
will match the format of the image itself. In all cases where both
this value and the image format is not png, reddit will also
convert the image mode to RGBA. reddit optimizes the image
according to this value.
:returns: A link to the uploaded image. Raises an exception otherwise.
"""
if name and header:
raise TypeError('Both name and header cannot be set.')
if upload_as not in (None, 'png', 'jpg'):
raise TypeError("upload_as must be 'jpg', 'png', or None.")
with open(image_path, 'rb') as image:
image_type = upload_as or _image_type(image)
data = {'r': six.text_type(subreddit), 'img_type': image_type}
if header:
data['header'] = 1
else:
if not name:
name = os.path.splitext(os.path.basename(image.name))[0]
data['name'] = name
response = json.loads(self._request(
self.config['upload_image'], data=data, files={'file': image},
method=to_native_string('POST'), retry_on_error=False))
if response['errors']:
raise errors.APIException(response['errors'], None)
return response['img_src']