Skip to content

Commit

Permalink
HG-3433: adds 'custom_headers' (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
davi-souza authored Sep 12, 2024
1 parent f9d37ad commit b118c8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 16 additions & 0 deletions target_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'"
Expand Down
8 changes: 2 additions & 6 deletions target_api/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit b118c8c

Please sign in to comment.