Skip to content

Commit aba0a7d

Browse files
authored
Merge pull request #31 from devolo/client
Add possibility to use own HTTP client
2 parents 246b948 + 6f84759 commit aba0a7d

File tree

6 files changed

+38
-13
lines changed

6 files changed

+38
-13
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: '22.3.0'
3+
rev: '23.1.0'
44
hooks:
55
- id: black
66
- repo: https://github.com/pycqa/isort
7-
rev: '5.7.0'
7+
rev: '5.12.0'
88
hooks:
99
- id: isort
1010
- repo: https://github.com/pre-commit/pre-commit-hooks
11-
rev: 'v3.4.0'
11+
rev: 'v4.4.0'
1212
hooks:
1313
- id: end-of-file-fixer
1414
- id: trailing-whitespace

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [v1.1.0] - 2022/02/16
8+
9+
### Added
10+
11+
- Use own HTTP client, if special settings are needed
12+
713
## [v1.0.2] - 2022/12/09
814

915
### Fixed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,29 @@ async def get_grade():
138138
api = Endpoint()
139139
endpoint = await api.get(host="devolo.de", s="195.201.179.93")
140140
return endpoint.grade
141+
142+
asyncio.run(get_grade())
141143
```
142144

143145
Classes are called like the [API call](https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#protocol-calls) without the leading get. The get method will query the API. It will take the parameters like in the documentation and return a dataclass representing the object, the API describes. One exception in the naming: the getEndpointData call is implemented in the Endpoint class to be able to better distinguish it from its EndpointData result object.
144146

145147
## Exceptions
146148

147149
Three types of exceptions might hit you, if the connection to SSL Labs' API is affected: ```httpx.ConnectTimeout``` or ```httpx.ReadTimeout``` appear, if the servers are down, and ```httpx.HTTPStatusError``` appears, if there is a client or server [error response](https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#error-response-status-codes). In this cases, you are asked to wait 15 to 30 minutes before you try again.
150+
151+
## Using an own HTTP client
152+
153+
If you have special needs (e.g. what to use a proxy server), you can create an own HTTP client. Please read the [httpx documentation](https://www.python-httpx.org/advanced) to find out which possibilities you have.
154+
155+
```python
156+
import asyncio
157+
158+
from httpx import AsyncClient
159+
from ssllabs import Ssllabs
160+
161+
async def analyze():
162+
async with AsyncClient(proxies="http://localhost:8030") as client:
163+
ssllabs = Ssllabs(client)
164+
return await ssllabs.analyze(host="devolo.de")
165+
166+
asyncio.run(analyze())

ssllabs/ssllabs.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
22
import logging
3+
from typing import Optional
34

4-
from httpx import ConnectTimeout, HTTPStatusError, ReadTimeout
5+
from httpx import AsyncClient, ConnectTimeout, HTTPStatusError, ReadTimeout
56

67
from .api import Analyze, Info, RootCertsRaw, StatusCodes
78
from .data.host import HostData
@@ -12,7 +13,8 @@
1213
class Ssllabs:
1314
"""Highlevel methods to interact with the SSL Labs Assessment APIs."""
1415

15-
def __init__(self):
16+
def __init__(self, client: Optional[AsyncClient] = None):
17+
self._client = client
1618
self._logger = logging.getLogger("ssllabs.Ssllabs")
1719
self._semaphore = asyncio.Semaphore(1)
1820

@@ -22,7 +24,7 @@ async def availability(self) -> bool:
2224
2325
See also: https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#error-response-status-codes
2426
"""
25-
i = Info()
27+
i = Info(self._client)
2628
try:
2729
await i.get()
2830
self._logger.info("SSL Labs servers are up an running.")
@@ -43,7 +45,7 @@ async def analyze(self, host: str, publish: bool = False, ignore_mismatch: bool
4345
"""
4446
await self._semaphore.acquire()
4547
self._logger.info("Analyzing %s", host)
46-
i = Info()
48+
i = Info(self._client)
4749
info = await i.get()
4850

4951
# Wait for a free slot, if all slots are in use
@@ -56,7 +58,7 @@ async def analyze(self, host: str, publish: bool = False, ignore_mismatch: bool
5658
if info.currentAssessments != 0:
5759
await asyncio.sleep(info.newAssessmentCoolOff / 1000)
5860

59-
a = Analyze()
61+
a = Analyze(self._client)
6062
host_object = await a.get(
6163
host=host, startNew="on", publish="on" if publish else "off", ignoreMismatch="on" if ignore_mismatch else "off"
6264
)
@@ -73,7 +75,7 @@ async def info(self) -> InfoData:
7375
7476
See also: https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#info
7577
"""
76-
i = Info()
78+
i = Info(self._client)
7779
return await i.get()
7880

7981
async def root_certs(self, trust_store: int = 1) -> str:
@@ -89,7 +91,7 @@ async def root_certs(self, trust_store: int = 1) -> str:
8991
"""Trust store not found. Please choose on of the following:
9092
1-Mozilla, 2-Apple MacOS, 3-Android, 4-Java, 5-Windows"""
9193
)
92-
rcr = RootCertsRaw()
94+
rcr = RootCertsRaw(self._client)
9395
return await rcr.get(trustStore=trust_store)
9496

9597
async def status_codes(self) -> StatusCodesData:
@@ -98,5 +100,5 @@ async def status_codes(self) -> StatusCodesData:
98100
99101
See also: https://github.com/ssllabs/ssllabs-scan/blob/master/ssllabs-api-docs-v3.md#retrieve-known-status-codes
100102
"""
101-
sc = StatusCodes()
103+
sc = StatusCodes(self._client)
102104
return await sc.get()

tests/test_api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919

2020
class TestApi:
21-
2221
API_CALLS = [
2322
(Endpoint, EndpointData, {"host": "devolo.de", "s": "195.201.179.93"}),
2423
(StatusCodes, StatusCodesData, {}),

tests/test_ssllabs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020

2121
class TestSsllabs:
22-
2322
API_CALLS: list = [("info.Info", InfoData, {}), ("status_codes.StatusCodes", StatusCodesData, {})]
2423

2524
@pytest.mark.asyncio

0 commit comments

Comments
 (0)