This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
arturgspb
committed
Jun 22, 2018
1 parent
3bc6a40
commit 901b490
Showing
6 changed files
with
144 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from metaappscriptsdk import MetaApp | ||
|
||
META = MetaApp() | ||
|
||
|
||
refresh_token ="XXXXX" | ||
ga_property_id = 'XXXX' | ||
|
||
resp = META.ApiProxyService.call_proxy("google_analytics", { | ||
"refresh_token": refresh_token, | ||
"version": "v4", | ||
"method": "reports.batchGet", | ||
"method_params": { | ||
"body": { | ||
# "useResourceQuotas": True, # Включение привелений GA360 | ||
"reportRequests": [ | ||
{ | ||
"viewId": ga_property_id, | ||
"dateRanges": [ | ||
{ | ||
"startDate": "2015-06-15", | ||
"endDate": "2015-06-30" | ||
}], | ||
"metrics": [ | ||
{ | ||
"expression": "ga:sessions" | ||
}], | ||
"dimensions": [ | ||
{ | ||
"name": "ga:browser" | ||
}] | ||
}] | ||
} | ||
} | ||
}, "native_call", False, []) | ||
print(u"resp = %s" % str(resp.text)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '0.5.37' | ||
__version__ = '0.6.0' | ||
__package_name__ = 'metaappscriptsdk' | ||
|
||
if __name__ == "__main__": | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import json | ||
|
||
import requests | ||
import time | ||
|
||
from metaappscriptsdk.exceptions import RetryHttpRequestError, EndOfTriesError, UnexpectedError, ApiProxyError | ||
|
||
|
||
class ApiProxyService: | ||
def __init__(self, app, default_headers): | ||
""" | ||
:type app: metaappscriptsdk.MetaApp | ||
""" | ||
self.__app = app | ||
self.__default_headers = default_headers | ||
self.__options = {} | ||
self.__data_get_cache = {} | ||
self.__data_get_flatten_cache = {} | ||
|
||
def call_proxy(self, engine, payload, method, analyze_json_error_param, retry_request_substr_variants, stream=False): | ||
""" | ||
:param engine: Система | ||
:param payload: Данные для запроса | ||
:param method: string Может содержать native_call | tsv | json_newline | ||
:param analyze_json_error_param: Нужно ли производить анализ параметра error d jndtnt ghjrcb | ||
:param retry_request_substr_variants: Список подстрок, при наличии которых в ответе будет происходить перезапрос | ||
:param stream: | ||
:return: | ||
""" | ||
log_ctx = {"engine": engine, "method": payload.get('method'), "method_params": payload.get('method_params')} | ||
self.__app.log.info("Call api proxy", log_ctx) | ||
body = { | ||
"engine": engine, | ||
"payload": payload | ||
} | ||
for try_idx in range(20): | ||
try: | ||
# 1h таймаут, так как бывают большие долгие данные, а лимит хоть какой-то нужен | ||
body_str = json.dumps(body) | ||
resp = requests.post(self.__app.api_proxy_url + "/" + method, body_str, timeout=3600, stream=stream, headers={ | ||
"User-Agent": self.__app.user_agent | ||
}) | ||
|
||
self.check_err(resp, analyze_json_error_param=analyze_json_error_param, retry_request_substr_variants=retry_request_substr_variants) | ||
return resp | ||
except RetryHttpRequestError as e: | ||
self.__app.log.warning("Sleep retry query: " + str(e.err_details) if e.err_details else "", log_ctx) | ||
time.sleep(20) | ||
raise EndOfTriesError("Api of api proxy tries request") | ||
|
||
def check_err(self, resp, analyze_json_error_param=False, retry_request_substr_variants=None): | ||
""" | ||
:type retry_request_substr_variants: list Список вхождений строк, при налиции которых в ошидке апи будет произведен повторный запрос к апи | ||
""" | ||
if retry_request_substr_variants is None: | ||
retry_request_substr_variants = [] | ||
|
||
# РКН блокировки вызывают ошибку SSL | ||
retry_request_substr_variants.append("TLSV1_ALERT_ACCESS_DENIED") | ||
|
||
if resp.status_code >= 400: | ||
rtext = resp.text | ||
for v_ in retry_request_substr_variants: | ||
if v_ in rtext: | ||
raise RetryHttpRequestError(rtext) | ||
raise UnexpectedError("HTTP request failed: {} {}".format(resp.status_code, rtext)) | ||
if analyze_json_error_param: | ||
data_ = resp.json() | ||
if 'error' in data_ and data_.get('error'): | ||
full_err_ = json.dumps(data_.get('error')) | ||
for v_ in retry_request_substr_variants: | ||
if v_ in full_err_: | ||
raise RetryHttpRequestError(full_err_) | ||
raise ApiProxyError(full_err_) | ||
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters