소스 검색

add BaseClient and Prestashop client

sxoinas12 2 년 전
부모
커밋
8a179933f1
2개의 변경된 파일53개의 추가작업 그리고 0개의 파일을 삭제
  1. 23 0
      telecaster/telecaster/Clients/BaseClient.py
  2. 30 0
      telecaster/telecaster/Clients/PrestaShopClient.py

+ 23 - 0
telecaster/telecaster/Clients/BaseClient.py

@@ -0,0 +1,23 @@
+import requests
+
+
+class BaseClient:
+    def get(self, url):
+        response = requests.get(url)
+        if response and response.status_code == 200:
+            return response.content
+        else:
+            print('Something went wrong')
+            return response.status_code
+
+    def post(self, url, body):
+        pass
+
+    def patch(self):
+        pass
+
+    def delete(self):
+        pass
+
+    def put(self):
+        pass

+ 30 - 0
telecaster/telecaster/Clients/PrestaShopClient.py

@@ -0,0 +1,30 @@
+from .BaseClient import BaseClient
+import urllib.parse
+
+
+class PrestaShopClient(BaseClient):
+    def __init__(self, base_url, token):
+        self.token = token
+        self.base_url = base_url
+
+    def build_url(self, url, params={}):
+        updated_params = {**params, 'ws_key': self.token}
+        return f'{self.base_url}{url}?{urllib.parse.urlencode(updated_params)}'
+
+    def get_products(self, params={}):
+        constructed_params = {
+            'display': '[name,id,id_default_image,id_category_default,price,wholesale_price,manufacturer_name,ean13,delivery_in_stock,additional_shipping_cost,description,quantity]',
+            **params,
+        }
+        full_url = self.build_url('/products', constructed_params)
+        # we need to parse these since the response is in XML
+        # check parsers folder for relevant functions/classes
+        products = self.get(full_url)
+
+        return products
+
+    def get_categories(self, params={}):
+        constructed_params = {'display': '[id,name]', **params}
+        full_url = self.build_url('/categories', constructed_params)
+        categories = self.get(full_url)
+        return categories