Skip to main content

Simple Python client for ShopCloud Proxy API - drop-in replacement for requests

Project description

ShopCloud Proxy Client

PyPI version Python Support Tests

Ein einfacher Python-Client für die ShopCloud Proxy API. Funktioniert als Drop-in-Replacement für requests - ohne komplexe Änderungen an Ihrem bestehenden Code!

Features

  • Einfach zu verwenden: Minimale Änderungen an bestehendem Code nötig
  • 🔄 Kompatibel mit requests: Gleiche API wie requests.Session
  • 🔐 Automatische Authentifizierung: Token-Verwaltung erfolgt automatisch
  • 🔁 Automatisches Retry: Bei transienten Fehlern (502, 503, 504) wird automatisch wiederholt
  • Proaktiver Token-Refresh: Token wird erneuert bevor er abläuft
  • 🎯 Bessere Error Messages: Spezifische Exceptions für verschiedene Fehlertypen
  • 🚀 Keine komplexen Änderungen: Einfach Session erstellen und loslegen

Installation

# Aus lokalem Verzeichnis installieren
cd client
pip install -e .

# Oder mit pip (wenn auf PyPI veröffentlicht)
pip install shopcloud-proxy-client

Schnellstart

Variante 1: ProxySession (empfohlen)

from shopcloud_proxy_client import ProxySession

# Session erstellen (automatisches Login)
session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

# Einfach verwenden wie requests!
response = session.get("https://api.github.com/users/octocat")
print(response.json())
print(f"Status: {response.status_code}")

# POST mit JSON
response = session.post(
    "https://httpbin.org/post",
    json={"name": "Test", "value": 123}
)
print(response.json())

# Mit Custom Headers
response = session.get(
    "https://api.example.com/data",
    headers={"X-API-Key": "secret123"}
)

Variante 2: Globale Konfiguration

import shopcloud_proxy_client as proxy

# Einmalig konfigurieren
proxy.configure(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

# Dann einfach verwenden
response = proxy.get("https://api.github.com/users/octocat")
print(response.json())

response = proxy.post("https://httpbin.org/post", json={"key": "value"})

Variante 3: Context Manager

from shopcloud_proxy_client import ProxySession

with ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
) as session:
    response = session.get("https://api.github.com/zen")
    print(response.text)
# Session wird automatisch geschlossen

Bestehenden Code migrieren

Vorher (mit requests):

import requests

response = requests.get("https://api.github.com/users/octocat")
print(response.json())

response = requests.post(
    "https://api.example.com/data",
    json={"name": "test"},
    headers={"X-API-Key": "secret"}
)

Nachher (mit Proxy):

from shopcloud_proxy_client import ProxySession

session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

# Rest des Codes bleibt GLEICH!
response = session.get("https://api.github.com/users/octocat")
print(response.json())

response = session.post(
    "https://api.example.com/data",
    json={"name": "test"},
    headers={"X-API-Key": "secret"}
)

API-Dokumentation

ProxySession

ProxySession(
    proxy_url: str,                        # URL der Proxy API
    username: str,                         # Ihr Username
    password: str,                         # Ihr Passwort
    auto_login: bool = True,               # Automatisch beim Start einloggen
    default_timeout: int = 30,             # Standard-Timeout in Sekunden
    default_headers: Dict[str, str] = None,# Standard-Headers für alle Requests
    max_retries: int = 3,                  # Anzahl der Wiederholungen bei Fehlern
    retry_delay: float = 1.0               # Verzögerung zwischen Retries in Sekunden
)

Methoden:

  • get(url, **kwargs) - GET Request
  • post(url, **kwargs) - POST Request
  • put(url, **kwargs) - PUT Request
  • patch(url, **kwargs) - PATCH Request
  • delete(url, **kwargs) - DELETE Request
  • options(url, **kwargs) - OPTIONS Request
  • head(url, **kwargs) - HEAD Request

Unterstützte kwargs:

  • headers: Dict mit Custom Headers
  • json: JSON Body (wird automatisch serialisiert)
  • data: Raw Body Data
  • timeout: Timeout in Sekunden (überschreibt Standard)

ProxyResponse

Das Response-Objekt ist kompatibel mit requests.Response:

response = session.get("https://api.example.com")

# Attribute
response.status_code  # HTTP Status Code (int)
response.headers      # Response Headers (dict)
response.ok           # True wenn 200-299 (bool)
response.url          # Original URL (str)
response.text         # Body als String (str)
response.content      # Body als Bytes (bytes)

# Methoden
response.json()              # Parse JSON
response.raise_for_status()  # Raise Exception bei Fehler

Erweiterte Beispiele

Mit Error Handling

from shopcloud_proxy_client import ProxySession
import requests

session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

try:
    response = session.get("https://api.example.dev/data")
    response.raise_for_status()  # Raise bei 4xx/5xx
    data = response.json()
    print(f"Success: {data}")
except requests.HTTPError as e:
    print(f"HTTP Error: {e}")
except Exception as e:
    print(f"Error: {e}")

Mehrere Requests mit einer Session

from shopcloud_proxy_client import ProxySession

session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

# Token wird automatisch wiederverwendet
users = ["octocat", "torvalds", "gvanrossum"]
for user in users:
    response = session.get(f"https://api.github.com/users/{user}")
    data = response.json()
    print(f"{user}: {data.get('name')}")

Custom Timeout

session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort",
    default_timeout=60  # 60 Sekunden Standard
)

# Oder per Request
response = session.get("https://slow-api.example.com", timeout=120)

Default Headers

# Session mit Standard-Headers erstellen
session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort",
    default_headers={
        "User-Agent": "MyApp/1.0",
        "Accept": "application/json",
        "X-Client-ID": "my-client"
    }
)

# Diese Headers werden automatisch bei jedem Request mitgesendet
response = session.get("https://api.example.com/data")

# Request-spezifische Headers überschreiben Default-Headers
response = session.get(
    "https://api.example.com/data",
    headers={
        "User-Agent": "SpecialAgent/2.0",  # Überschreibt Default
        "X-Request-ID": "123"               # Zusätzlicher Header
    }
)
# Resultierende Headers: User-Agent=SpecialAgent/2.0, Accept=application/json,
#                        X-Client-ID=my-client, X-Request-ID=123

Automatisches Retry & Error Handling

from shopcloud_proxy_client import (
    ProxySession,
    ProxyRateLimitError,
    ProxyTimeoutError,
    ProxyAuthenticationError
)

# Session mit Custom Retry-Einstellungen
session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort",
    max_retries=5,        # 5 Versuche statt 3
    retry_delay=0.5       # 0.5 Sekunden zwischen Versuchen
)

# Automatisches Retry bei 502, 503, 504 Fehlern
# Mit exponentiellem Backoff: 0.5s, 1.0s, 1.5s, 2.0s, 2.5s
try:
    response = session.get("https://unreliable-api.example.com")
except ProxyRateLimitError:
    print("Rate Limit erreicht")
except ProxyTimeoutError:
    print("Request hat zu lange gedauert")
except ProxyAuthenticationError:
    print("Authentifizierung fehlgeschlagen")

Proaktiver Token-Refresh

# Token wird automatisch 30 Sekunden vor Ablauf erneuert
# Keine manuellen Eingriffe nötig!

session = ProxySession(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

# Token-Ablauf wird aus JWT extrahiert und überwacht
# Bei jedem Request wird geprüft, ob Token bald abläuft
# Falls ja: automatischer Refresh VOR dem Request
for i in range(1000):
    response = session.get("https://api.example.com/data")
    # Token wird automatisch erneuert wenn nötig

Unterschiede zu requests

Der Client ist weitgehend kompatibel mit requests, aber es gibt einige Einschränkungen:

Unterstützt:

  • ✅ HTTP Methoden (GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD)
  • ✅ Custom Headers
  • ✅ JSON und String Bodies
  • ✅ Timeout
  • ✅ Response Parsing (json(), text, content)
  • ✅ Error Handling (raise_for_status)

Nicht unterstützt:

  • ❌ Datei-Uploads (files parameter)
  • ❌ Streaming Responses
  • ❌ Session Cookies
  • ❌ SSL Verification Parameter
  • ❌ Proxies Parameter (wird ja schon geproxyt!)

Entwicklung

Tests ausführen

cd client
pip install -e ".[dev]"
pytest tests/

Code-Qualität prüfen

ruff check .

Troubleshooting

"Proxy client not configured"

Wenn Sie die globalen Funktionen (proxy.get()) verwenden, müssen Sie zuerst configure() aufrufen:

import shopcloud_proxy_client as proxy

proxy.configure(
    proxy_url="https://test-proxy.example.dev",
    username="ihr-username",
    password="ihr-passwort"
)

response = proxy.get("https://api.example.com")

Oder verwenden Sie direkt ProxySession.

Token abgelaufen

Der Client erneuert automatisch abgelaufene Tokens. Wenn Sie einen 401-Fehler sehen, überprüfen Sie Ihre Credentials.

Timeouts

Standardmäßig ist der Timeout 30 Sekunden. Für langsame APIs erhöhen Sie ihn:

response = session.get("https://slow-api.example.com", timeout=120)

Lizenz

MIT

Support

Bei Fragen oder Problemen öffnen Sie bitte ein Issue auf GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shopcloud_proxy_client-0.15.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shopcloud_proxy_client-0.15.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file shopcloud_proxy_client-0.15.0.tar.gz.

File metadata

  • Download URL: shopcloud_proxy_client-0.15.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shopcloud_proxy_client-0.15.0.tar.gz
Algorithm Hash digest
SHA256 7e6823e8cd48928a30f1a23225f640911c743bbfb7db6e8e308b61380fa6598c
MD5 802e50d579562be97b126abc9e7a7234
BLAKE2b-256 1372004baa6a646a092765921aa5bb3d8a60829e143e34bcca5f2bb79b5a2d1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for shopcloud_proxy_client-0.15.0.tar.gz:

Publisher: cd.yml on Talk-Point/shopcloud-proxy-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shopcloud_proxy_client-0.15.0-py3-none-any.whl.

File metadata

File hashes

Hashes for shopcloud_proxy_client-0.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 687227889b3810834661bd523ac433d6e5d8b11b629c971a07a96f260214db00
MD5 d2f6e55336c44cab3b3dc10915fc9fd6
BLAKE2b-256 c3aaf4ff300ca0ace93fe6c5321f76357f5c2a38fee456a719c878d2686c80a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for shopcloud_proxy_client-0.15.0-py3-none-any.whl:

Publisher: cd.yml on Talk-Point/shopcloud-proxy-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page