Skip to main content

Python SDK for Report Service.

Project description

Khởi tạo client - install SDK

from exn_salon_report_sdk import ReportClient, ReportSDKException

api_key = "your_api_key"
secret_key = "your_secret_key"
client = ReportClient(api_key, secret_key)

# Gọi API để lấy báo cáo
try:
    report_id = "12345"
    report = client.get_report(report_id)
    print("Report:", report)

    # Tạo mới một báo cáo
    new_report_data = {
        "name": "Sales Report",
        "filters": {"date_range": "2025-01-01 to 2025-01-20"},
    }
    new_report = client.create_report(new_report_data)
    print("New Report Created:", new_report)
    
except ReportSDKException as e:
    print("SDK Error:", str(e))

Xử lý phía Service Report

1. Lấy API key từ header.

api_key = request.headers.get("API-Key")
if not api_key:
    return {"error": "API key is required"}, 401

secret_key = get_secret_key_from_db(api_key)
if not secret_key:
    return {"error": "Invalid API key"}, 401

2. Xác minh chữ ký

import hmac
import hashlib
import base64

def verify_signature(secret_key, signature_payload, signature):
    expected_signature = hmac.new(
        secret_key.encode(), 
        signature_payload.encode(), 
        hashlib.sha256
    ).digest()
    expected_signature = base64.b64encode(expected_signature).decode()
    return hmac.compare_digest(expected_signature, signature)

# Xử lý chữ ký
signature_payload = f"{api_key}{timestamp}{request_payload}"
if not verify_signature(secret_key, signature_payload, received_signature):
    return {"error": "Invalid signature"}, 401

3. Kiểm tra timestamp (chống replay attack):

from datetime import datetime, timedelta

def is_timestamp_valid(request_timestamp, max_skew_minutes=5):
    try:
        timestamp = datetime.fromisoformat(request_timestamp)
        now = datetime.utcnow()
        return abs((now - timestamp).total_seconds()) <= max_skew_minutes * 60
    except ValueError:
        return False

if not is_timestamp_valid(timestamp):
    return {"error": "Invalid or expired timestamp"}, 401

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

exn_report_sdk-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

exn_report_sdk-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file exn_report_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: exn_report_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for exn_report_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0b288a20e7d3ca772c324c7e056c75a90f3740e4c4a48bf0195f2f766035268
MD5 b1b013c5dc1383e29b3f30579d302470
BLAKE2b-256 9275306e2b72d04aac26ff8897d38e828b7add5e524a1f93241b66ae1fb837f8

See more details on using hashes here.

File details

Details for the file exn_report_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: exn_report_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for exn_report_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29fa60f528b87eb3dcfa2e70cf4f2416a82323055ca83d4abec8f55133bbba84
MD5 b28608c773da622294774c6d2919d3c1
BLAKE2b-256 3bd8e4e86b7c8778942bffdc09dd4be46af1e7c43cf39bfaf7d74d27bf3e7d27

See more details on using hashes here.

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