| 1 |
# Contributing |
| 2 |
|
| 3 |
Thank you for being interested in contributing to HTTPX. |
| 4 |
There are many ways you can contribute to the project: |
| 5 |
|
| 6 |
- Try HTTPX and [report bugs/issues you find](https://github.com/encode/httpx/issues/new) |
| 7 |
- [Implement new features](https://github.com/encode/httpx/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) |
| 8 |
- [Review Pull Requests of others](https://github.com/encode/httpx/pulls) |
| 9 |
- Write documentation |
| 10 |
- Participate in discussions |
| 11 |
|
| 12 |
## Reporting Bugs or Other Issues |
| 13 |
|
| 14 |
Found something that HTTPX should support? |
| 15 |
Stumbled upon some unexpected behaviour? |
| 16 |
|
| 17 |
Contributions should generally start out with [a discussion](https://github.com/encode/httpx/discussions). |
| 18 |
Possible bugs may be raised as a "Potential Issue" discussion, feature requests may |
| 19 |
be raised as an "Ideas" discussion. We can then determine if the discussion needs |
| 20 |
to be escalated into an "Issue" or not, or if we'd consider a pull request. |
| 21 |
|
| 22 |
Try to be more descriptive as you can and in case of a bug report, |
| 23 |
provide as much information as possible like: |
| 24 |
|
| 25 |
- OS platform |
| 26 |
- Python version |
| 27 |
- Installed dependencies and versions (`python -m pip freeze`) |
| 28 |
- Code snippet |
| 29 |
- Error traceback |
| 30 |
|
| 31 |
You should always try to reduce any examples to the *simplest possible case* |
| 32 |
that demonstrates the issue. |
| 33 |
|
| 34 |
Some possibly useful tips for narrowing down potential issues... |
| 35 |
|
| 36 |
- Does the issue exist on HTTP/1.1, or HTTP/2, or both? |
| 37 |
- Does the issue exist with `Client`, `AsyncClient`, or both? |
| 38 |
- When using `AsyncClient` does the issue exist when using `asyncio` or `trio`, or both? |
| 39 |
|
| 40 |
## Development |
| 41 |
|
| 42 |
To start developing HTTPX create a **fork** of the |
| 43 |
[HTTPX repository](https://github.com/encode/httpx) on GitHub. |
| 44 |
|
| 45 |
Then clone your fork with the following command replacing `YOUR-USERNAME` with |
| 46 |
your GitHub username: |
| 47 |
|
| 48 |
```shell |
| 49 |
$ git clone https://github.com/YOUR-USERNAME/httpx |
| 50 |
``` |
| 51 |
|
| 52 |
You can now install the project and its dependencies using: |
| 53 |
|
| 54 |
```shell |
| 55 |
$ cd httpx |
| 56 |
$ scripts/install |
| 57 |
``` |
| 58 |
|
| 59 |
## Testing and Linting |
| 60 |
|
| 61 |
We use custom shell scripts to automate testing, linting, |
| 62 |
and documentation building workflow. |
| 63 |
|
| 64 |
To run the tests, use: |
| 65 |
|
| 66 |
```shell |
| 67 |
$ scripts/test |
| 68 |
``` |
| 69 |
|
| 70 |
!!! warning |
| 71 |
The test suite spawns testing servers on ports **8000** and **8001**. |
| 72 |
Make sure these are not in use, so the tests can run properly. |
| 73 |
|
| 74 |
You can run a single test script like this: |
| 75 |
|
| 76 |
```shell |
| 77 |
$ scripts/test -- tests/test_multipart.py |
| 78 |
``` |
| 79 |
|
| 80 |
To run the code auto-formatting: |
| 81 |
|
| 82 |
```shell |
| 83 |
$ scripts/lint |
| 84 |
``` |
| 85 |
|
| 86 |
Lastly, to run code checks separately (they are also run as part of `scripts/test`), run: |
| 87 |
|
| 88 |
```shell |
| 89 |
$ scripts/check |
| 90 |
``` |
| 91 |
|
| 92 |
## Documenting |
| 93 |
|
| 94 |
Documentation pages are located under the `docs/` folder. |
| 95 |
|
| 96 |
To run the documentation site locally (useful for previewing changes), use: |
| 97 |
|
| 98 |
```shell |
| 99 |
$ scripts/docs |
| 100 |
``` |
| 101 |
|
| 102 |
## Resolving Build / CI Failures |
| 103 |
|
| 104 |
Once you've submitted your pull request, the test suite will automatically run, and the results will show up in GitHub. |
| 105 |
If the test suite fails, you'll want to click through to the "Details" link, and try to identify why the test suite failed. |
| 106 |
|
| 107 |
<p align="center" style="margin: 0 0 10px"> |
| 108 |
<img src="https://raw.githubusercontent.com/encode/httpx/master/docs/img/gh-actions-fail.png" alt='Failing PR commit status'> |
| 109 |
</p> |
| 110 |
|
| 111 |
Here are some common ways the test suite can fail: |
| 112 |
|
| 113 |
### Check Job Failed |
| 114 |
|
| 115 |
<p align="center" style="margin: 0 0 10px"> |
| 116 |
<img src="https://raw.githubusercontent.com/encode/httpx/master/docs/img/gh-actions-fail-check.png" alt='Failing GitHub action lint job'> |
| 117 |
</p> |
| 118 |
|
| 119 |
This job failing means there is either a code formatting issue or type-annotation issue. |
| 120 |
You can look at the job output to figure out why it's failed or within a shell run: |
| 121 |
|
| 122 |
```shell |
| 123 |
$ scripts/check |
| 124 |
``` |
| 125 |
|
| 126 |
It may be worth it to run `$ scripts/lint` to attempt auto-formatting the code |
| 127 |
and if that job succeeds commit the changes. |
| 128 |
|
| 129 |
### Docs Job Failed |
| 130 |
|
| 131 |
This job failing means the documentation failed to build. This can happen for |
| 132 |
a variety of reasons like invalid markdown or missing configuration within `mkdocs.yml`. |
| 133 |
|
| 134 |
### Python 3.X Job Failed |
| 135 |
|
| 136 |
<p align="center" style="margin: 0 0 10px"> |
| 137 |
<img src="https://raw.githubusercontent.com/encode/httpx/master/docs/img/gh-actions-fail-test.png" alt='Failing GitHub action test job'> |
| 138 |
</p> |
| 139 |
|
| 140 |
This job failing means the unit tests failed or not all code paths are covered by unit tests. |
| 141 |
|
| 142 |
If tests are failing you will see this message under the coverage report: |
| 143 |
|
| 144 |
`=== 1 failed, 435 passed, 1 skipped, 1 xfailed in 11.09s ===` |
| 145 |
|
| 146 |
If tests succeed but coverage doesn't reach our current threshold, you will see this |
| 147 |
message under the coverage report: |
| 148 |
|
| 149 |
`FAIL Required test coverage of 100% not reached. Total coverage: 99.00%` |
| 150 |
|
| 151 |
## Releasing |
| 152 |
|
| 153 |
*This section is targeted at HTTPX maintainers.* |
| 154 |
|
| 155 |
Before releasing a new version, create a pull request that includes: |
| 156 |
|
| 157 |
- **An update to the changelog**: |
| 158 |
- We follow the format from [keepachangelog](https://keepachangelog.com/en/1.0.0/). |
| 159 |
- [Compare](https://github.com/encode/httpx/compare/) `master` with the tag of the latest release, and list all entries that are of interest to our users: |
| 160 |
- Things that **must** go in the changelog: added, changed, deprecated or removed features, and bug fixes. |
| 161 |
- Things that **should not** go in the changelog: changes to documentation, tests or tooling. |
| 162 |
- Try sorting entries in descending order of impact / importance. |
| 163 |
- Keep it concise and to-the-point. 🎯 |
| 164 |
- **A version bump**: see `__version__.py`. |
| 165 |
|
| 166 |
For an example, see [#1006](https://github.com/encode/httpx/pull/1006). |
| 167 |
|
| 168 |
Once the release PR is merged, create a |
| 169 |
[new release](https://github.com/encode/httpx/releases/new) including: |
| 170 |
|
| 171 |
- Tag version like `0.13.3`. |
| 172 |
- Release title `Version 0.13.3` |
| 173 |
- Description copied from the changelog. |
| 174 |
|
| 175 |
Once created this release will be automatically uploaded to PyPI. |
| 176 |
|
| 177 |
If something goes wrong with the PyPI job the release can be published using the |
| 178 |
`scripts/publish` script. |
| 179 |
|
| 180 |
## Development proxy setup |
| 181 |
|
| 182 |
To test and debug requests via a proxy it's best to run a proxy server locally. |
| 183 |
Any server should do but HTTPCore's test suite uses |
| 184 |
[`mitmproxy`](https://mitmproxy.org/) which is written in Python, it's fully |
| 185 |
featured and has excellent UI and tools for introspection of requests. |
| 186 |
|
| 187 |
You can install `mitmproxy` using `pip install mitmproxy` or [several |
| 188 |
other ways](https://docs.mitmproxy.org/stable/overview-installation/). |
| 189 |
|
| 190 |
`mitmproxy` does require setting up local TLS certificates for HTTPS requests, |
| 191 |
as its main purpose is to allow developers to inspect requests that pass through |
| 192 |
it. We can set them up follows: |
| 193 |
|
| 194 |
1. [`pip install trustme-cli`](https://github.com/sethmlarson/trustme-cli/). |
| 195 |
2. `trustme-cli -i example.org www.example.org`, assuming you want to test |
| 196 |
connecting to that domain, this will create three files: `server.pem`, |
| 197 |
`server.key` and `client.pem`. |
| 198 |
3. `mitmproxy` requires a PEM file that includes the private key and the |
| 199 |
certificate so we need to concatenate them: |
| 200 |
`cat server.key server.pem > server.withkey.pem`. |
| 201 |
4. Start the proxy server `mitmproxy --certs server.withkey.pem`, or use the |
| 202 |
[other mitmproxy commands](https://docs.mitmproxy.org/stable/) with different |
| 203 |
UI options. |
| 204 |
|
| 205 |
At this point the server is ready to start serving requests, you'll need to |
| 206 |
configure HTTPX as described in the |
| 207 |
[proxy section](https://www.python-httpx.org/advanced/#http-proxying) and |
| 208 |
the [SSL certificates section](https://www.python-httpx.org/advanced/#ssl-certificates), |
| 209 |
this is where our previously generated `client.pem` comes in: |
| 210 |
|
| 211 |
``` |
| 212 |
import httpx |
| 213 |
|
| 214 |
ssl_context = httpx.SSLContext() |
| 215 |
ssl_context.load_verify_locations("/path/to/client.pem") |
| 216 |
|
| 217 |
with httpx.Client(proxy="http://127.0.0.1:8080/", ssl_context=ssl_context) as client: |
| 218 |
response = client.get("https://example.org") |
| 219 |
print(response.status_code) # should print 200 |
| 220 |
``` |
| 221 |
|
| 222 |
Note, however, that HTTPS requests will only succeed to the host specified |
| 223 |
in the SSL/TLS certificate we generated, HTTPS requests to other hosts will |
| 224 |
raise an error like: |
| 225 |
|
| 226 |
``` |
| 227 |
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate |
| 228 |
verify failed: Hostname mismatch, certificate is not valid for |
| 229 |
'duckduckgo.com'. (_ssl.c:1108) |
| 230 |
``` |
| 231 |
|
| 232 |
If you want to make requests to more hosts you'll need to regenerate the |
| 233 |
certificates and include all the hosts you intend to connect to in the |
| 234 |
seconds step, i.e. |
| 235 |
|
| 236 |
`trustme-cli -i example.org www.example.org duckduckgo.com www.duckduckgo.com` |