PK#fM iipyshoper/__init__.py"""Python client to shoper.pl REST api""" import pyshoper.api_helpers as exceptions __version__ = '0.1' PKadMm@##pyshoper/api.pyimport uplink from .api_helpers import error_map, ShoperApiError def raise_for_status(response): """Checks whether or not the response was successful.""" if response.status_code != 200: res_data = response.json() if (response.status_code, res_data['error']) in error_map: raise error_map[(response.status_code, res_data['error'])](res_data['error_description']) raise ShoperApiError(res_data['error_description']) return response def add_shoper_get_method(cls, name, path): @uplink.returns.json @uplink.response_handler(raise_for_status) @uplink.get(path + '/{id}') def shoper_get_method(self, id: uplink.Path): """Fetches an object.""" pass setattr(cls, f"{name}_get", shoper_get_method) def add_shoper_list_method(cls, name, path): @uplink.returns.json @uplink.response_handler(raise_for_status) @uplink.get(path) def shoper_list_method(self, limit: uplink.Query = 10, page: uplink.Query = 1, filters: uplink.Query=''): """Fetches objects collection.""" pass setattr(cls, f"{name}_list", shoper_list_method) def add_shoper_delete_method(cls, name, path): @uplink.returns.json @uplink.response_handler(raise_for_status) @uplink.delete(path + '/{id}') def shoper_delete_method(self, id: uplink.Path): """Deletes an object.""" pass setattr(cls, f"{name}_delete", shoper_delete_method) def add_shoper_insert_method(cls, name, path): @uplink.returns.json @uplink.response_handler(raise_for_status) @uplink.json @uplink.post(path) def shoper_insert_method(self, body: uplink.Body): """Fetches an object.""" pass setattr(cls, f"{name}_insert", shoper_insert_method) def add_shoper_update_method(cls, name, path): @uplink.returns.json @uplink.response_handler(raise_for_status) @uplink.json @uplink.put(path + '/{id}') def shoper_update_method(self, id: uplink.Path, body: uplink.Body): pass setattr(cls, f"{name}_update", shoper_update_method) def add_shoper_methods_set(cls, name, path, methods=('get', 'delete', 'insert', 'list', 'update')): if 'get' in methods: add_shoper_get_method(cls, name, path) if 'list' in methods: add_shoper_list_method(cls, name, path) if 'delete' in methods: add_shoper_delete_method(cls, name, path) if 'insert' in methods: add_shoper_insert_method(cls, name, path) if 'update' in methods: add_shoper_update_method(cls, name, path) @uplink.returns.json @uplink.response_handler(raise_for_status) class ShoperApi(uplink.Consumer): @uplink.post("/webapi/rest/auth") def authentication(self, client_id: uplink.Query("client_id"), client_secret: uplink.Query("client_secret")): pass def __init__(self, username, password, **kwargs): super().__init__(**kwargs) response = self.authentication(username, password) access_token = response['access_token'] self.session.headers["Authorization"] = f'Bearer {access_token}' shoper_resources = [ { 'name': 'aboutpages', 'path': '/webapi/rest/aboutpages/', }, { 'name': 'additional_fields', 'path': '/webapi/rest/additional-fields', }, { 'name': 'application_config', 'path': '/webapi/rest/application-config', 'methods': ('list',), }, { 'name': 'application_lock', 'path': '/webapi/rest/application-lock', 'methods': ('get', 'delete', 'insert', 'update'), }, { 'name': 'application_version', 'path': '/webapi/rest/application-version', 'methods': ('list',), }, { 'name': 'attribute_groups', 'path': '/webapi/rest/attribute-groups', }, { 'name': 'attributes', 'path': '/webapi/rest/attributes', }, { 'name': 'auction_houses', 'path': '/webapi/rest/auction-houses', }, { 'name': 'auction_orders', 'path': '/webapi/rest/auction-orders', 'methods': ('get', 'list', 'insert', 'update'), }, { 'name': 'auctions', 'path': '/webapi/rest/auctions', }, { 'name': 'availabilities', 'path': '/webapi/rest/availabilities', 'methods': ('get', 'list'), }, { 'name': 'categories_tree', 'path': '/webapi/rest/categories-tree', 'methods': ('get', 'list'), }, { 'name': 'categories', 'path': '/webapi/rest/categories', }, { 'name': 'currencies', 'path': '/webapi/rest/currencies', 'methods': ('get', 'list'), }, { 'name': 'dashboard_activities', 'path': '/webapi/rest/dashboard-activities', 'methods': ('list',), }, { 'name': 'dashboard_stats', 'path': '/webapi/rest/dashboard-stats', 'methods': ('get',), }, { 'name': 'deliveries', 'path': '/webapi/rest/deliveries', 'methods': ('get', 'list'), }, { 'name': 'gauges', 'path': '/webapi/rest/gauges', 'methods': ('get', 'list'), }, { 'name': 'geolocation_countries', 'path': '/webapi/rest/geolocation-countries', 'methods': ('get', 'list'), }, { 'name': 'geolocation_regions', 'path': '/webapi/rest/geolocation-regions', 'methods': ('get', 'list'), }, { 'name': 'geolocation_subregions', 'path': '/webapi/rest/geolocation-subregions', 'methods': ('get', 'list'), }, { 'name': 'languages', 'path': '/webapi/rest/languages', 'methods': ('get', 'list'), }, { 'name': 'metafield_values', 'path': '/webapi/rest/metafield-values', }, # { # 'name': 'metafields', # 'path': '/webapi/rest/metafields/', # }, { 'name': 'news_categories', 'path': '/webapi/rest/news-categories', }, { 'name': 'news_comments', 'path': '/webapi/rest/news-comments', }, { 'name': 'news_files', 'path': '/webapi/rest/news-files', }, { 'name': 'news_tags', 'path': '/webapi/rest/news-tags', }, { 'name': 'news', 'path': '/webapi/rest/news', }, { 'name': 'object_mtime', 'path': '/webapi/rest/object-mtime', 'methods': ('get',), }, { 'name': 'option_groups', 'path': '/webapi/rest/option-groups', }, { 'name': 'option_values', 'path': '/webapi/rest/option-values', }, { 'name': 'options', 'path': '/webapi/rest/options', }, { 'name': 'order_products', 'path': '/webapi/rest/order-products', }, { 'name': 'orders', 'path': '/webapi/rest/orders', }, { 'name': 'parcels', 'path': '/webapi/rest/parcels', 'methods': ('get', 'list', 'insert', 'update'), }, { 'name': 'payments', 'path': '/webapi/rest/payments', 'methods': ('get', 'list'), }, { 'name': 'producers', 'path': '/webapi/rest/producers', }, { 'name': 'product_files', 'path': '/webapi/rest/product-files', }, { 'name': 'product_images', 'path': '/webapi/rest/product-images', }, { 'name': 'product_stocks', 'path': '/webapi/rest/product-stocks', }, { 'name': 'products', 'path': '/webapi/rest/products', }, { 'name': 'shippings', 'path': '/webapi/rest/shippings', }, { 'name': 'statuses', 'path': '/webapi/rest/statuses', 'methods': ('get', 'list'), }, { 'name': 'subscriber-groups', 'path': '/webapi/rest/subscriber-groups', }, { 'name': 'product_stocks', 'path': '/webapi/rest/product-stocks', }, { 'name': 'subscribers', 'path': '/webapi/rest/subscribers', }, { 'name': 'taxes', 'path': '/webapi/rest/taxes', 'methods': ('get', 'list'), }, { 'name': 'units', 'path': '/webapi/rest/units', }, { 'name': 'user_addresses', 'path': '/webapi/rest/user-addresses', }, { 'name': 'user_groups', 'path': '/webapi/rest/user-groups', }, { 'name': 'users', 'path': '/webapi/rest/users', }, { 'name': 'webhooks', 'path': '/webapi/rest/webhooks', }, { 'name': 'zones', 'path': '/webapi/rest/zones', }, ] for resource in shoper_resources: if 'methods' in resource: add_shoper_methods_set(ShoperApi, resource['name'], resource['path'], resource['methods']) else: add_shoper_methods_set(ShoperApi, resource['name'], resource['path'])PK1V_M'A1   pyshoper/api_helpers.pyclass ShoperApiError(Exception): """Base class for shoper api exceptions""" pass class InvalidRequest(ShoperApiError): """Raised when invalid request""" pass class InvalidRequestInsufficientPermissions(ShoperApiError): """Raised when invalid request - insufficient permissions""" pass class InvalidRequestInvalidAuthenticationMethod(ShoperApiError): """Raised when invalid request - invalid authentication method""" pass class AuthenticationError(ShoperApiError): """Raised when authentication error""" pass class PaymentRequiredError(ShoperApiError): """Raised when payment required""" pass class AccessDeniedError(ShoperApiError): """Raised when access denied """ pass class ObjectNotExistError(ShoperApiError): """Raised when an object doesn't exist""" pass class InvalidRequestMethodError(ShoperApiError): """Raised when invalid request method""" pass class AdministratorLockConflictError(ShoperApiError): """Raised when conflict - another administrator has locked an access to the object""" pass class CallsLimitExceededError(ShoperApiError): """Raised when calls limit exceeded""" pass class ApplicationError(ShoperApiError): """Raised when application error""" pass class MethodNotImplementedError(ShoperApiError): """Raised when method not implemented""" pass class ApplicationLockError(ShoperApiError): """Raised when system is temporarily unavailable (application has been completely locked by administrator)""" pass error_map = { (400, 'invalid_request'): InvalidRequest, (400, 'invalid_scope'): InvalidRequestInsufficientPermissions, (400, 'invalid_grant'): InvalidRequestInvalidAuthenticationMethod, (401, 'unauthorized_client'): AuthenticationError, (402, 'access_denied'): PaymentRequiredError, (403, 'insufficient_scope'): AccessDeniedError, (404, 'server_error'): ObjectNotExistError, (405, 'invalid_request'): InvalidRequestMethodError, (409, 'server_error'): AdministratorLockConflictError, (429, 'temporarily_unavailable'): CallsLimitExceededError, (500, 'server_error'): ApplicationError, (501, 'server_error'): MethodNotImplementedError, (503, 'temporarily_unavailable'): ApplicationLockError, } PK`_eMazpyshoper/client.pyimport json import backoff from pyshoper.api import ShoperApi, shoper_resources from pyshoper import exceptions API_METHOD_MAX_RETRIES = 12 API_METHOD_MAX_TIME = 60 * 15 class ShoperClient: def __init__(self, username, password, base_url, max_tries=API_METHOD_MAX_RETRIES, max_time=API_METHOD_MAX_TIME): self.username = username self.password = password self.api = ShoperApi(username, password, base_url=base_url) self.max_tries = max_tries self.max_time = max_time self.special_methods = dict() self._preapre_clinet() def _preapre_clinet(self): for resource in shoper_resources: methods = resource.get('methods', ('get', 'delete', 'insert', 'list', 'update')) if 'list' in methods: self.special_methods[f'{resource["name"]}_generator'] = f'{resource["name"]}_list' @staticmethod def _list_resource_decorator(method): def new_method(*args, **kwargs): if 'filters' in kwargs: kwargs['filters'] = json.dumps(kwargs['filters']) result = method(*args, **kwargs) return result return new_method def _list_resource_generator(self, method): method = backoff.on_exception(backoff.expo, exceptions.CallsLimitExceededError, max_tries=self.max_tries, max_time=self.max_time, jitter=None)(method) def new_method(*args, **kwargs): page = 1 while True: kwargs['page'] = page result = method(*args, **kwargs) if not result['list']: break for elem in result['list']: yield elem page += 1 return new_method def __getattr__(self, item): if item in self.special_methods: orig_attr = self.__getattr__(self.special_methods[item]) return self._list_resource_generator(orig_attr) orig_attr = self.api.__getattribute__(item) if item.endswith('_list'): return self._list_resource_decorator(orig_attr) else: return orig_attr PKafM`Mz22pyshoper-0.1.dist-info/LICENSEMIT License Copyright (c) 2018 Przemysław Łada Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H>*RQpyshoper-0.1.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UrPK!HAuupyshoper-0.1.dist-info/METADATAT]O0}PIS>h 6mD0M&UlgP:i;hGX `}ZU<g0LO&pS @Bu̒λu6RjH7:boa9 -vAShA 4p^W moBYS'Prs'r:̞g#YTè1wxMͶ%vuDA;S.DqmAeaXAtʐL:Z^w&Bz:%;o S0 ڹܓ:jæjYȗBMK1S?0'uoY})m萦v|'N/mb껴=,܊70zު8Gr@~ca&IHd-uf汥xܯn2>Sg*Is(%`2]GHؖs€4N*5 ('"n H^HڀZ^Ȭ9.LxڜO=bhIvG7aþ:RUp~«NDy.!%P#:˝oC21![6aS;EfhT!\%o"hm5"SRd5isH}PK#fM iipyshoper/__init__.pyPKadMm@##pyshoper/api.pyPK1V_M'A1   #pyshoper/api_helpers.pyPK`_eMaz-pyshoper/client.pyPKafM`Mz225pyshoper-0.1.dist-info/LICENSEPK!H>*RQL:pyshoper-0.1.dist-info/WHEELPK!HAuu:pyshoper-0.1.dist-info/METADATAPK!H{;LQ=pyshoper-0.1.dist-info/RECORDPK2?