Skip to content

Commit 623f5bc

Browse files
feat(client): add support for passing in a httpx client (#123)
1 parent 92bb83c commit 623f5bc

File tree

5 files changed

+699
-91
lines changed

5 files changed

+699
-91
lines changed

README.md

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Finch Python API Library
1+
# Finch Python API library
22

33
[![PyPI version](https://img.shields.io/pypi/v/finch-api.svg)](https://pypi.org/project/finch-api/)
44

55
The Finch Python library provides convenient access to the Finch REST API from any Python 3.7+
6-
application. It includes type definitions for all request params and response fields,
6+
application. The library includes type definitions for all request params and response fields,
77
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
88

99
## Documentation
1010

11-
The API documentation can be found [here](https://developer.tryfinch.com/).
11+
The API documentation can be found at [https://developer.tryfinch.com/](https://developer.tryfinch.com/).
1212

1313
## Installation
1414

@@ -34,7 +34,7 @@ directory = page.individuals[0]
3434
print(directory.first_name)
3535
```
3636

37-
## Async Usage
37+
## Async usage
3838

3939
Simply import `AsyncFinch` instead of `Finch` and use `await` with each API call:
4040

@@ -58,11 +58,11 @@ asyncio.run(main())
5858

5959
Functionality between the synchronous and asynchronous clients is otherwise identical.
6060

61-
## Using Types
61+
## Using types
6262

63-
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev), which provide helper methods for things like serializing back into json ([v1](https://docs.pydantic.dev/1.10/usage/models/), [v2](https://docs.pydantic.dev/latest/usage/serialization/)). To get a dictionary, you can call `dict(model)`.
63+
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev), which provide helper methods for things like serializing back into JSON ([v1](https://docs.pydantic.dev/1.10/usage/models/), [v2](https://docs.pydantic.dev/latest/usage/serialization/)). To get a dictionary, call `dict(model)`.
6464

65-
This helps provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `"basic"`.
65+
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
6666

6767
## Pagination
6868

@@ -169,10 +169,10 @@ async def handler(request: Request):
169169

170170
## Handling errors
171171

172-
When the library is unable to connect to the API (e.g., due to network connection problems or a timeout), a subclass of `finch.APIConnectionError` is raised.
172+
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `finch.APIConnectionError` is raised.
173173

174-
When the API returns a non-success status code (i.e., 4xx or 5xx
175-
response), a subclass of `finch.APIStatusError` will be raised, containing `status_code` and `response` properties.
174+
When the API returns a non-success status code (that is, 4xx or 5xx
175+
response), a subclass of `finch.APIStatusError` is raised, containing `status_code` and `response` properties.
176176

177177
All errors inherit from `finch.APIError`.
178178

@@ -210,11 +210,11 @@ Error codes are as followed:
210210

211211
### Retries
212212

213-
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
213+
Certain errors are automatically retried 2 times by default, with a short exponential backoff.
214214
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
215-
429 Rate Limit, and >=500 Internal errors will all be retried by default.
215+
429 Rate Limit, and >=500 Internal errors are all retried by default.
216216

217-
You can use the `max_retries` option to configure or disable this:
217+
You can use the `max_retries` option to configure or disable retry settings:
218218

219219
```python
220220
from finch import Finch
@@ -231,8 +231,8 @@ client.with_options(max_retries=5).hris.directory.list_individuals()
231231

232232
### Timeouts
233233

234-
Requests time out after 1 minute by default. You can configure this with a `timeout` option,
235-
which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/#fine-tuning-the-configuration):
234+
By default requests time out after 1 minute. You can configure this with a `timeout` option,
235+
which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/#fine-tuning-the-configuration) object:
236236

237237
```python
238238
from finch import Finch
@@ -254,7 +254,7 @@ client.with_options(timeout=5 * 1000).hris.directory.list_individuals()
254254

255255
On timeout, an `APITimeoutError` is thrown.
256256

257-
Note that requests which time out will be [retried twice by default](#retries).
257+
Note that requests that time out are [retried twice by default](#retries).
258258

259259
## Default Headers
260260

@@ -276,7 +276,7 @@ client = Finch(
276276

277277
### How to tell whether `None` means `null` or missing
278278

279-
In an API response, a field may be explicitly null, or missing entirely; in either case, its value is `None` in this library. You can differentiate the two cases with `.model_fields_set`:
279+
In an API response, a field may be explicitly `null`, or missing entirely; in either case, its value is `None` in this library. You can differentiate the two cases with `.model_fields_set`:
280280

281281
```py
282282
if response.my_field is None:
@@ -286,27 +286,30 @@ if response.my_field is None:
286286
print('Got json like {"my_field": null}.')
287287
```
288288

289-
### Configuring custom URLs, proxies, and transports
289+
### Configuring the HTTP client
290290

291-
You can configure the following keyword arguments when instantiating the client:
291+
You can directly override the [httpx client](https://www.python-httpx.org/api/#client) to customize it for your use case, including:
292+
293+
- Support for proxies
294+
- Custom transports
295+
- Additional [advanced](https://www.python-httpx.org/advanced/#client-instances) functionality
292296

293297
```python
294298
import httpx
295299
from finch import Finch
296300

297301
client = Finch(
298-
# Use a custom base URL
299302
base_url="http://my.test.server.example.com:8083",
300-
proxies="http://my.test.proxy.example.com",
301-
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
303+
http_client=httpx.Client(
304+
proxies="http://my.test.proxy.example.com",
305+
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
306+
),
302307
)
303308
```
304309

305-
See the httpx documentation for information about the [`proxies`](https://www.python-httpx.org/advanced/#http-proxying) and [`transport`](https://www.python-httpx.org/advanced/#custom-transports) keyword arguments.
306-
307310
### Managing HTTP resources
308311

309-
By default we will close the underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__) is called but you can also manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting.
312+
By default the library closes underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__). You can manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting.
310313

311314
## Versioning
312315

0 commit comments

Comments
 (0)