Metadata-Version: 2.3
Name: eq-api-connector
Version: 1.2.3
Summary: API connector (API SDK) to connect to public client app registrations using delegated permissions (MSAL).
Author: Åsmund Våge Fannemel
Author-email: Åsmund Våge Fannemel <asmf@equinor.com>
License: MIT
Requires-Dist: requests>=2.34.2,<3.0.0
Requires-Dist: msal-bearer>=1.6.1,<2.0.0
Requires-Python: >=3.10, <4.0
Description-Content-Type: text/markdown

# eq-api-connector
A small python package to handle authentication and act as a generic api sdk.

It supports apis Public client applications using [acquire_token_interactive](https://learn.microsoft.com/en-us/python/api/msal/msal.application.publicclientapplication?view=msal-py-latest#msal-application-publicclientapplication-acquire-token-interactive) and contains a few basic endpoint accessors. 

# Install
Install from pypi ```pip install eq-api-connector``` or clone and use ```uv sync``` as a developer.

# Usage
See the dummy example below

```
from eq_api_connector import APIConnector

url_prod = "https://api.gateway.equinor.com/dummy/v1"
client_id = "insert_your_client_id_here"  # IOC SME monitoring SDK
scope = ["34de7368-10f7-4c34-a4df-928156065f2c/ReadWrite"]

c = APIConnector(client_id=client_id, scope=scope)
c.url_prod = url_prod

model_owners = c.get_json("/example-endpoint")
````

If you already have an access token, pass it with `token`:

```
access_token = get_access_token_from_your_auth_flow()
c = APIConnector(token=access_token)
```

When a token is supplied, `APIConnector` uses it as-is and does not check its expiry or refresh it. The caller is responsible for obtaining and supplying a fresh token when it expires.

Typically packages using eq-api-connector should subclass the APIConnector or keep a singleton a reference.

## Optional API Subscription Key (APIM)

Some APIs require an API subscription key (called an APIM key in Equinor) in
addition to MSAL sign-in. Pass it as `subscription_key`:

```python
import os
from eq_api_connector import APIConnector

c = APIConnector(
	client_id=client_id,
	scope=scope,
	subscription_key=os.getenv("APIM_SUBSCRIPTION_KEY"),
)
c.url_prod = url_prod
```

When set, the key is sent as the `Ocp-Apim-Subscription-Key` header for all
requests, including concurrent reads, downloads and uploads. Omitting it leaves
the existing bearer-token authentication unchanged; MSAL authentication is still
required. The connector does not automatically read environment variables or
retrieve keys from Azure Key Vault.

Use `c.subscription_key = new_key` to replace the key, or
`c.subscription_key = None` (or `""`) to remove it. Only use the connector with
trusted API URLs and redirect destinations, since its session carries the key.
Store keys outside source code, for example in an environment variable or a
secret store. Do not commit keys or log request headers containing them.

