File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77from prometheus_client import Summary
88
99from 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 ()
Original file line number Diff line number Diff line change 77from prometheus_client import Summary
88
99from hackspaceapi .config import settings
10+ from hackspaceapi .services .session import get_requests_session
11+
12+ session = get_requests_session ()
1013
1114prometheus_metric_summary = Summary ('hackspaceapi_prometheus_api_time' , 'Summary of calls to the Prometheus API' )
1215
1518def 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" :
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 55from cachetools .func import ttl_cache
66from prometheus_client import Summary
77
8+ from hackspaceapi .services .session import get_requests_session
9+
10+ session = get_requests_session ()
811
912website_metric_summary = Summary (
1013 "hackspaceapi_website_data_time" , "Summary of calls to the Website data"
1619def 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" ]
You can’t perform that action at this time.
0 commit comments