From 6341ae7c63f90fbf8103c60a9d3a289115f62086 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 24 Aug 2023 20:02:51 +0200 Subject: [PATCH] Fork pykeydelivery for glsapi --- .gitignore | 3 +- .gitlab-ci.yml | 12 +++- LICENSE | 2 +- README.md | 16 +++-- config.dist.ini | 3 - pyproject.toml | 10 +-- src/{pykeydelivery => glsapi}/__init__.py | 0 src/glsapi/classes/__init__.py | 2 + src/glsapi/classes/api.py | 33 ++++++++++ src/{pykeydelivery => glsapi}/classes/http.py | 6 +- src/pykeydelivery/classes/__init__.py | 2 - src/pykeydelivery/classes/keydelivery.py | 61 ------------------- test.py | 34 +++-------- 13 files changed, 69 insertions(+), 115 deletions(-) delete mode 100644 config.dist.ini rename src/{pykeydelivery => glsapi}/__init__.py (100%) create mode 100644 src/glsapi/classes/__init__.py create mode 100644 src/glsapi/classes/api.py rename src/{pykeydelivery => glsapi}/classes/http.py (61%) delete mode 100644 src/pykeydelivery/classes/__init__.py delete mode 100644 src/pykeydelivery/classes/keydelivery.py diff --git a/.gitignore b/.gitignore index f20399b..7fededd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .venv config.ini __pycache__ -*.pyc \ No newline at end of file +*.pyc +dist/ \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e5a66bb..0598623 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,10 +9,16 @@ before_script: - source venv/bin/activate - pip install -U pip - pip install . - - echo "[KeyDelivery]" > config.ini - - echo "key = ${API_KEY}" >> config.ini - - echo "secret = ${API_SECRET}" >> config.ini test: stage: test script: python -m unittest test.py + +publish: + stage: publish + script: + - pip install -U hatchling twine build + - python -m build . + - python -m twine upload --username __token__ --password ${PYPI_TOKEN} dist/* + only: + - tags \ No newline at end of file diff --git a/LICENSE b/LICENSE index cad43dc..9f997c0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2023 Kumi Mitterer +Copyright (c) 2023 Kumi Mitterer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index e5c44cd..1a8a0ab 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,27 @@ -# KeyDelivery API Python Client +# GLS REST API Python Client -This is a Python client for the KeyDelivery API. It is a wrapper around the [KeyDelivery](https://kd100.com/) API, which allows you to track your shipments. +This is a Python client for the GLS (https://gls-group.eu) REST API. It allows you to track your shipments. -It is not fully featured yet, but it is a good starting point. +It currently only supports package tracking, not any other API endpoints. ## Installation ```bash -pip install git+https://kumig.it/kumitterer/pykeydelivery +pip install git+https://kumig.it/kumitterer/glsapi.git ``` ## Usage ```python -from keydelivery import KeyDelivery +from glsapi import GLSAPI -api = KeyDelivery("YOUR_API_KEY", "YOUR_API_SECRET") - -# Find carrier by shipment number +api = GLSAPI() carrier_options = api.detect_carrier("YOUR_SHIPMENT_NUMBER") # Realtime tracking -tracking = api.realtime("CARRIER_CODE", "YOUR_SHIPMENT_NUMBER") +tracking = api.tracking("YOUR_SHIPMENT_NUMBER") ``` ## License diff --git a/config.dist.ini b/config.dist.ini deleted file mode 100644 index bac223f..0000000 --- a/config.dist.ini +++ /dev/null @@ -1,3 +0,0 @@ -[KeyDelivery] -key = api_key -secret = api_secret diff --git a/pyproject.toml b/pyproject.toml index 54ad196..1dcc735 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,12 +3,12 @@ requires = ["hatchling"] build-backend = "hatchling.build" [project] -name = "pykeydelivery" +name = "glsapi" version = "0.9.0" authors = [ - { name="Kumi Mitterer", email="pykeydelivery@kumi.email" }, + { name="Kumi Mitterer", email="glsapi@kumi.email" }, ] -description = "Simple Python wrapper to fetch data from KeyDelivery (kd100.com)" +description = "Simple Python wrapper to fetch data from GLS (gls-group.eu)" readme = "README.md" license = { file="LICENSE" } requires-python = ">=3.10" @@ -19,5 +19,5 @@ classifiers = [ ] [project.urls] -"Homepage" = "https://kumig.it/kumitterer/pykeydelivery" -"Bug Tracker" = "https://kumig.it/kumitterer/pykeydelivery/issues" \ No newline at end of file +"Homepage" = "https://kumig.it/kumitterer/glsapi" +"Bug Tracker" = "https://kumig.it/kumitterer/glsapi/issues" \ No newline at end of file diff --git a/src/pykeydelivery/__init__.py b/src/glsapi/__init__.py similarity index 100% rename from src/pykeydelivery/__init__.py rename to src/glsapi/__init__.py diff --git a/src/glsapi/classes/__init__.py b/src/glsapi/classes/__init__.py new file mode 100644 index 0000000..36a9e81 --- /dev/null +++ b/src/glsapi/classes/__init__.py @@ -0,0 +1,2 @@ +from .http import HTTPRequest +from .api import GLSAPI \ No newline at end of file diff --git a/src/glsapi/classes/api.py b/src/glsapi/classes/api.py new file mode 100644 index 0000000..7b01391 --- /dev/null +++ b/src/glsapi/classes/api.py @@ -0,0 +1,33 @@ +from hashlib import md5 +from configparser import ConfigParser +from urllib.parse import urlencode + +import json + +from .http import HTTPRequest + + +class GLSAPI: + COUNTRY_CODE = "GB" + LANGUAGE_CODE = "en" + BASE_URL = "https://gls-group.eu/app/service/open/rest/" + + def __init__(self, country_code: str = COUNTRY_CODE, language_code: str = LANGUAGE_CODE, base_url: str = BASE_URL): + self.country_code = country_code.upper() + self.language_code = language_code.lower() + self.base_url = base_url + + def get_request(self, endpoint: str, parameters: dict = {}) -> HTTPRequest: + url = f"{self.base_url}/{self.country_code}/{self.language_code}/{endpoint}{f'?{urlencode(parameters)}' if parameters else ''}" + request = HTTPRequest(url) + return request + + def tracking(self, tracking_number: str): + endpoint = "rstt001" + parameters = { + "match": tracking_number, + } + + request = self.get_request(endpoint, parameters) + response = request.execute() + return response \ No newline at end of file diff --git a/src/pykeydelivery/classes/http.py b/src/glsapi/classes/http.py similarity index 61% rename from src/pykeydelivery/classes/http.py rename to src/glsapi/classes/http.py index d1613df..6fcf8c4 100644 --- a/src/pykeydelivery/classes/http.py +++ b/src/glsapi/classes/http.py @@ -4,7 +4,7 @@ import json class HTTPRequest(Request): - USER_AGENT = "Mozilla/5.0 (compatible; PyKeyDelivery/dev; +https://kumig.it/kumitterer/pykeydelivery)" + USER_AGENT = "Mozilla/5.0 (compatible; GLSAPI/dev; +https://kumig.it/kumitterer/glsapi)" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -15,7 +15,3 @@ class HTTPRequest(Request): if load_json: response = json.loads(response) return response - - def add_json_payload(self, payload: dict): - self.add_header("Content-Type", "application/json") - self.data = json.dumps(payload).encode("utf-8") diff --git a/src/pykeydelivery/classes/__init__.py b/src/pykeydelivery/classes/__init__.py deleted file mode 100644 index ad5576c..0000000 --- a/src/pykeydelivery/classes/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from .http import HTTPRequest -from .keydelivery import KeyDelivery \ No newline at end of file diff --git a/src/pykeydelivery/classes/keydelivery.py b/src/pykeydelivery/classes/keydelivery.py deleted file mode 100644 index 675ac52..0000000 --- a/src/pykeydelivery/classes/keydelivery.py +++ /dev/null @@ -1,61 +0,0 @@ -from hashlib import md5 -from configparser import ConfigParser - -import json - -from .http import HTTPRequest - - -class KeyDelivery: - BASE_URL = "https://www.kd100.com/api/v1/" - - def __init__(self, key: str, secret: str, base_url: str = BASE_URL): - self.key = key - self.secret = secret - self.base_url = base_url - - @classmethod - def from_config(cls, config: ConfigParser | str, section: str = "KeyDelivery") -> "KeyDelivery": - if isinstance(config, str): - temp_config = ConfigParser() - temp_config.read(config) - config = temp_config - - key = config.get(section, "key") - secret = config.get(section, "secret") - base_url = config.get(section, "base_url", fallback=cls.BASE_URL) - - return cls(key, secret, base_url) - - def get_signature(self, message: dict) -> str: - content = json.dumps(message) - data = (content + self.key + self.secret).encode("utf-8") - return md5(data).hexdigest().upper() - - def get_request(self, endpoint: str, message: dict) -> HTTPRequest: - url = self.base_url + endpoint - signature = self.get_signature(message) - - request = HTTPRequest(url) - request.add_json_payload(message) - request.add_header("API-Key", self.key) - request.add_header("signature", signature) - - return request - - def realtime(self, carrier: str, tracking_number: str) -> bytes: - message = { - "carrier_id": carrier, - "tracking_number": tracking_number, - } - - request = self.get_request("tracking/realtime", message) - return request.execute() - - def detect_carrier(self, tracking_number: str) -> bytes: - message = { - "tracking_number": tracking_number, - } - - request = self.get_request("carriers/detect", message) - return request.execute() diff --git a/test.py b/test.py index 280602e..6cf151e 100644 --- a/test.py +++ b/test.py @@ -3,7 +3,7 @@ from configparser import ConfigParser import json -from pykeydelivery import * +from glsapi import * class TestHTTPRequest(TestCase): def test_http_request(self): @@ -11,28 +11,12 @@ class TestHTTPRequest(TestCase): response = http.execute() self.assertEqual(response["headers"]["User-Agent"], http.USER_AGENT) - def test_http_request_with_json_payload(self): - http = HTTPRequest("https://httpbin.org/post") - http.add_json_payload({"foo": "bar"}) - response = http.execute() - self.assertEqual(response["headers"]["User-Agent"], http.USER_AGENT) - self.assertEqual(response["headers"]["Content-Type"], "application/json") - self.assertEqual(response["json"]["foo"], "bar") +class TestGLSAPI(TestCase): + def setUp(self): + self.api = GLSAPI() -class TestKeyDelivery(TestCase): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.config = ConfigParser() - self.config.read("config.ini") - self.keydelivery = KeyDelivery.from_config(self.config) - - def test_detect_carrier(self): - response = self.keydelivery.detect_carrier("483432314669") - self.assertEqual(response["code"], 200) - - def test_realtime(self): - response = self.keydelivery.realtime("gls", "483432314669") - self.assertEqual(response["code"], 200) - -if __name__ == "__main__": - main() \ No newline at end of file + def test_gls_api(self): + tracking_number = "483432314669" + response = self.api.tracking(tracking_number) + unitno = [x for x in response["tuStatus"][0]["references"] if x["type"] == "UNITNO"][0]["value"] + self.assertTrue(tracking_number.startswith(unitno)) \ No newline at end of file