59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""
|
|
Module: lexoffice.py
|
|
Description: Provides an interface to interact with the Lexoffice API.
|
|
This module enables operations such as retrieving invoice details from Lexoffice,
|
|
making it easier to integrate Lexoffice API functionality into a Flask application.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import yaml
|
|
import requests
|
|
import logging
|
|
|
|
|
|
class LexofficeAPI:
|
|
"""Class to handle interactions with the Lexoffice API."""
|
|
|
|
def __init__(self):
|
|
"""Initialize with the Lexoffice API access token from a config file."""
|
|
self.base_url = "https://api.lexoffice.io/v1"
|
|
self.headers = {
|
|
"Authorization": f"Bearer {self._load_api_token()}",
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
}
|
|
|
|
@staticmethod
|
|
def _load_api_token() -> str:
|
|
"""Load the API token from the config.yaml file."""
|
|
config_path = Path(__file__).parent.parent / 'config.yaml'
|
|
with config_path.open('r') as file:
|
|
config = yaml.safe_load(file)
|
|
return config['lexoffice']['api_token']
|
|
|
|
def get_invoice_total_gross_amount(self, invoice_id: str) -> float:
|
|
"""
|
|
Retrieve the total gross amount for a specific invoice.
|
|
|
|
Args:
|
|
invoice_id (str): Unique identifier of the invoice.
|
|
|
|
Returns:
|
|
float or None: Total gross amount of the invoice, or None if an error occurs.
|
|
"""
|
|
url = f"{self.base_url}/invoices/{invoice_id}"
|
|
|
|
try:
|
|
response = requests.get(url, headers=self.headers)
|
|
response.raise_for_status()
|
|
return response.json().get('totalPrice', {}).get('totalGrossAmount')
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
logging.error(f"Error fetching invoice: {e}")
|
|
return None
|
|
|
|
# Usage example:
|
|
# lexoffice_api = LexofficeAPI()
|
|
# total_gross_amount = lexoffice_api.get_invoice_total_gross_amount('your_invoice_id')
|
|
# print(total_gross_amount)
|