gfwapiclient.http.client

Global Fishing Watch (GFW) API Python Client - HTTP Client.

Module Contents

Classes

HTTPClient

Asynchronous HTTP client for interacting with the Global Fishing Watch (GFW) API.

Data

API

gfwapiclient.http.client.__all__ = ['HTTPClient']
class gfwapiclient.http.client.HTTPClient(*, base_url: Optional[Union[str, httpx.URL]] = None, access_token: Optional[str] = None, follow_redirects: Optional[bool] = True, timeout: Optional[float] = 60.0, connect_timeout: Optional[float] = 5.0, max_connections: Optional[int] = 100, max_keepalive_connections: Optional[int] = 20, max_redirects: Optional[int] = 2, **kwargs: Any)

Bases: httpx.AsyncClient

Asynchronous HTTP client for interacting with the Global Fishing Watch (GFW) API.

This client extends httpx.AsyncClient and provides: - Connection pooling with configurable limits. - HTTP/2 support for better performance. - Automatic redirects with max limits. - Fine-grained control over timeouts.

Example:

```python async with HTTPClient() as client:

response = await client.get(“/vessels”) data = response.json()

```

Initialization

Initializes a new HTTPClient with specified configurations.

Args:
base_url (Optional[Union[str, httpx.URL]], default=None):

The base URL for API requests. If not provided, the value is taken from the GFW_API_BASE_URL environment variable. Raises BaseUrlError if neither is set.

access_token (Optional[str], default=None):

The access token for API request authentication. If not provided, the value is taken from the GFW_API_ACCESS_TOKEN environment variable. Raises AccessTokenError if neither is set.

follow_redirects (Optional[bool], default=True):

Whether the client should automatically follow redirects. Defaults to True.

timeout (Optional[float], default=60.0):

The default timeout (in seconds) for all operations (connect, read, pool, etc.). Defaults to 60.0 seconds.

connect_timeout (Optional[float], default=5.0):

Timeout (in seconds) for establishing a connection. Defaults to 5.0 seconds.

max_connections (Optional[int], default=100):

Maximum number of concurrent connections. Defaults to 100.

max_keepalive_connections (Optional[int], default=20):

Maximum number of keep-alive connections in the connection pool. Should not exceed max_connections. Defaults to 20.

max_redirects (Optional[int], default=2):

Maximum number of redirects to follow before raising an error. Defaults to 2.

**kwargs (Any):

Additional parameters passed to httpx.AsyncClient.

Raises:
BaseUrlError:

If base_url is not provided and the GFW_API_BASE_URL environment variable is also not set.

AccessTokenError:

If access_token is not provided and the GFW_API_ACCESS_TOKEN environment variable is also not set.

property user_agent: str

Returns the User-Agent header for the HTTP client.

Returns:
str:

The User-Agent string.

property auth_headers: Dict[str, str]

Returns authentication headers for API requests.

Returns:
Dict[str, str]:

Headers containing the API access token.

property default_headers: Dict[str, str]

Returns the default headers for all API requests.

Returns:
Dict[str, str]:

Default request headers, including authentication.

async __aenter__() Self

Enter the async context and return the client instance.

Returns:
HTTPClient:

The HTTP client instance.

async __aexit__(exc_type: Optional[Type[BaseException]] = None, exc_value: Optional[BaseException] = None, traceback: Optional[types.TracebackType] = None) None

Exit the async context, ensuring the client session is properly closed.

Args:
exc_type (Optional[Type[BaseException]]):

Exception type if an error occurs.

exc_value (Optional[BaseException]):

Exception value if an error occurs.

traceback (Optional[TracebackType]):

Traceback details if an error occurs.