-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HG-3433: adds 'custom_headers' (#18)
- Loading branch information
1 parent
f9d37ad
commit b118c8c
Showing
2 changed files
with
18 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,22 @@ def endpoint(self) -> str: | |
def unified_schema(self) -> BaseModel: | ||
return None | ||
|
||
@property | ||
def custom_headers(self) -> dict: | ||
custom_headers = { | ||
"User-Agent": self._config.get("user_agent", "target-api <[email protected]>") | ||
} | ||
config_custom_headers = self._config.get("custom_headers") or list() | ||
for ch in config_custom_headers: | ||
if not isinstance(ch, dict): | ||
continue | ||
name = ch.get("name") | ||
value = ch.get("value") | ||
if not isinstance(name, str) or not isinstance(value, str): | ||
continue | ||
custom_headers[name] = value | ||
return custom_headers | ||
|
||
def response_error_message(self, response: requests.Response) -> str: | ||
try: | ||
response_text = f" with response body: '{response.text}'" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,7 @@ def preprocess_record(self, record: dict, context: dict) -> dict: | |
|
||
def upsert_record(self, record: dict, context: dict): | ||
response = self.request_api( | ||
self._config.get("method", "POST").upper(), request_data=record, headers={ | ||
"User-Agent": self._config.get("user_agent", "target-api <[email protected]>") | ||
} | ||
self._config.get("method", "POST").upper(), request_data=record, headers=self.custom_headers | ||
) | ||
|
||
self.logger.info(f"Response: {response.status_code} - {response.text}") | ||
|
@@ -70,9 +68,7 @@ def process_batch_record(self, record: dict, index: int) -> dict: | |
|
||
def make_batch_request(self, records: List[dict]): | ||
response = self.request_api( | ||
self._config.get("method", "POST").upper(), request_data=records, headers={ | ||
"User-Agent": self._config.get("user_agent", "target-api <[email protected]>") | ||
} | ||
self._config.get("method", "POST").upper(), request_data=records, headers=self.custom_headers | ||
) | ||
|
||
self.logger.info(f"Response: {response.status_code} - {response.text}") | ||
|