You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:
133
-
134
-
- Serializing back into JSON, `model.to_json()`
135
-
- Converting to a dictionary, `model.to_dict()`
136
-
137
-
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`.
164
+
For non-streaming responses, the library returns a single response object.
138
165
139
166
## Pagination
140
167
@@ -178,51 +205,30 @@ asyncio.run(main())
178
205
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:
179
206
180
207
```python
181
-
first_page =await client.graphs.list()
208
+
first_page =await client.graphs.list()# Remove `await` for non-async usage.
182
209
if first_page.has_next_page():
183
210
print(f"will fetch next page using these details: {first_page.next_page_info()}")
184
211
next_page =await first_page.get_next_page()
185
212
print(f"number of items we just fetched: {len(next_page.data)}")
186
-
187
-
# Remove `await` for non-async usage.
188
213
```
189
214
190
-
Or just work directly with the returned data:
215
+
You can also work directly with the returned data:
191
216
192
217
```python
193
-
first_page =await client.graphs.list()
218
+
first_page =await client.graphs.list()# Remove `await` for non-async usage.
Request parameters that correspond to file uploads can be passed as `bytes`, a [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) instance or a tuple of `(filename, contents, media type)`.
205
-
206
-
```python
207
-
from pathlib import Path
208
-
from writerai import Writer
209
-
210
-
client = Writer()
211
-
212
-
client.files.upload(
213
-
content=Path("/path/to/file"),
214
-
content_disposition="Content-Disposition",
215
-
)
216
223
```
217
224
218
-
The async client uses the exact same interface. If you pass a [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) instance, the file contents will be read asynchronously automatically.
219
-
220
225
## Handling errors
221
226
222
-
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `writerai.APIConnectionError` is raised.
227
+
When the library is unable to connect to the API (for example, due to network connection problems, a timeout, or a firewall that doesn't allow the connection), a subclass of `writerai.APIConnectionError` is raised.
228
+
229
+
> If you are behind a firewall, you may need to configure it to allow connections to the Writer API at `https://api.writer.com/v1`.
223
230
224
-
When the API returns a non-success status code (that is, 4xx or 5xx
225
-
response), a subclass of `writerai.APIStatusError` is raised, containing `status_code` and `response` properties.
231
+
When the API returns a non-success status code - 4xx or 5xx - a subclass of `writerai.APIStatusError` is raised, containing `status_code` and `response` properties.
226
232
227
233
All errors inherit from `writerai.APIError`.
228
234
@@ -268,9 +274,9 @@ Error codes are as follows:
268
274
269
275
### Retries
270
276
271
-
Certain errors are automatically retried 2 times by default, with a short exponential backoff.
272
-
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
273
-
429 Rate Limit, and >=500 Internal errors are all retried by default.
277
+
The library automatically retries certain errors two times by default, with a short exponential backoff.
278
+
Connection errors (for example, due to a network connectivity problem), `408 Request Timeout`, `409 Conflict`,
279
+
`429 Rate Limit`, and `>=500 Internal errors` are all retried by default.
274
280
275
281
You can use the `max_retries` option to configure or disable retry settings:
@@ -330,11 +336,9 @@ On timeout, an `APITimeoutError` is thrown.
330
336
331
337
Note that requests that time out are [retried twice by default](#retries).
332
338
333
-
## Advanced
334
-
335
-
### Logging
339
+
## Logging
336
340
337
-
We use the standard library [`logging`](https://docs.python.org/3/library/logging.html) module.
341
+
We use the standard [`logging`](https://docs.python.org/3/library/logging.html) module.
338
342
339
343
You can enable logging by setting the environment variable `WRITER_LOG` to `info`.
340
344
@@ -344,21 +348,25 @@ $ export WRITER_LOG=info
344
348
345
349
Or to `debug` for more verbose logging.
346
350
351
+
## Advanced
352
+
347
353
### How to tell whether `None` means `null` or missing
348
354
349
355
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`:
350
356
351
357
```py
352
358
if response.my_field isNone:
353
359
if'my_field'notin response.model_fields_set:
354
-
print('Got json like {}, without a "my_field" key present at all.')
360
+
print('Result was {}.')
355
361
else:
356
-
print('Got json like {"my_field": null}.')
362
+
print('Result was{"my_field": null}.')
357
363
```
358
364
359
365
### Accessing raw response data (e.g. headers)
360
366
361
-
The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, e.g.,
367
+
You can access the raw Response object by prefixing `.with_raw_response.` to any HTTP method call.
368
+
369
+
#### Non-streaming responses
362
370
363
371
```py
364
372
from writerai import Writer
@@ -377,15 +385,13 @@ chat = response.parse() # get the object that `chat.chat()` would have returned
377
385
print(chat.id)
378
386
```
379
387
380
-
These methods return an [`APIResponse`](https://github.com/writer/writer-python/tree/main/src/writerai/_response.py) object.
388
+
Calling a method with `.with_raw_response` returns an [`APIResponse`](https://github.com/writer/writer-python/tree/main/src/writerai/_response.py) object.
381
389
382
390
The async client returns an [`AsyncAPIResponse`](https://github.com/writer/writer-python/tree/main/src/writerai/_response.py) with the same structure, the only difference being `await`able methods for reading the response content.
383
391
384
-
#### `.with_streaming_response`
392
+
#### Streaming responses
385
393
386
-
The above interface eagerly reads the full response body when you make the request, which may not always be what you want.
387
-
388
-
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.
394
+
To stream the raw response body, use `.with_streaming_response`, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.
389
395
390
396
```python
391
397
with client.chat.with_streaming_response.chat(
@@ -409,12 +415,12 @@ The context manager is required so that the response will reliably be closed.
409
415
410
416
This library is typed for convenient access to the documented API.
411
417
412
-
If you need to access undocumented endpoints, params, or response properties, the library can still be used.
418
+
If you need to access undocumented endpoints, parameters, or response properties, you can still use the library.
413
419
414
420
#### Undocumented endpoints
415
421
416
-
To make requests to undocumented endpoints, you can make requests using`client.get`, `client.post`, and other
417
-
http verbs. Options on the client will be respected (such as retries) when making this request.
422
+
To make requests to undocumented endpoints, use`client.get`, `client.post`, and other
423
+
http verbs. Options on the client (such as retries) are respected when making these requests.
418
424
419
425
```py
420
426
import httpx
@@ -428,9 +434,9 @@ response = client.post(
428
434
print(response.headers.get("x-foo"))
429
435
```
430
436
431
-
#### Undocumented request params
437
+
#### Undocumented request parameters
432
438
433
-
If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` request
439
+
If you want to explicitly send an extra parameter, you can do so with the `extra_query`, `extra_body`, and `extra_headers` request
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.
478
+
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.
473
479
474
480
```py
475
481
from writerai import Writer
@@ -489,7 +495,7 @@ This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) con
489
495
2. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_
490
496
3. Changes that we do not expect to impact the vast majority of users in practice.
491
497
492
-
We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
498
+
We take backwardscompatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
493
499
494
500
We are keen for your feedback; please open an [issue](https://www.github.com/writer/writer-python/issues) with questions, bugs, or suggestions.
495
501
@@ -504,10 +510,6 @@ import writerai
504
510
print(writerai.__version__)
505
511
```
506
512
507
-
## Requirements
508
-
509
-
Python 3.8 or higher.
510
-
511
-
## Contributing
513
+
## Feedback
512
514
513
-
See [the contributing documentation](./CONTRIBUTING.md).
515
+
We welcome feedback! Please open an [issue](https://www.github.com/writer/writer-python/issues) with questions, bugs, or suggestions.
0 commit comments