34 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			34 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import aiohttp
 | 
						|
 | 
						|
from backend.config import BILLING_URL
 | 
						|
from .schemas import *
 | 
						|
 | 
						|
 | 
						|
class BillingClient:
 | 
						|
    def __init__(self, api_key: str):
 | 
						|
        self.api_key = api_key
 | 
						|
        self.headers = {
 | 
						|
            'Authorization': f'Bearer {self.api_key}'
 | 
						|
        }
 | 
						|
        self.base_url = BILLING_URL
 | 
						|
 | 
						|
    async def _method(self, http_method, method, **kwargs):
 | 
						|
        async with aiohttp.ClientSession(headers=self.headers) as session:
 | 
						|
            async with session.request(http_method, self.base_url + method, **kwargs) as response:
 | 
						|
                return await response.json()
 | 
						|
 | 
						|
    async def create(self, request: CreateBillRequestSchema) -> CreateBillingResponseSchema:
 | 
						|
        json_data = request.model_dump()
 | 
						|
        response = await self._method('POST', '/create', json=json_data)
 | 
						|
        return CreateBillingResponseSchema.model_validate(response)
 | 
						|
 | 
						|
    async def delete(self, request: DeleteBillRequestSchema) -> DeleteBillResponseSchema:
 | 
						|
        method = f'/delete/{request.listener_transaction_id}'
 | 
						|
        response = await self._method('POST', method)
 | 
						|
        return DeleteBillResponseSchema.model_validate(response)
 | 
						|
 | 
						|
    async def notify_received(self, request: NotifyReceivedBillRequestSchema) -> NotifyReceivedBillResponseSchema:
 | 
						|
        json_data = request.model_dump()
 | 
						|
        response = await self._method('POST', '/notify-received', json=json_data)
 | 
						|
        return NotifyReceivedBillResponseSchema.model_validate(response)
 |