Skip to content

Commit

Permalink
aiohttp to async requests
Browse files Browse the repository at this point in the history
  • Loading branch information
talsabagport committed Oct 20, 2024
1 parent 69745d6 commit 979628e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
88 changes: 41 additions & 47 deletions integrations/jira/jira/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import asyncio
import typing
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator

import aiohttp
from aiohttp import ClientTimeout, BasicAuth
import requests
from loguru import logger

from jira.overrides import JiraResourceConfig
Expand Down Expand Up @@ -33,77 +32,71 @@ def __init__(self, jira_url: str, jira_email: str, jira_token: str) -> None:
self.jira_rest_url = f"{self.jira_url}/rest"
self.jira_email = jira_email
self.jira_token = jira_token

self.jira_api_auth = BasicAuth(self.jira_email, self.jira_token)
self.jira_api_auth = (self.jira_email, self.jira_token)

self.api_url = f"{self.jira_rest_url}/api/3"
self.webhooks_url = f"{self.jira_rest_url}/webhooks/1.0/webhook"

@asynccontextmanager
async def _create_session(self):
try:
session = aiohttp.ClientSession(auth=self.jira_api_auth, timeout=ClientTimeout(30))
yield session
finally:
await session.close()

self.client = requests.session()
self.client.auth = self.jira_api_auth

@staticmethod
def _generate_base_req_params(
maxResults: int = 0, startAt: int = 0
maxResults: int = 0, startAt: int = 0
) -> dict[str, Any]:
return {
"maxResults": maxResults,
"startAt": startAt,
}

async def _get_paginated_projects(self, params: dict[str, Any]) -> dict[str, Any]:
async with self._create_session() as session:
async with session.get(
f"{self.api_url}/project/search", params=params
) as project_response:
project_response.raise_for_status()
return await project_response.json()
project_response = await asyncio.to_thread(
self.client.get, f"{self.api_url}/project/search", params=params
)
project_response.raise_for_status()
return project_response.json()

async def _get_paginated_issues(self, params: dict[str, Any]) -> dict[str, Any]:
async with self._create_session() as session:
async with session.get(f"{self.api_url}/search", params=params) as issue_response:
issue_response.raise_for_status()
return await issue_response.json()
issue_response = await asyncio.to_thread(
self.client.get, f"{self.api_url}/search", params=params
)
issue_response.raise_for_status()
return issue_response.json()

async def create_events_webhook(self, app_host: str) -> None:
webhook_target_app_host = f"{app_host}/integration/webhook"
async with self._create_session() as session:
async with session.get(f"{self.webhooks_url}") as webhook_check_response:
webhook_check_response.raise_for_status()
webhook_check = await webhook_check_response.json()
webhook_check_response = await asyncio.to_thread(
self.client.get, f"{self.webhooks_url}"
)
webhook_check_response.raise_for_status()
webhook_check = webhook_check_response.json()

for webhook in webhook_check:
if webhook["url"] == webhook_target_app_host:
logger.info("Ocean real time reporting webhook already exists")
return
for webhook in webhook_check:
if webhook["url"] == webhook_target_app_host:
logger.info("Ocean real time reporting webhook already exists")
return

body = {
"name": f"{ocean.config.integration.identifier}-{WEBHOOK_NAME}",
"url": webhook_target_app_host,
"events": WEBHOOK_EVENTS,
}

async with self._create_session() as session:
async with session.post(f"{self.webhooks_url}", json=body) as webhook_create_response:
webhook_create_response.raise_for_status()
logger.info("Ocean real time reporting webhook created")
webhook_create_response = await asyncio.to_thread(
self.client.post, f"{self.webhooks_url}", json=body
)
webhook_create_response.raise_for_status()
logger.info("Ocean real time reporting webhook created")

async def get_single_project(self, project_key: str) -> dict[str, Any]:
async with self._create_session() as session:
async with session.get(
f"{self.api_url}/project/{project_key}"
) as project_response:
project_response.raise_for_status()
return await project_response.json()
project_response = await asyncio.to_thread(
self.client.get, f"{self.api_url}/project/{project_key}"
)
project_response.raise_for_status()
return project_response.json()

async def get_paginated_projects(
self,
self,
) -> AsyncGenerator[list[dict[str, Any]], None]:
logger.info("Getting projects from Jira")

Expand All @@ -126,10 +119,11 @@ async def get_paginated_projects(
params["startAt"] += PAGE_SIZE

async def get_single_issue(self, issue_key: str) -> dict[str, Any]:
async with self._create_session() as session:
async with session.get(f"{self.api_url}/issue/{issue_key}") as issue_response:
issue_response.raise_for_status()
return await issue_response.json()
issue_response = await asyncio.to_thread(
self.client.get, f"{self.api_url}/issue/{issue_key}"
)
issue_response.raise_for_status()
return issue_response.json()

async def get_paginated_issues(self) -> AsyncGenerator[list[dict[str, Any]], None]:
logger.info("Getting issues from Jira")
Expand Down
2 changes: 1 addition & 1 deletion integrations/jira/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jira"
version = "0.1.89-dev023"
version = "0.1.89-dev024"
description = "Integration to bring information from Jira into Port"
authors = ["Mor Paz <mor@getport.io>"]

Expand Down

0 comments on commit 979628e

Please sign in to comment.