11import os
2- from typing import Any , Dict , MutableMapping , Optional , Union
2+ from typing import Any , MutableMapping
33from urllib .parse import urljoin
44
5- import requests
6- from requests import Response
5+ import httpx
6+ from httpx import Response
77
88from ..types .exc import FacturapiResponseException
99from ..version import CLIENT_VERSION
@@ -21,18 +21,18 @@ class Client:
2121
2222 Attributes:
2323 host (str): Base URL to perform requests.
24- session (requests.Session ): The requests session used
24+ client (httpx.Client ): The httpx client used
2525 to perform requests.
2626 api_key (str): API KEY for Facturapi
2727
2828 """
2929
3030 host : str = API_HOST
31- session : requests . Session
31+ client : httpx . Client
3232
33- def __init__ (self ):
34- self .session = requests . Session ()
35- self .session .headers .update (
33+ def __init__ (self ) -> None :
34+ self .client = httpx . Client ()
35+ self .client .headers .update (
3636 {
3737 'User-Agent' : f'facturapi-python/{ CLIENT_VERSION } ' ,
3838 'Content-Type' : 'application/json' ,
@@ -41,9 +41,9 @@ def __init__(self):
4141
4242 # Auth
4343 self .api_key = os .getenv ('FACTURAPI_KEY' , '' )
44- self .session .auth = (self .api_key , '' )
44+ self .client .auth = httpx . BasicAuth (self .api_key , '' )
4545
46- def configure (self , api_key : str ):
46+ def configure (self , api_key : str ) -> None :
4747 """Configure the http client.
4848
4949 Import the client and configure it passing the `API_KEY`
@@ -54,36 +54,36 @@ def configure(self, api_key: str):
5454
5555 """
5656 self .api_key = api_key
57- self .session .auth = (self .api_key , '' )
57+ self .client .auth = httpx . BasicAuth (self .api_key , '' )
5858
5959 def get (
6060 self ,
6161 endpoint : str ,
62- params : Union [ None , bytes , MutableMapping [str , str ]] = None ,
63- ) -> Dict [str , Any ]:
62+ params : bytes | MutableMapping [str , str ] | None = None ,
63+ ) -> dict [str , Any ]:
6464 """Performs GET request to Facturapi."""
6565 return self .request ('get' , endpoint , params = params )
6666
67- def post (self , endpoint : str , data : Dict [str , Any ]) -> Dict [str , Any ]:
67+ def post (self , endpoint : str , data : dict [str , Any ]) -> dict [str , Any ]:
6868 """Performs POST request to Facturapi."""
6969 return self .request ('post' , endpoint , data = data )
7070
71- def put (self , endpoint : str , data : Dict [str , Any ]) -> Dict [str , Any ]:
71+ def put (self , endpoint : str , data : dict [str , Any ]) -> dict [str , Any ]:
7272 """Performs PUT request to Facturapi."""
7373 return self .request ('put' , endpoint , data = data )
7474
75- def delete (self , endpoint : str ) -> Dict [str , Any ]:
75+ def delete (self , endpoint : str ) -> dict [str , Any ]:
7676 """Performs DELETE request to Facturapi."""
7777 return self .request ('delete' , endpoint )
7878
7979 def request (
8080 self ,
8181 method : str ,
8282 endpoint : str ,
83- params : Union [ None , bytes , MutableMapping [str , str ]] = None ,
84- data : Optional [ Dict [ str , Union [ int , str ]]] = None ,
83+ params : bytes | MutableMapping [str , str ] | None = None ,
84+ data : dict [ str , int | str ] | None = None ,
8585 ** kwargs ,
86- ) -> Dict [str , Any ]:
86+ ) -> dict [str , Any ]:
8787 """Performs a request to Facturapi.
8888
8989 Given a `method` and `endpoint`, perform a request to
@@ -97,14 +97,14 @@ def request(
9797 **kwargs: Arbitrary keyword arguments.
9898
9999 Returns:
100- Dict [str, Any]: JSON of the request's response.
100+ dict [str, Any]: JSON of the request's response.
101101
102102 Raises:
103103 FacturapiResponseException: If response is not
104104 successful.
105105
106106 """
107- response = self .session .request (
107+ response = self .client .request (
108108 method = method ,
109109 url = ('https://' + self .host + urljoin ('/' , endpoint )),
110110 json = data ,
@@ -133,7 +133,7 @@ def download_request(
133133 successful.
134134
135135 """
136- response = self .session .request (
136+ response = self .client .request (
137137 method = 'GET' ,
138138 url = ('https://' + self .host + urljoin ('/' , endpoint )),
139139 ** kwargs ,
@@ -142,8 +142,8 @@ def download_request(
142142 return response .content
143143
144144 @staticmethod
145- def _check_response (response : Response ):
146- if not response .ok :
145+ def _check_response (response : Response ) -> None :
146+ if not response .is_success :
147147 raise FacturapiResponseException (
148148 json = response .json (),
149149 status_code = response .status_code ,
0 commit comments