|
| 1 | +import logging |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from ._new_auth import NewAuth |
| 6 | +from ._request_error import raise_request_error_exception |
| 7 | +from .config import AGG_APP_REGISTRATION, TENANT_ID |
| 8 | + |
| 9 | +logger = logging.getLogger("sumo.wrapper") |
| 10 | + |
| 11 | + |
| 12 | +class SumoAggregationClient: |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + env: str, |
| 16 | + interactive: bool = False, |
| 17 | + verbosity: str = "CRITICAL", |
| 18 | + ): |
| 19 | + """Initialize a new Sumo Aggregation object |
| 20 | + Args: |
| 21 | + env: Sumo environment |
| 22 | + token: Access token or refresh token. |
| 23 | + interactive: Enable interactive authentication (in browser). |
| 24 | + If not enabled, code grant flow will be used. |
| 25 | + verbosity: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
| 26 | + """ |
| 27 | + |
| 28 | + logger.setLevel(verbosity) |
| 29 | + |
| 30 | + if env not in AGG_APP_REGISTRATION: |
| 31 | + raise ValueError(f"Invalid environment: {env}") |
| 32 | + |
| 33 | + self.auth = NewAuth( |
| 34 | + client_id=AGG_APP_REGISTRATION[env]["CLIENT_ID"], |
| 35 | + resource_id=AGG_APP_REGISTRATION[env]["RESOURCE_ID"], |
| 36 | + tenant_id=TENANT_ID, |
| 37 | + interactive=interactive, |
| 38 | + verbosity=verbosity, |
| 39 | + ) |
| 40 | + |
| 41 | + if env == "localhost": |
| 42 | + self.base_url = ( |
| 43 | + "https://main-sumo-surface-aggregation-service-preview" |
| 44 | + + ".radix.equinor.com" |
| 45 | + ) |
| 46 | + else: |
| 47 | + self.base_url = ( |
| 48 | + f"https://main-sumo-surface-aggregation-service-{env}" |
| 49 | + + ".radix.equinor.com" |
| 50 | + ) |
| 51 | + |
| 52 | + def get_aggregate(self, json: dict): |
| 53 | + """ |
| 54 | + Performs a POST-request to Sumo Aggregation API /fastaggregation. |
| 55 | +
|
| 56 | + Takes json with objects to aggregate and aggregation operation |
| 57 | + as payload |
| 58 | + Args: |
| 59 | + json: Json payload |
| 60 | + Returns: |
| 61 | + Sumo aggregate response object |
| 62 | + """ |
| 63 | + token = self.auth.get_token() |
| 64 | + |
| 65 | + headers = { |
| 66 | + "Content-Type": "application/json", |
| 67 | + "authorization": f"Bearer {token}", |
| 68 | + "Content-Length": str(len(json)), |
| 69 | + } |
| 70 | + |
| 71 | + try: |
| 72 | + response = requests.post( |
| 73 | + f"{self.base_url}/fastaggregation", json=json, headers=headers |
| 74 | + ) |
| 75 | + except requests.exceptions.ProxyError as err: |
| 76 | + raise_request_error_exception(503, err) |
| 77 | + |
| 78 | + if not response.ok: |
| 79 | + raise_request_error_exception(response.status_code, response.text) |
| 80 | + |
| 81 | + return response |
0 commit comments