Skip to content

Commit

Permalink
Improve error handling—add error codes and HTTP status code to except…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
krasun committed Jun 12, 2024
1 parent c84d6ae commit dac061c
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 198 deletions.
42 changes: 35 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

An official Python SDK for [ScreenshotOne.com API](https://screenshotone.com) to take screenshots of URLs, render HTML as images and PDF.

It takes minutes to start taking screenshots. Just [sign up](https://screenshotone.com/) to get access and secret keys, import the client, and you are ready to go.
It takes minutes to start taking screenshots. Just [sign up](https://screenshotone.com/) to get access and secret keys, import the client, and you are ready to go.

The SDK client is synchronized with the latest [screenshot API options](https://screenshotone.com/docs/options/).

Expand All @@ -14,12 +14,13 @@ pip install screenshotone

## Usage

Generate a screenshot URL without executing the request. Or download the screenshot. It is up to you:
Generate a screenshot URL without executing the request. Or download the screenshot. It is up to you:

```python
import shutil
from screenshotone import Client, TakeOptions

# create API client
# create API client
client = Client('<your access key>', '<your secret key>')

# set up options
Expand All @@ -42,10 +43,37 @@ with open('example.png', 'wb') as result_file:
shutil.copyfileobj(image, result_file)
```

## Release
### How to handle errors

Read about [how to handle the ScreenshotOne API errors](https://screenshotone.com/docs/guides/how-to-handle-api-errors/), and that's how you can get the HTTP status code and the error code of the request:

```python
try:
# ...
# render a screenshot and download the image as stream
image = client.take(options)
# ...
except InvalidRequestException as e:
print(f"Invalid request: {e}")
if e.http_status_code:
print(f"HTTP Status Code: {e.http_status_code}")
if e.error_code:
print(f"Error Code: {e.error_code}")
except APIErrorException as e:
print(f"API Error: {e}")
if e.http_status_code:
print(f"HTTP Status Code: {e.http_status_code}")
if e.error_code:
print(f"Error Code: {e.error_code}")
except Exception as e:
# handle any other exceptions
print(f"An unexpected error occurred: {e}")
```

## Release

[Github Actions](https://github.com/screenshotone/pythonsdk/blob/main/.github/workflows/pypi-release.yml) is used to automate the release process and publishing to PyPI. Update the library version in `pyproject.toml` and [create a new release](https://github.com/screenshotone/pythonsdk/releases/new) to launch the `publish` workflow.
[Github Actions](https://github.com/screenshotone/pythonsdk/blob/main/.github/workflows/pypi-release.yml) is used to automate the release process and publishing to PyPI. Update the library version in `pyproject.toml` and [create a new release](https://github.com/screenshotone/pythonsdk/releases/new) to launch the `publish` workflow.

## License
## License

`screenshotone/pythonsdk` is released under [the MIT license](LICENSE).
`screenshotone/pythonsdk` is released under [the MIT license](LICENSE).
18 changes: 7 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "screenshotone"
version = "0.0.12"
authors = [
{ name="Dmytro Krasun", email="support@screenshotone.com" },
]
dependencies = [
"requests >= 2.28.1"
]
version = "0.0.13"
authors = [{ name = "Dmytro Krasun", email = "support@screenshotone.com" }]
dependencies = ["requests >= 2.28.1"]
description = "A Python SDK for ScreenshotOne.com API to take screenshots of URLs, render HTML as images and PDF"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
[project.urls]
"Homepage" = "https://screenshotone.com/"
"Docs" = "https://screenshotone.com/docs/getting-started/"
"Docs" = "https://screenshotone.com/docs/getting-started/"
Loading

0 comments on commit dac061c

Please sign in to comment.