3 Commits da06d14896 ... 4c3aaa7f57

Auteur SHA1 Bericht Datum
  sxoinas12 4c3aaa7f57 add readme file for the project 2 jaren geleden
  sxoinas12 78e7667383 created basic implementation of PrestaShop parser 2 jaren geleden
  sxoinas12 8a179933f1 add BaseClient and Prestashop client 2 jaren geleden

+ 26 - 0
README.md

@@ -0,0 +1,26 @@
+# Telecaster Project Instructions
+
+### Fetch repository
+#### `git clone http://onarbooks.com/Klapi/Telecaster.git`
+
+## Setup
+#### `cd telecaster`
+
+### Install requirments
+#### `pip3 install -r requirments.txt`
+
+### How to test?
+
+#### `cd telecaster`
+
+#### `python manage.py runserver`
+
+#### Open postman and perform a `POST` request with the below payload
+
+#### `url: localhost:8000/generate/xml`
+```
+{
+    "url": "prestashop_domain",
+    "token": "your_secret_token"
+}
+```

+ 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

+ 28 - 0
telecaster/telecaster/parsers/PrestaShopParser.py

@@ -0,0 +1,28 @@
+import xml.etree.ElementTree as ElementTree
+
+
+class PrestaShopParser:
+    def __init__(self):
+        pass
+
+    @classmethod
+    # WIP, parser is not finished yet
+    def categories_helper_parser(self, xmlCategories):
+        response = {}
+        for child in list(xmlCategories):
+            if len(list(child)) > 0:
+                if child.tag == 'category':
+                    self.categories_helper_parser(child)
+                else:
+                    response[child.tag] = self.categories_helper_parser(child)
+            else:
+                response[child.tag] = child.text or ''
+
+        return response
+
+    # parses xml categories response to a dictionary
+    @classmethod
+    def parse_categories(self, xmlCategoriesContent):
+        xmlCategories = ElementTree.fromstring(xmlCategoriesContent)
+        categories = self.categories_helper_parser(xmlCategories)
+        return categories

+ 1 - 1
telecaster/telecaster/parsers/parse_xml_to_json.py

@@ -19,4 +19,4 @@ def parse_prestashop_xml_products(xml):
         else:
             response[child.attrib.get('id')] = child.attrib
 
-    return response
+    return response

+ 11 - 8
telecaster/telecaster/views/XmlGeneratorView.py

@@ -2,9 +2,12 @@ import requests
 
 from rest_framework import views
 from rest_framework.request import Request
-import xml.etree.ElementTree as ElementTree
+
 from django.http import JsonResponse
 from ..parsers.parse_xml_to_json import parse_xml_to_json
+from ..Clients.PrestaShopClient import PrestaShopClient
+from ..parsers.PrestaShopParser import PrestaShopParser
+
 
 class XmlGeneratorView(views.APIView):
     @classmethod
@@ -14,13 +17,13 @@ class XmlGeneratorView(views.APIView):
     @classmethod
     def post(cls, request: Request, *args, **kwargs) -> JsonResponse:
         url = request.data.get('url')
+        token = request.data.get('token')
+
+        prestashop_client = PrestaShopClient(base_url=url, token=token)
 
-        if url:
-            response = requests.get(url)
-            if response and response.status_code == 200:
-                response_xml_as_string = response.content
-                response_xml = ElementTree.fromstring(response_xml_as_string)
-                response_json = parse_xml_to_json(response_xml)
+        products_response = prestashop_client.get_products({'limit': 4})
+        categories_response = prestashop_client.get_categories({'limit': 4})
+        categories = PrestaShopParser.parse_categories(categories_response)
 
-        #serialized_response = XmlGeneratorSerializer.for_api()
+        response_json = {}
         return JsonResponse(response_json, safe=False)