-
Notifications
You must be signed in to change notification settings - Fork 4
/
gae_http_client.py
72 lines (53 loc) · 2.02 KB
/
gae_http_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# coding: utf-8
import json
from google.appengine.api import urlfetch
from linebot import HttpClient
from linebot import HttpResponse
class RequestsHttpClient(HttpClient):
def __init__(self, timeout=HttpClient.DEFAULT_TIMEOUT):
super(RequestsHttpClient, self).__init__(timeout)
def get(self, url, headers=None, params=None, stream=False, timeout=None):
if timeout is None:
timeout = self.timeout
response = urlfetch.fetch(url,
payload=params,
method=urlfetch.GET,
headers=headers,
deadline=timeout)
return RequestsHttpResponse(response)
def post(self, url, headers=None, data=None, timeout=None):
if timeout is None:
timeout = self.timeout
response = urlfetch.fetch(url,
payload=data,
method=urlfetch.POST,
headers=headers,
deadline=timeout)
return RequestsHttpResponse(response)
class RequestsHttpResponse(HttpResponse):
def __init__(self, response):
self.response = response
@property
def status_code(self):
return self.response.status_code
@property
def headers(self):
return self.response.headers
@property
def text(self):
return unicode(self.content, 'UTF-8', errors='replace')
@property
def content(self):
return self.response.content
@property
def json(self):
# return self.response.json()
return json.loads(self.content)
def iter_content(self, chunk_size=1024, decode_unicode=False):
"""Get request body as iterator content (stream).
:param int chunk_size:
:param bool decode_unicode:
"""
# return self.response.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode)
return self.content