diff --git a/LICENSE b/LICENSE index 9f997c0..6df5da1 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 cffaa5e..5ac9d09 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,29 @@ -# GLS REST API Python Client +# myDPD Python Client -This is a Python client for the GLS (https://gls-group.eu) REST API. It allows you to track your shipments. +This is a Python client for the myDPD Austria (https://mydpd.at) tracker. It allows you to track your shipments. -It currently only supports package tracking, not any other API endpoints. +It currently *only* supports DPD Austria. If you want to add support for other countries, feel free to open a pull request. Tracking for DPD shipments in other countries *may* work, but it is not guaranteed. ## Installation ```bash -pip install glsapi +pip install dpdtrack ``` ## Usage ```python -from glsapi import GLSAPI +from dpdtrack import DPD -api = GLSAPI() +api = DPD() # Realtime tracking tracking = api.tracking("YOUR_SHIPMENT_NUMBER") + +# Optionally pass the recipient's postal code to get more accurate results + +tracking = api.tracking("YOUR_SHIPMENT_NUMBER", "RECIPIENT_POSTAL_CODE") ``` ## License diff --git a/pyproject.toml b/pyproject.toml index d1d49b3..be592fe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,12 +3,12 @@ requires = ["hatchling"] build-backend = "hatchling.build" [project] -name = "glsapi" +name = "dpdtrack" version = "0.9.1" authors = [ - { name="Kumi Mitterer", email="glsapi@kumi.email" }, + { name="Kumi Mitterer", email="dpdtrack@kumi.email" }, ] -description = "Simple Python wrapper to fetch data from GLS (gls-group.eu)" +description = "Simple Python wrapper to fetch data from DPD Austria (mydpd.at)" readme = "README.md" license = { file="LICENSE" } requires-python = ">=3.10" @@ -19,5 +19,5 @@ classifiers = [ ] [project.urls] -"Homepage" = "https://kumig.it/kumitterer/glsapi" -"Bug Tracker" = "https://kumig.it/kumitterer/glsapi/issues" \ No newline at end of file +"Homepage" = "https://kumig.it/kumitterer/dpdtrack" +"Bug Tracker" = "https://kumig.it/kumitterer/dpdtrack/issues" \ No newline at end of file diff --git a/src/glsapi/__init__.py b/src/dpdtrack/__init__.py similarity index 100% rename from src/glsapi/__init__.py rename to src/dpdtrack/__init__.py diff --git a/src/glsapi/classes/__init__.py b/src/dpdtrack/classes/__init__.py similarity index 56% rename from src/glsapi/classes/__init__.py rename to src/dpdtrack/classes/__init__.py index 36a9e81..d5681a0 100644 --- a/src/glsapi/classes/__init__.py +++ b/src/dpdtrack/classes/__init__.py @@ -1,2 +1,2 @@ from .http import HTTPRequest -from .api import GLSAPI \ No newline at end of file +from .api import DPD \ No newline at end of file diff --git a/src/dpdtrack/classes/api.py b/src/dpdtrack/classes/api.py new file mode 100644 index 0000000..1c0ba9b --- /dev/null +++ b/src/dpdtrack/classes/api.py @@ -0,0 +1,27 @@ +from hashlib import md5 +from configparser import ConfigParser +from urllib.parse import urlencode + +import json + +from .http import HTTPRequest + +class DPD: + SEARCH = "https://www.mydpd.at/jws.php/parcel/search" + VERIFY = "https://www.mydpd.at/jws.php/parcel/verify" + + def tracking(self, tracking_number: str, postal_code: str = None): + if postal_code is None: + endpoint = self.SEARCH + payload = tracking_number + else: + endpoint = self.VERIFY + payload = [tracking_number, postal_code] + + request = HTTPRequest(endpoint) + request.add_json_payload(payload) + + response = request.execute() + return response + + diff --git a/src/glsapi/classes/http.py b/src/dpdtrack/classes/http.py similarity index 62% rename from src/glsapi/classes/http.py rename to src/dpdtrack/classes/http.py index 6fcf8c4..d97fb0e 100644 --- a/src/glsapi/classes/http.py +++ b/src/dpdtrack/classes/http.py @@ -4,7 +4,7 @@ import json class HTTPRequest(Request): - USER_AGENT = "Mozilla/5.0 (compatible; GLSAPI/dev; +https://kumig.it/kumitterer/glsapi)" + USER_AGENT = "Mozilla/5.0 (compatible; DPDTrack/dev; +https://kumig.it/kumitterer/dpdtrack)" def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -15,3 +15,7 @@ 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") \ No newline at end of file diff --git a/src/glsapi/classes/api.py b/src/glsapi/classes/api.py deleted file mode 100644 index 7b01391..0000000 --- a/src/glsapi/classes/api.py +++ /dev/null @@ -1,33 +0,0 @@ -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/test.py b/test.py index 6cf151e..1b8ce1a 100644 --- a/test.py +++ b/test.py @@ -3,7 +3,7 @@ from configparser import ConfigParser import json -from glsapi import * +from dpdtrack import * class TestHTTPRequest(TestCase): def test_http_request(self): @@ -11,12 +11,19 @@ class TestHTTPRequest(TestCase): response = http.execute() self.assertEqual(response["headers"]["User-Agent"], http.USER_AGENT) -class TestGLSAPI(TestCase): +class TestDPD(TestCase): def setUp(self): - self.api = GLSAPI() + self.api = DPD() - def test_gls_api(self): - tracking_number = "483432314669" + def test_tracking(self): + tracking_number = "01155036780055" 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 + self.assertEqual(response["state"], "success") + self.assertEqual(response["data"][0]["pno"], tracking_number) + + def test_tracking_with_postal_code(self): + tracking_number = "01155036780055" + postal_code = "8010" + response = self.api.tracking(tracking_number, postal_code) + self.assertEqual(response["state"], "success") + self.assertEqual(response["data"][0]["pno"], tracking_number) \ No newline at end of file