Skip to content

Commit

Permalink
new tapi wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelmaksimov committed May 7, 2021
1 parent 3175596 commit 5adc435
Show file tree
Hide file tree
Showing 9 changed files with 1,584 additions and 1,343 deletions.
407 changes: 313 additions & 94 deletions README.md

Large diffs are not rendered by default.

1,533 changes: 674 additions & 859 deletions examples.ipynb

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


try:
from setuptools import setup
Expand Down Expand Up @@ -29,15 +27,15 @@ def get_version(package):
setup(
name="tapi-yandex-direct",
version=get_version(package),
description="Python библиотека для API Яндекс Директ",
description="Python client for API Yandex Direct",
long_description=readme,
long_description_content_type="text/markdown",
author="Pavel Maksimov",
author_email="vur21@ya.ru",
url="https://github.com/pavelmaksimov/tapi-yandex-direct",
packages=[package],
include_package_data=True,
install_requires=["requests-oauthlib>=0.4.2", "tapi-wrapper==2019.12.10"],
install_requires=["requests", "orjson", "tapi-wrapper2>=0.1.1,<0.2"],
license="MIT",
zip_safe=False,
keywords="tapi,wrapper,yandex,metrika,api,direct,яндекс,директ,апи",
Expand Down
7 changes: 3 additions & 4 deletions tapi_yandex_direct/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-

__author__ = 'Pavel Maksimov'
__email__ = 'vur21@ya.com'
__version__ = '2020.12.15'
__email__ = 'vur21@ya.ru'
__version__ = '2021.5.1'


from .tapi_yandex_direct import YandexDirect, GetTokenYandexDirect
from .resource_mapping import *
from .tapi_yandex_direct import YandexDirect
83 changes: 51 additions & 32 deletions tapi_yandex_direct/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
# -*- coding: utf-8 -*-
import logging
import json
from typing import Dict, Union

from requests import Response
from tapi2.tapi import TapiClient


class YandexDirectApiError(Exception):
def __init__(self, response, message=None, *args, **kwargs):
def __init__(
self,
response: Response,
data: Union[str, dict],
client: TapiClient,
*args,
**kwargs
):
self.response = response
self.message = message
self.data = data
self.client = client

def __str__(self):
logging.info("HEADERS = " + str(self.response.headers))
logging.info("URL = " + self.response.url)
return "{}{} {} {}".format(
self.message + " " or "",
return "{} {} {}\nHEADERS = {}\nURL = {}".format(
self.response.status_code,
self.response.reason,
self.response.text,
self.data or self.response.text,
self.response.headers,
self.response.url,
)


class YandexDirectServerError(YandexDirectApiError):
pass


class YandexDirectClientError(YandexDirectApiError):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.jdata = json.loads(self.response.content.decode("utf-8"))
error_data = self.jdata.get("error", {})
self.code = error_data.get("error_code")
self.request_id = error_data.get("request_id")
self.error_string = error_data.get("error_string")
self.error_detail = error_data.get("error_detail")
def __init__(
self,
response: Response,
message: Dict[str, dict],
client: TapiClient,
*args,
**kwargs
):
self.error_code = message["error"]["error_code"]
self.request_id = message["error"]["request_id"]
self.error_string = message["error"]["error_string"]
self.error_detail = message["error"]["error_detail"]
super().__init__(response, message, client, *args, **kwargs)

def __str__(self):
str = "\n\trequest_id={},\n\tcode={},\n\terror_string={},\n\terror_detail={}"
return str.format(
self.request_id, self.code, self.error_string, self.error_detail
text = "request_id={}, error_code={}, error_string={}, error_detail={}"

return text.format(
self.request_id, self.error_code, self.error_string, self.error_detail
)


Expand All @@ -45,15 +55,24 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


class YandexDirectLimitError(YandexDirectApiError):
class YandexDirectNotEnoughUnitsError(YandexDirectClientError):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


class YandexDirectRequestsLimitError(YandexDirectClientError):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


class BackwardCompatibilityError(Exception):
def __init__(self, name):
self.name = name

def __str__(self):
return (
"{} {} Исчерпан лимит запросов. "
"Повторите запрос через некоторое время.\n "
"{}".format(
self.response.status_code, self.response.reason, self.response.text
)
)
"Starting from version 2021.4.26, this {} is deprecated and not supported. "
"Install a later version "
"'pip install --upgrade tapi-yandex-metrika==2020.12.15'. "
"Info https://github.com/pavelmaksimov/tapi-yandex-metrika"
).format(self.name)
16 changes: 12 additions & 4 deletions tapi_yandex_direct/resource_mapping.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8

RESOURCE_MAPPING_V5 = {
"adextensions": {
Expand Down Expand Up @@ -97,11 +96,20 @@
"resource": "json/v5/reports",
"docs": "https://yandex.ru/dev/direct/doc/reports/reports-docpage/",
},
}

RESOURCE_MAPPING_OAUTH = {
"debugtoken": {
"resource": "oauth.yandex.ru/authorize?response_type=token&client_id={client_id}",
"docs": "https://yandex.ru/dev/direct/doc/dg/concepts/auth-token-docpage/",
},
"feeds": {
"resource": "json/v5/feeds",
"docs": "https://yandex.ru/dev/direct/doc/ref-v5/feeds/feeds.html",
},
"smartadtargets": {
"resource": "json/v5/smartadtargets",
"docs": "https://yandex.ru/dev/direct/doc/ref-v5/smartadtargets/smartadtargets.html",
},
"businesses": {
"resource": "json/v5/businesses",
"docs": "https://yandex.ru/dev/direct/doc/ref-v5/businesses/businesses.html",
},
}
Loading

0 comments on commit 5adc435

Please sign in to comment.