Skip to content

Commit 6ac9a87

Browse files
Merge pull request #1 from thewebscraping/chore/fix-multipart-encoders
chore: fix multipart encoders, cross share auth.
2 parents 2a4b999 + 3552808 commit 6ac9a87

File tree

12 files changed

+115
-65
lines changed

12 files changed

+115
-65
lines changed

.github/workflows/ci.yml

+23
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,26 @@ jobs:
2828
2929
- name: Run pre-commit
3030
uses: pre-commit/[email protected]
31+
32+
deploy:
33+
needs: build
34+
runs-on: ubuntu-latest
35+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
36+
steps:
37+
- uses: actions/checkout@v4
38+
- name: Configure Git Credentials
39+
run: |
40+
git config user.name github-actions[bot]
41+
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
42+
- uses: actions/setup-python@v5
43+
with:
44+
python-version: '3.10'
45+
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
46+
- uses: actions/cache@v4
47+
with:
48+
key: mkdocs-material-${{ env.cache_id }}
49+
path: .cache
50+
restore-keys: |
51+
mkdocs-material-
52+
- run: pip install -r requirements.txt
53+
- run: mkdocs gh-deploy --force

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ cython_debug/
160160
# and can be added to the global gitignore or merged into this file. For a more nuclear
161161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162162
.idea/
163+
tls_requests/bin/*xgo*

docs/advanced/async_client.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To send asynchronous HTTP requests, use the `AsyncClient`:
2525
>>> import asyncio
2626
>>> async def fetch(url):
2727
async with tls_requests.AsyncClient() as client:
28-
r = await client.get("https://www.example.com/")
28+
r = await client.get(url)
2929
return r
3030

3131
>>> r = asyncio.run(fetch("https://httpbin.org/get"))
@@ -87,7 +87,7 @@ import asyncio
8787
async def fetch(url):
8888
client = tls_requests.AsyncClient()
8989
try:
90-
response = await client.get("https://www.example.com/")
90+
response = await client.get("https://httpbin.org/get")
9191
finally:
9292
await client.aclose()
9393
```

docs/advanced/authentication.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ To customize how authentication is handled, you can use a function that modifies
2929
request.headers["X-Authorization"] = "123456"
3030
return request
3131

32-
>>> client = tls_requests.Client(auth=custom_auth)
33-
>>> response = client.get("https://www.example.com/")
32+
>>> response = tls_requests.get("https://httpbin.org/headers", auth=custom_auth)
3433
>>> response
3534
<Response [200 OK]>
35+
>>> response.request.headers["X-Authorization"]
36+
'123456'
37+
>>> response.json()["headers"]["X-Authorization"]
38+
'123456'
3639
```
3740

3841
* * *
@@ -53,7 +56,7 @@ class BearerAuth(tls_requests.Auth):
5356
self.token = token
5457

5558
def build_auth(self, request: tls_requests.Request) -> tls_requests.Request | None:
56-
request.headers['Authorization'] = f"Bearer {self.token}"
59+
request.headers["Authorization"] = f"Bearer {self.token}"
5760
return request
5861
```
5962

@@ -65,10 +68,13 @@ To use your custom `BearerAuth` implementation:
6568

6669
```pycon
6770
>>> auth = BearerAuth(token="your_jwt_token")
68-
>>> client = tls_requests.Client(auth=auth)
69-
>>> response = client.get("https://www.example.com/secure-endpoint")
71+
>>> response = tls_requests.get("https://httpbin.org/headers", auth=auth)
7072
>>> response
7173
<Response [200 OK]>
74+
>>> response.request.headers["Authorization"]
75+
'Bearer your_jwt_token'
76+
>>> response.json()["headers"]["Authorization"]
77+
'Bearer your_jwt_token'
7278
```
7379

7480
With these approaches, you can integrate various authentication strategies into your `tls_requests` workflow, whether built-in or custom-designed for specific needs.

docs/advanced/client.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The best practice is to use a `Client` as a context manager. This ensures connec
3333

3434
```python
3535
with tls_requests.Client() as client:
36-
response = client.get("https://example.com")
36+
response = client.get("https://httpbin.org/get")
3737
print(response) # <Response [200 OK]>
3838
```
3939

@@ -44,7 +44,7 @@ If not using a context manager, ensure to close the client explicitly:
4444
```python
4545
client = tls_requests.Client()
4646
try:
47-
response = client.get("https://example.com")
47+
response = client.get("https://httpbin.org/get")
4848
print(response) # <Response [200 OK]>
4949
finally:
5050
client.close()
@@ -59,7 +59,7 @@ A `Client` can send requests using methods like `.get()`, `.post()`, etc.:
5959

6060
```python
6161
with tls_requests.Client() as client:
62-
response = client.get("https://example.com")
62+
response = client.get("https://httpbin.org/get")
6363
print(response) # <Response [200 OK]>
6464
```
6565

@@ -70,7 +70,7 @@ To include custom headers in a request:
7070
```python
7171
headers = {'X-Custom': 'value'}
7272
with tls_requests.Client() as client:
73-
response = client.get("https://example.com", headers=headers)
73+
response = client.get("https://httpbin.org/get", headers=headers)
7474
print(response.request.headers['X-Custom']) # 'value'
7575
```
7676

@@ -101,7 +101,7 @@ When client-level and request-level options overlap:
101101
client_headers = {'X-Auth': 'client'}
102102
request_headers = {'X-Custom': 'request'}
103103
with tls_requests.Client(headers=client_headers) as client:
104-
response = client.get("https://example.com", headers=request_headers)
104+
response = client.get("https://httpbin.org/get", headers=request_headers)
105105
print(response.request.headers['X-Auth']) # 'client'
106106
print(response.request.headers['X-Custom']) # 'request'
107107
```
@@ -110,7 +110,7 @@ with tls_requests.Client(headers=client_headers) as client:
110110

111111
```python
112112
with tls_requests.Client(auth=('user', 'pass')) as client:
113-
response = client.get("https://example.com", auth=('admin', 'adminpass'))
113+
response = client.get("https://httpbin.org/get", auth=('admin', 'adminpass'))
114114
print(response.request.headers['Authorization']) # Encoded 'admin:adminpass'
115115

116116
```
@@ -123,7 +123,7 @@ Advanced Request Handling
123123
For more control, explicitly build and send `Request` instances:
124124

125125
```python
126-
request = tls_requests.Request("GET", "https://example.com")
126+
request = tls_requests.Request("GET", "https://httpbin.org/get")
127127
with tls_requests.Client() as client:
128128
response = client.send(request)
129129
print(response) # <Response [200 OK]>
@@ -133,7 +133,7 @@ To combine client- and request-level configurations:
133133

134134
```python
135135
with tls_requests.Client(headers={"X-Client-ID": "ABC123"}) as client:
136-
request = client.build_request("GET", "https://api.example.com")
136+
request = client.build_request("GET", "https://httpbin.org/json")
137137
del request.headers["X-Client-ID"] # Modify as needed
138138
response = client.send(request)
139139
print(response)

docs/advanced/hooks.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ client.hooks = {
100100
Best Practices
101101
--------------
102102

103-
1. **Always Use Lists:** Hooks must be registered as **lists of callables**, even if you are adding only one function.
104-
2. **Combine Hooks:** You can register multiple hooks for the same event type to handle various concerns, such as logging and error handling.
105-
3. **Order Matters:** Hooks are executed in the order they are registered.
103+
1. **Access Content**: Use `.read()` or `await read()` in asynchronous contexts to access `response.content` before returning it.
104+
2. **Always Use Lists:** Hooks must be registered as **lists of callables**, even if you are adding only one function.
105+
3. **Combine Hooks:** You can register multiple hooks for the same event type to handle various concerns, such as logging and error handling.
106+
4. **Order Matters:** Hooks are executed in the order they are registered.
106107

107108
With hooks, TLS Requests provides a flexible mechanism to seamlessly integrate monitoring, logging, or custom behaviors into your HTTP workflows.

docs/advanced/proxies.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To route traffic through an HTTP proxy, specify the proxy URL in the `proxy` par
1919

2020
```python
2121
with tls_requests.Client(proxy="http://localhost:8030") as client:
22-
response = client.get("https://example.com")
22+
response = client.get("https://httpbin.org/get")
2323
print(response) # <Response [200 OK]>
2424

2525
```
@@ -30,7 +30,7 @@ For SOCKS proxies, use the `socks5` scheme in the proxy URL:
3030

3131
```python
3232
client = tls_requests.Client(proxy="socks5://user:pass@host:port")
33-
response = client.get("https://example.com")
33+
response = client.get("https://httpbin.org/get")
3434
print(response) # <Response [200 OK]>
3535
```
3636

@@ -47,7 +47,7 @@ You can include proxy credentials in the `userinfo` section of the URL:
4747

4848
```python
4949
with tls_requests.Client(proxy="http://username:password@localhost:8030") as client:
50-
response = client.get("https://example.com")
50+
response = client.get("https://httpbin.org/get")
5151
print(response) # <Response [200 OK]>
5252
```
5353

docs/quickstart.md

+22-25
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ Include lists or merge parameters with existing query strings:
109109
'<URL: https://httpbin.org/get?order_by=asc&key1=value1&key2=value2&key2=value3>'
110110
```
111111

112+
* * *
113+
114+
Custom Headers
115+
--------------
116+
117+
Add custom headers to requests:
118+
119+
```pycon
120+
>>> url = 'https://httpbin.org/headers'
121+
>>> headers = {'user-agent': 'my-app/1.0.0'}
122+
>>> r = tls_requests.get(url, headers=headers)
123+
>>> r.json()
124+
{
125+
"headers": {
126+
...
127+
"Host": "httpbin.org",
128+
"User-Agent": "my-app/1.0.0",
129+
...
130+
}
131+
}
132+
```
133+
112134

113135
* * *
114136

@@ -163,28 +185,6 @@ Parse JSON responses directly:
163185
}
164186
```
165187

166-
* * *
167-
168-
Custom Headers
169-
--------------
170-
171-
Add custom headers to requests:
172-
173-
```pycon
174-
>>> url = 'https://httpbin.org/headers'
175-
>>> headers = {'user-agent': 'my-app/1.0.0'}
176-
>>> r = tls_requests.get(url, headers=headers)
177-
>>> r.json()
178-
{
179-
"headers": {
180-
...
181-
"Host": "httpbin.org",
182-
"User-Agent": "my-app/1.0.0",
183-
...
184-
}
185-
}
186-
```
187-
188188
### Form-Encoded Data
189189

190190
Include form data in POST requests:
@@ -380,9 +380,6 @@ The `Headers` data type is case-insensitive, so you can use any capitalization.
380380
```pycon
381381
>>> r.headers['Content-Type']
382382
'application/json'
383-
384-
>>> r.headers.get('content-type')
385-
'application/json'
386383
```
387384

388385
### Cookies

docs/tls/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Retrieves cookies associated with a session for a specific URL.
4848
```pycon
4949
>>> from tls_requests import TLSClient
5050
>>> TLSClient.initialize()
51-
>>> cookies = TLSClient.get_cookies(session_id="session123", url="https://example.com")
51+
>>> cookies = TLSClient.get_cookies(session_id="session123", url="https://httpbin.org/get")
5252
```
5353

5454
* * *
@@ -74,7 +74,7 @@ Adds cookies to a specific TLS session.
7474
"value": "baz2",
7575
}],
7676
"sessionId": "session123",
77-
"url": "https://example.com",
77+
"url": "https://httpbin.org/",
7878
}
7979
>>> TLSClient.add_cookies(session_id="session123", payload=payload)
8080
```
@@ -138,7 +138,7 @@ Sends a request using the TLS library. Using [TLSConfig](configuration) to gener
138138
```pycon
139139
>>> from tls_requests import TLSClient, TLSConfig
140140
>>> TLSClient.initialize()
141-
>>> config = TLSConfig(requestMethod="GET", requestUrl="https://example.com")
141+
>>> config = TLSConfig(requestMethod="GET", requestUrl="https://httpbin.org/get")
142142
>>> response = TLSClient.request(config.to_dict())
143143
```
144144

tls_requests/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
__url__ = "https://github.com/thewebscraping/tls-requests"
44
__author__ = "Tu Pham"
55
__author_email__ = "[email protected]"
6-
__version__ = "1.0.2"
6+
__version__ = "1.0.3"
77
__license__ = "MIT"

0 commit comments

Comments
 (0)