Skip to content

Commit 7c24752

Browse files
committed
Use common sessions config
1 parent bcc2319 commit 7c24752

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

hackspaceapi/services/homeassistant.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
from prometheus_client import Summary
88

99
from hackspaceapi.config import settings
10+
from hackspaceapi.services.session import get_requests_session
1011

11-
session = requests.session()
12-
session.headers = {
13-
"Authorization": "Bearer {0}".format(settings.homeassistant_token),
14-
"content-type": "application/json",
15-
}
12+
session = get_requests_session()
13+
session.headers.update(
14+
{
15+
"Authorization": "Bearer {0}".format(settings.homeassistant_token),
16+
"content-type": "application/json",
17+
}
18+
)
19+
20+
call_homeassistant_metrics = Summary(
21+
"hackspaceapi_homeassistant_api_time", "Summary of calls to the Home Assistant API"
22+
)
1623

17-
call_homeassistant_metrics = Summary('hackspaceapi_homeassistant_api_time', 'Summary of calls to the Home Assistant API')
1824

1925
@ttl_cache(ttl=60)
2026
@call_homeassistant_metrics.time()

hackspaceapi/services/prometheus.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from prometheus_client import Summary
88

99
from hackspaceapi.config import settings
10+
from hackspaceapi.services.session import get_requests_session
11+
12+
session = get_requests_session()
1013

1114
prometheus_metric_summary = Summary('hackspaceapi_prometheus_api_time', 'Summary of calls to the Prometheus API')
1215

@@ -15,7 +18,7 @@
1518
def get_prometheus_metric(query: str) -> Optional[Dict]:
1619
url = urljoin(settings.prometheus_instance, "/api/v1/query")
1720
try:
18-
resp = requests.get(url, params={"query": query})
21+
resp = session.get(url, params={"query": query})
1922
if resp.ok:
2023
data = resp.json()
2124
if "status" in data and data["status"] == "success":

hackspaceapi/services/session.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import requests
2+
3+
from hackspaceapi import VERSION
4+
5+
6+
def get_requests_session() -> requests.Session:
7+
"""
8+
Creates a Requests session pre-configured with the common values used by
9+
all outbound requests
10+
"""
11+
session = requests.session()
12+
session.headers = {"User-Agent": "HackspaceAPI/{0}".format(VERSION)}
13+
14+
return session

hackspaceapi/services/website.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from cachetools.func import ttl_cache
66
from prometheus_client import Summary
77

8+
from hackspaceapi.services.session import get_requests_session
9+
10+
session = get_requests_session()
811

912
website_metric_summary = Summary(
1013
"hackspaceapi_website_data_time", "Summary of calls to the Website data"
@@ -16,7 +19,7 @@
1619
def get_membership_data() -> Optional[Iterable]:
1720
url = "https://web-test.leighhack.org/membership/index.json"
1821
try:
19-
resp = requests.get(url)
22+
resp = session.get(url)
2023
if resp.ok:
2124
data = resp.json()
2225
return data["memberships"]

0 commit comments

Comments
 (0)