-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.py
39 lines (32 loc) · 1.24 KB
/
connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import requests
import datetime
import os
import structlog
from BaseClient import BaseClient
logger = structlog.get_logger()
class Connector(BaseClient):
def auth_is_connection_test(self):
"""
Whether the authentication is a connection test.
"""
return True
def auth(self, params: dict) -> dict:
"""
Implement special auth scheme.
:param params: Parameters to authenticate from the connector configuration form.
:return True if the authentication was successful, False otherwise.
"""
api_key: str = params["api_key"]
headers = {"X-Api-Key": f"{api_key}"}
# Try to get units just to test the connection.
url = "https://newsapi.org/v2/everything?q=movinglake"
r = requests.get(url, headers=headers)
if r.status_code > 399:
raise Exception(
f"Invalid credentials using url {url} headers {headers}. Status code: {r.status_code}"
)
self._cv.http.set_header(headers)
# Needed if using the url from the configuration JSON.
self._cv.http.URL_BASE = self._cv.http.URL_BASE_INIT
# Return the headers! For any subsequent call to this API.
return headers