Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[uss_qualifier][draft stacked] Dss0030 concurrent dss queries with subscriptions #12

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions monitoring/monitorlib/fetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from enum import Enum
from urllib.parse import urlparse

import aiohttp
import flask
from loguru import logger
import requests
Expand Down Expand Up @@ -145,6 +146,20 @@ def describe_response(resp: requests.Response) -> ResponseDescription:
return ResponseDescription(**kwargs)


def describe_aiohttp_response(
status: int, headers: Dict, resp_json: Dict, duration: datetime.timedelta
) -> ResponseDescription:
kwargs = {
"code": status,
"headers": headers,
"elapsed_s": duration.total_seconds(),
"reported": StringBasedDateTime(datetime.datetime.utcnow()),
"json": resp_json,
}

return ResponseDescription(**kwargs)


def describe_flask_response(resp: flask.Response, elapsed_s: float):
headers = {k: v for k, v in resp.headers.items()}
kwargs = {
Expand Down
44 changes: 37 additions & 7 deletions monitoring/monitorlib/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
from typing import Dict, List, Optional
import urllib.parse
from aiohttp import ClientSession
from aiohttp import ClientSession, ClientResponse

import jwt
import requests
Expand Down Expand Up @@ -189,19 +189,39 @@ def adjust_request_kwargs(self, url, method, kwargs):
kwargs["timeout"] = self.timeout_seconds
return kwargs

async def put(self, url, **kwargs):
async def put_with_headers(self, url, **kwargs):
"""Issues a PUT and returns the status code, headers, and JSON body."""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "PUT", kwargs)
async with self._client.put(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)

async def get(self, url, **kwargs):
async def put(self, url, **kwargs):
"""Issues a PUT and returns the status code and JSON body."""
(status, _, json) = await self.put_with_headers(url, **kwargs)
return status, json

async def get_with_headers(self, url, **kwargs):
"""Issues a GET and returns the status code, headers, and JSON body."""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "GET", kwargs)
async with self._client.get(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)

async def get(self, url, **kwargs):
"""Issues a GET and returns the status code and JSON body."""
(status, _, json) = await self.get_with_headers(url, **kwargs)
return status, json

async def post(self, url, **kwargs):
url = self._prefix_url + url
Expand All @@ -210,12 +230,22 @@ async def post(self, url, **kwargs):
async with self._client.post(url, **kwargs) as response:
return response.status, await response.json()

async def delete(self, url, **kwargs):
async def delete_with_headers(self, url, **kwargs):
"""Issues a DELETE and returns the status code, headers, and JSON body."""
url = self._prefix_url + url
if "auth" not in kwargs:
kwargs = self.adjust_request_kwargs(url, "DELETE", kwargs)
async with self._client.delete(url, **kwargs) as response:
return response.status, await response.json()
return (
response.status,
{k: v for k, v in response.headers.items()},
await response.json(),
)

async def delete(self, url, **kwargs):
"""Issues a DELETE and returns the status code and JSON body."""
(status, _, json) = await self.delete_with_headers(url, **kwargs)
return status, json


def default_scopes(scopes: List[str]):
Expand Down
Loading
Loading