commonlib-reader 1.0.7__tar.gz → 1.0.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: commonlib-reader
3
- Version: 1.0.7
3
+ Version: 1.0.8
4
4
  Summary: Reader for Equinor commonlib api.
5
5
  License: MIT
6
6
  Author: Åsmund Våge Fannemel
@@ -12,7 +12,7 @@ Classifier: Programming Language :: Python :: 3.10
12
12
  Classifier: Programming Language :: Python :: 3.11
13
13
  Classifier: Programming Language :: Python :: 3.12
14
14
  Classifier: Programming Language :: Python :: 3.13
15
- Requires-Dist: eq-api-connector (>=0.1.7,<0.2.0)
15
+ Requires-Dist: eq-api-connector (>=1.0.0,<2.0.0)
16
16
  Requires-Dist: pandas (>=2.1.4)
17
17
  Requires-Dist: requests (>=2.32.4)
18
18
  Requires-Dist: urllib3 (>=2.5.0)
@@ -0,0 +1,29 @@
1
+ from eq_api_connector import APIConnector
2
+
3
+ _connector = None
4
+
5
+
6
+ def get_connector() -> APIConnector:
7
+ """Get a singleton instance of the APIConnector.
8
+
9
+ Returns:
10
+ APIConnector: An instance of the APIConnector class.
11
+ """
12
+ global _connector
13
+ if _connector is None:
14
+ clientID = "728cfddd-b8e9-4ed7-b9a3-8f2fd5b8e79a"
15
+ scopes = ["37d598fc-da0f-46bd-949f-7107918d47a5/user_impersonation"]
16
+ _connector = APIConnector(client_id=clientID, scope=scopes)
17
+ _connector.set_url_prod("https://commonlibapi.equinor.com/")
18
+
19
+ return _connector
20
+
21
+
22
+ def set_connector(connector: APIConnector):
23
+ """Set the singleton instance of the APIConnector.
24
+
25
+ Args:
26
+ connector (APIConnector): An instance of the APIConnector class.
27
+ """
28
+ global _connector
29
+ _connector = connector
@@ -15,6 +15,7 @@ class Facility:
15
15
  Field (str) : Field identifier associated with the facility (if available).
16
16
  ParentFacility (str) : Parent facility identifier (if available).
17
17
  NPDID (str) : NPD ID (if available).
18
+ GovernmentalName (str) : Governmental facility name (if available).
18
19
 
19
20
  _cache (list of dict, optional):
20
21
  Class level cache to store facility data, to minimize data source access.
@@ -45,6 +46,7 @@ class Facility:
45
46
  self.Field = ""
46
47
  self.ParentFacility = ""
47
48
  self.NPDID = ""
49
+ self.GovernmentalName = ""
48
50
 
49
51
  for key, val in self._attributes.items():
50
52
  if key in self.__getattribute__("__dict__").keys():
@@ -57,7 +59,6 @@ class Facility:
57
59
  except (TypeError, ValueError):
58
60
  pass
59
61
 
60
-
61
62
  def isSTID(self):
62
63
  """
63
64
  Checks if the facility is marked for STID.
@@ -70,7 +71,7 @@ class Facility:
70
71
  return True
71
72
 
72
73
  return False
73
-
74
+
74
75
  def __str__(self):
75
76
  return f"Facility: {self.identity}, Name: {self.name}, Description: {self.description}"
76
77
 
@@ -3,12 +3,12 @@ from typing import Dict, List, Optional
3
3
  from urllib.parse import urljoin
4
4
 
5
5
  import requests
6
- from msal_bearer import BearerAuth, get_user_name
6
+
7
+ from commonlib_reader.connector import get_connector
7
8
 
8
9
 
9
10
  def get_library_names():
10
- url = urljoin(get_api_url(), "api/Library/NameList")
11
- library_names = get_json(url=url)
11
+ library_names = get_connector().get_json(url="/api/Library/NameList")
12
12
  library_names.sort()
13
13
  return library_names
14
14
 
@@ -31,67 +31,13 @@ def get_code_param(code, params=None):
31
31
  if params is None:
32
32
  params = {}
33
33
 
34
- url = urljoin(get_api_url(), f"api/Code/{code}")
35
- return get_json(url, params=params)
34
+ return get_connector().get_json(f"/api/Code/{code}", params=params)
36
35
 
37
36
 
38
37
  def query_sql(sql: str):
39
38
  return post_sql(sql=sql, take=0, skip=0)
40
39
 
41
40
 
42
- def get_api_url() -> str:
43
- """Get base url to api
44
-
45
- Returns:
46
- str: Base url to api
47
- """
48
- return "https://commonlibapi.equinor.com/"
49
-
50
-
51
- def get_auth() -> BearerAuth:
52
- """Get Bearerauth object including token for accessing api.
53
-
54
- Returns:
55
- BearerAuth: Object with access token for api.
56
- """
57
- tenantID = "3aa4a235-b6e2-48d5-9195-7fcf05b459b0"
58
- clientID = "728cfddd-b8e9-4ed7-b9a3-8f2fd5b8e79a"
59
- scopes = ["37d598fc-da0f-46bd-949f-7107918d47a5/user_impersonation"]
60
- auth = BearerAuth.get_auth(
61
- tenantID=tenantID,
62
- clientID=clientID,
63
- scopes=scopes,
64
- username=f"{get_user_name()}@equinor.com",
65
- )
66
-
67
- return auth
68
-
69
-
70
- def get_json(url: str, params=None) -> list:
71
- """Get json from api endpoint
72
-
73
- Args:
74
- url (str): Url to api endpoint to get.
75
- params (dict, optional): Dictionary of parameters to pass. Defaults to None.
76
-
77
- Returns:
78
- list: List of response from api
79
- """
80
- response = requests.get(url, auth=get_auth(), params=params)
81
- # response.raise_for_status()
82
- if response.status_code == 200:
83
- try:
84
- return response.json()
85
- except json.JSONDecodeError:
86
- print(
87
- f"Warning: {str(url)} returned successfully, but not with a valid json response"
88
- )
89
- else:
90
- print(f"Warning: {str(url)} returned status code {response.status_code}")
91
-
92
- return []
93
-
94
-
95
41
  def post_sql(sql: str, take: int = 100, skip: int = 0) -> list:
96
42
  """Get json from api/sql endpoint accepting sql queries.
97
43
 
@@ -103,9 +49,12 @@ def post_sql(sql: str, take: int = 100, skip: int = 0) -> list:
103
49
  Returns:
104
50
  list: List of response from api
105
51
  """
106
- url = urljoin(get_api_url(), "api/sql")
52
+
53
+ url = urljoin(get_connector().get_url(), "api/sql")
107
54
  response = requests.post(
108
- url=url, json={"query": sql, "take": take, "skip": skip}, auth=get_auth()
55
+ url=url,
56
+ json={"query": sql, "take": take, "skip": skip},
57
+ headers={"Authorization": "Bearer " + get_connector().get_token()},
109
58
  )
110
59
  if response.status_code == 200:
111
60
  try:
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "commonlib-reader"
3
- version = "1.0.7"
3
+ version = "1.0.8"
4
4
  description = "Reader for Equinor commonlib api."
5
5
  authors = [
6
6
  { name = "Åsmund Våge Fannemel", email = "asmf@equinor.com" }
@@ -13,7 +13,7 @@ dependencies = [
13
13
  "pandas>=2.1.4",
14
14
  "requests>=2.32.4",
15
15
  "urllib3>=2.5.0", # Pinned to avoid security issues in some older versions
16
- "eq-api-connector (>=0.1.7,<0.2.0)"
16
+ "eq-api-connector (>=1.0.0,<2.0.0)"
17
17
  ]
18
18
 
19
19
  [tool.poetry.group.test.dependencies]