Fork GLSAPI for DPDTrack

This commit is contained in:
Kumi 2023-08-30 11:33:55 +02:00
parent 8529f0748c
commit 7acf1ece30
Signed by: kumi
GPG key ID: ECBCC9082395383F
9 changed files with 63 additions and 54 deletions

View file

@ -1,4 +1,4 @@
Copyright (c) 2023 Kumi Mitterer <glsapi@kumi.email>
Copyright (c) 2023 Kumi Mitterer <dpdtrack@kumi.email>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -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

View file

@ -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"
"Homepage" = "https://kumig.it/kumitterer/dpdtrack"
"Bug Tracker" = "https://kumig.it/kumitterer/dpdtrack/issues"

View file

@ -1,2 +1,2 @@
from .http import HTTPRequest
from .api import GLSAPI
from .api import DPD

View file

@ -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

View file

@ -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")

View file

@ -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

21
test.py
View file

@ -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))
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)