Skip to content

chore: fix multipart encoders, cross share auth. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,26 @@ jobs:

- name: Run pre-commit
uses: pre-commit/action@v3.0.0

deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Configure Git Credentials
run: |
git config user.name github-actions[bot]
git config user.email 41898282+github-actions[bot]@users.noreply.github.com
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
- uses: actions/cache@v4
with:
key: mkdocs-material-${{ env.cache_id }}
path: .cache
restore-keys: |
mkdocs-material-
- run: pip install -r requirements.txt
- run: mkdocs gh-deploy --force
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
tls_requests/bin/*xgo*
4 changes: 2 additions & 2 deletions docs/advanced/async_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To send asynchronous HTTP requests, use the `AsyncClient`:
>>> import asyncio
>>> async def fetch(url):
async with tls_requests.AsyncClient() as client:
r = await client.get("https://www.example.com/")
r = await client.get(url)
return r

>>> r = asyncio.run(fetch("https://httpbin.org/get"))
Expand Down Expand Up @@ -87,7 +87,7 @@ import asyncio
async def fetch(url):
client = tls_requests.AsyncClient()
try:
response = await client.get("https://www.example.com/")
response = await client.get("https://httpbin.org/get")
finally:
await client.aclose()
```
Expand Down
16 changes: 11 additions & 5 deletions docs/advanced/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ To customize how authentication is handled, you can use a function that modifies
request.headers["X-Authorization"] = "123456"
return request

>>> client = tls_requests.Client(auth=custom_auth)
>>> response = client.get("https://www.example.com/")
>>> response = tls_requests.get("https://httpbin.org/headers", auth=custom_auth)
>>> response
<Response [200 OK]>
>>> response.request.headers["X-Authorization"]
'123456'
>>> response.json()["headers"]["X-Authorization"]
'123456'
```

* * *
Expand All @@ -53,7 +56,7 @@ class BearerAuth(tls_requests.Auth):
self.token = token

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

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

```pycon
>>> auth = BearerAuth(token="your_jwt_token")
>>> client = tls_requests.Client(auth=auth)
>>> response = client.get("https://www.example.com/secure-endpoint")
>>> response = tls_requests.get("https://httpbin.org/headers", auth=auth)
>>> response
<Response [200 OK]>
>>> response.request.headers["Authorization"]
'Bearer your_jwt_token'
>>> response.json()["headers"]["Authorization"]
'Bearer your_jwt_token'
```

With these approaches, you can integrate various authentication strategies into your `tls_requests` workflow, whether built-in or custom-designed for specific needs.
16 changes: 8 additions & 8 deletions docs/advanced/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The best practice is to use a `Client` as a context manager. This ensures connec

```python
with tls_requests.Client() as client:
response = client.get("https://example.com")
response = client.get("https://httpbin.org/get")
print(response) # <Response [200 OK]>
```

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

```python
with tls_requests.Client() as client:
response = client.get("https://example.com")
response = client.get("https://httpbin.org/get")
print(response) # <Response [200 OK]>
```

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

Expand Down Expand Up @@ -101,7 +101,7 @@ When client-level and request-level options overlap:
client_headers = {'X-Auth': 'client'}
request_headers = {'X-Custom': 'request'}
with tls_requests.Client(headers=client_headers) as client:
response = client.get("https://example.com", headers=request_headers)
response = client.get("https://httpbin.org/get", headers=request_headers)
print(response.request.headers['X-Auth']) # 'client'
print(response.request.headers['X-Custom']) # 'request'
```
Expand All @@ -110,7 +110,7 @@ with tls_requests.Client(headers=client_headers) as client:

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

```
Expand All @@ -123,7 +123,7 @@ Advanced Request Handling
For more control, explicitly build and send `Request` instances:

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

```python
with tls_requests.Client(headers={"X-Client-ID": "ABC123"}) as client:
request = client.build_request("GET", "https://api.example.com")
request = client.build_request("GET", "https://httpbin.org/json")
del request.headers["X-Client-ID"] # Modify as needed
response = client.send(request)
print(response)
Expand Down
7 changes: 4 additions & 3 deletions docs/advanced/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ client.hooks = {
Best Practices
--------------

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

With hooks, TLS Requests provides a flexible mechanism to seamlessly integrate monitoring, logging, or custom behaviors into your HTTP workflows.
6 changes: 3 additions & 3 deletions docs/advanced/proxies.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ To route traffic through an HTTP proxy, specify the proxy URL in the `proxy` par

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

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

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

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

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

Expand Down
47 changes: 22 additions & 25 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ Include lists or merge parameters with existing query strings:
'<URL: https://httpbin.org/get?order_by=asc&key1=value1&key2=value2&key2=value3>'
```

* * *

Custom Headers
--------------

Add custom headers to requests:

```pycon
>>> url = 'https://httpbin.org/headers'
>>> headers = {'user-agent': 'my-app/1.0.0'}
>>> r = tls_requests.get(url, headers=headers)
>>> r.json()
{
"headers": {
...
"Host": "httpbin.org",
"User-Agent": "my-app/1.0.0",
...
}
}
```


* * *

Expand Down Expand Up @@ -163,28 +185,6 @@ Parse JSON responses directly:
}
```

* * *

Custom Headers
--------------

Add custom headers to requests:

```pycon
>>> url = 'https://httpbin.org/headers'
>>> headers = {'user-agent': 'my-app/1.0.0'}
>>> r = tls_requests.get(url, headers=headers)
>>> r.json()
{
"headers": {
...
"Host": "httpbin.org",
"User-Agent": "my-app/1.0.0",
...
}
}
```

### Form-Encoded Data

Include form data in POST requests:
Expand Down Expand Up @@ -380,9 +380,6 @@ The `Headers` data type is case-insensitive, so you can use any capitalization.
```pycon
>>> r.headers['Content-Type']
'application/json'

>>> r.headers.get('content-type')
'application/json'
```

### Cookies
Expand Down
6 changes: 3 additions & 3 deletions docs/tls/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Retrieves cookies associated with a session for a specific URL.
```pycon
>>> from tls_requests import TLSClient
>>> TLSClient.initialize()
>>> cookies = TLSClient.get_cookies(session_id="session123", url="https://example.com")
>>> cookies = TLSClient.get_cookies(session_id="session123", url="https://httpbin.org/get")
```

* * *
Expand All @@ -74,7 +74,7 @@ Adds cookies to a specific TLS session.
"value": "baz2",
}],
"sessionId": "session123",
"url": "https://example.com",
"url": "https://httpbin.org/",
}
>>> TLSClient.add_cookies(session_id="session123", payload=payload)
```
Expand Down Expand Up @@ -138,7 +138,7 @@ Sends a request using the TLS library. Using [TLSConfig](configuration) to gener
```pycon
>>> from tls_requests import TLSClient, TLSConfig
>>> TLSClient.initialize()
>>> config = TLSConfig(requestMethod="GET", requestUrl="https://example.com")
>>> config = TLSConfig(requestMethod="GET", requestUrl="https://httpbin.org/get")
>>> response = TLSClient.request(config.to_dict())
```

Expand Down
2 changes: 1 addition & 1 deletion tls_requests/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
__url__ = "https://github.com/thewebscraping/tls-requests"
__author__ = "Tu Pham"
__author_email__ = "thetwofarm@gmail.com"
__version__ = "1.0.2"
__version__ = "1.0.3"
__license__ = "MIT"
Loading
Loading