· 6 years ago · Jan 29, 2020, 10:06 AM
1def call(
2 self,
3 method,
4 path,
5 params=None,
6 headers=None,
7 files=None,
8 url_override=None,
9 api_version=None,
10 ):
11 """Makes an API call.
12 Args:
13 method: The HTTP method name (e.g. 'GET').
14 path: A tuple of path tokens or a full URL string. A tuple will
15 be translated to a url as follows:
16 graph_url/tuple[0]/tuple[1]...
17 It will be assumed that if the path is not a string, it will be
18 iterable.
19 params (optional): A mapping of request parameters where a key
20 is the parameter name and its value is a string or an object
21 which can be JSON-encoded.
22 headers (optional): A mapping of request headers where a key is the
23 header name and its value is the header value.
24 files (optional): An optional mapping of file names to binary open
25 file objects. These files will be attached to the request.
26 Returns:
27 A FacebookResponse object containing the response body, headers,
28 http status, and summary of the call that was made.
29 Raises:
30 FacebookResponse.error() if the request failed.
31 """
32 if not params:
33 params = {}
34 if not headers:
35 headers = {}
36 if not files:
37 files = {}
38
39 api_version = api_version or self._api_version
40
41 if api_version and not re.search('v[0-9]+\.[0-9]+', api_version):
42 raise FacebookBadObjectError(
43 'Please provide the API version in the following format: %s'
44 % self.API_VERSION,
45 )
46
47 self._num_requests_attempted += 1
48
49 if not isinstance(path, six.string_types):
50 # Path is not a full path
51 path = "/".join((
52 url_override or self._session.GRAPH,
53 api_version,
54 '/'.join(map(str, path)),
55 ))
56
57 # Include api headers in http request
58 headers = headers.copy()
59 headers.update(FacebookAdsApi.HTTP_DEFAULT_HEADERS)
60
61 if params:
62 params = _top_level_param_json_encode(params)
63
64 # Get request response and encapsulate it in a FacebookResponse
65 if method in ('GET', 'DELETE'):
66 response = self._session.requests.request(
67 method,
68 path,
69 params=params,
70 headers=headers,
71 files=files,
72 timeout=self._session.timeout
73 )
74
75 else:
76 response = self._session.requests.request(
77 method,
78 path,
79 data=params,
80 headers=headers,
81 files=files,
82 timeout=self._session.timeout
83 )
84 if self._enable_debug_logger:
85 import curlify
86 print(curlify.to_curl(response.request))
87 fb_response = FacebookResponse(
88 body=response.text,
89 headers=response.headers,
90 http_status=response.status_code,
91 call={
92 'method': method,
93 'path': path,
94 'params': params,
95 'headers': headers,
96 'files': files,
97 },
98 )
99
100 if fb_response.is_failure():
101 raise fb_response.error()
102
103 self._num_requests_succeeded += 1
104 return fb_response