Skip to content

Commit

Permalink
Improve speed with requests Session
Browse files Browse the repository at this point in the history
  • Loading branch information
b-per committed Jun 28, 2024
1 parent 4e055a1 commit 3766f66
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dbt-jobs-as-code"
version = "0.6.0"
version = "0.6.1"
description = "A CLI to allow defining dbt Cloud jobs as code"
authors = ["dbt Labs <[email protected]>"]
license = "Apache License 2.0"
Expand Down
23 changes: 12 additions & 11 deletions src/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(
logger.warning(
"SSL verification is disabled. This is not recommended unless you absolutely need this config."
)
self._session = requests.Session()

def _clear_env_var_cache(self, job_definition_id: Optional[int]) -> None:
"""Clear out any cached environment variables for a given job."""
Expand Down Expand Up @@ -79,7 +80,7 @@ def update_job(self, job: JobDefinition) -> JobDefinition:

logger.debug("Updating {job_name}. {job}", job_name=job.name, job=job)

response = requests.post( # Yes, it's actually a POST. Ew.
response = self._session.post( # Yes, it's actually a POST. Ew.
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job.id}/",
headers=self._headers,
data=job.to_payload(),
Expand All @@ -98,7 +99,7 @@ def create_job(self, job: JobDefinition) -> Optional[JobDefinition]:

logger.debug("Creating {job_name}. {job}", job_name=job.name, job=job)

response = requests.post(
response = self._session.post(
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/",
headers=self._headers,
data=job.to_payload(),
Expand All @@ -118,7 +119,7 @@ def delete_job(self, job: JobDefinition) -> None:

logger.debug("Deleting {job_name}. {job}", job_name=job.name, job=job)

response = requests.delete(
response = self._session.delete(
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job.id}/",
headers=self._headers,
verify=self._verify,
Expand All @@ -134,7 +135,7 @@ def get_job(self, job_id: int) -> Optional[JobDefinition]:

self._check_for_creds()

response = requests.get(
response = self._session.get(
url=(f"{self.base_url}/api/v2/accounts/" f"{self.account_id}/jobs/{job_id}/"),
headers=self._headers,
verify=self._verify,
Expand All @@ -149,7 +150,7 @@ def get_job_missing_fields(self, job_id: int) -> Optional[JobMissingFields]:

self._check_for_creds()

response = requests.get(
response = self._session.get(
url=(f"{self.base_url}/api/v2/accounts/" f"{self.account_id}/jobs/{job_id}/"),
headers=self._headers,
verify=self._verify,
Expand Down Expand Up @@ -222,7 +223,7 @@ def _build_parameters(
return parameters

def _make_request(self, parameters: dict[str, Any]):
response = requests.get(
response = self._session.get(
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/",
params=parameters,
headers=self._headers,
Expand All @@ -246,7 +247,7 @@ def get_env_vars(

self._check_for_creds()

response = requests.get(
response = self._session.get(
url=(
f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{project_id}/environment-variables/job/?job_definition_id={job_id}"
),
Expand Down Expand Up @@ -274,7 +275,7 @@ def create_env_var(
) -> CustomEnvironmentVariablePayload:
"""Create a new Custom Environment Variable in dbt Cloud."""

response = requests.post(
response = self._session.post(
f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{env_var.project_id}/environment-variables/",
headers=self._headers,
data=env_var.model_dump_json(),
Expand Down Expand Up @@ -322,7 +323,7 @@ def update_env_var(
**custom_env_var.model_dump(),
)

response = requests.post(
response = self._session.post(
url=url, headers=self._headers, data=payload.model_dump_json(), verify=self._verify
)

Expand All @@ -339,7 +340,7 @@ def delete_env_var(self, project_id: int, env_var_id: int) -> None:

logger.debug(f"Deleting env var id {env_var_id}")

response = requests.delete(
response = self._session.delete(
url=f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{project_id}/environment-variables/{env_var_id}/",
headers=self._headers,
verify=self._verify,
Expand All @@ -351,7 +352,7 @@ def delete_env_var(self, project_id: int, env_var_id: int) -> None:
logger.success("Env Var Job Overwrite deleted successfully.")

def _fetch_environment(self, url) -> List[dict]:
response = requests.get(
response = self._session.get(
url=url,
headers=self._headers,
verify=self._verify,
Expand Down

0 comments on commit 3766f66

Please sign in to comment.