Skip to content

Commit d419539

Browse files
authored
Merge pull request #131 from equinor/use-new-aggregation-endpoint
add aggregation client
2 parents 53d0af7 + 91256e2 commit d419539

4 files changed

Lines changed: 122 additions & 1 deletion

File tree

src/sumo/wrapper/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ._call_sumo_api import CallSumoApi
2+
from ._sumo_aggregation_client import SumoAggregationClient
23
from .sumo_client import SumoClient
34

4-
__all__ = ["CallSumoApi", "SumoClient"]
5+
__all__ = ["CallSumoApi", "SumoClient", "SumoAggregationClient"]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

src/sumo/wrapper/config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,26 @@
2424
TENANT_ID = "3aa4a235-b6e2-48d5-9195-7fcf05b459b0"
2525

2626
AUTHORITY_HOST_URI = "https://login.microsoftonline.com"
27+
28+
AGG_APP_REGISTRATION = {
29+
"prod": {
30+
"CLIENT_ID": "6311fb13-81e5-4393-ab8b-13b369fab92d",
31+
"RESOURCE_ID": "784f0baa-013f-4fbf-b28a-76d72b5e8d5c",
32+
},
33+
"test": {
34+
"CLIENT_ID": "6311fb13-81e5-4393-ab8b-13b369fab92d",
35+
"RESOURCE_ID": "784f0baa-013f-4fbf-b28a-76d72b5e8d5c",
36+
},
37+
"dev": {
38+
"CLIENT_ID": "6311fb13-81e5-4393-ab8b-13b369fab92d",
39+
"RESOURCE_ID": "4d631410-6983-4fdb-b20c-2e2ba0c0d506",
40+
},
41+
"preview": {
42+
"CLIENT_ID": "6311fb13-81e5-4393-ab8b-13b369fab92d",
43+
"RESOURCE_ID": "4d631410-6983-4fdb-b20c-2e2ba0c0d506",
44+
},
45+
"localhost": {
46+
"CLIENT_ID": "6311fb13-81e5-4393-ab8b-13b369fab92d",
47+
"RESOURCE_ID": "4d631410-6983-4fdb-b20c-2e2ba0c0d506",
48+
},
49+
}

src/sumo/wrapper/sumo_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ._new_auth import NewAuth
88
from ._request_error import raise_request_error_exception
99
from ._blob_client import BlobClient
10+
from ._sumo_aggregation_client import SumoAggregationClient
1011

1112
logger = logging.getLogger("sumo.wrapper")
1213

@@ -70,6 +71,10 @@ def __init__(
7071
else:
7172
self.base_url = f"https://main-sumo-{env}.radix.equinor.com/api/v1"
7273

74+
self.agg_client = SumoAggregationClient(
75+
env=env, interactive=interactive, verbosity=verbosity
76+
)
77+
7378
def authenticate(self) -> str:
7479
"""Authenticate to Sumo.
7580
@@ -362,3 +367,14 @@ def delete(self, path: str) -> dict:
362367
raise_request_error_exception(response.status_code, response.text)
363368

364369
return response.json()
370+
371+
def get_aggregate(self, json: dict):
372+
"""Gets a surface aggregation using SumoAggregationClient:get_aggregate
373+
374+
Args:
375+
json: Json payload
376+
377+
Returns:
378+
Sumo aggregate response object
379+
"""
380+
return self.agg_client.get_aggregate(json)

0 commit comments

Comments
 (0)