Fork pykeydelivery for glsapi
This commit is contained in:
parent
aa3eb4dacf
commit
6341ae7c63
13 changed files with 69 additions and 115 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@
|
|||
config.ini
|
||||
__pycache__
|
||||
*.pyc
|
||||
dist/
|
|
@ -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
|
2
LICENSE
2
LICENSE
|
@ -1,4 +1,4 @@
|
|||
Copyright (c) 2023 Kumi Mitterer <pykeydelivery@kumi.email>
|
||||
Copyright (c) 2023 Kumi Mitterer <glsapi@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
|
||||
|
|
16
README.md
16
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
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
[KeyDelivery]
|
||||
key = api_key
|
||||
secret = api_secret
|
|
@ -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"
|
||||
"Homepage" = "https://kumig.it/kumitterer/glsapi"
|
||||
"Bug Tracker" = "https://kumig.it/kumitterer/glsapi/issues"
|
2
src/glsapi/classes/__init__.py
Normal file
2
src/glsapi/classes/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from .http import HTTPRequest
|
||||
from .api import GLSAPI
|
33
src/glsapi/classes/api.py
Normal file
33
src/glsapi/classes/api.py
Normal file
|
@ -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
|
|
@ -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")
|
|
@ -1,2 +0,0 @@
|
|||
from .http import HTTPRequest
|
||||
from .keydelivery import KeyDelivery
|
|
@ -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()
|
34
test.py
34
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()
|
||||
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))
|
Loading…
Reference in a new issue