Skip to content
Open
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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ The constructor expects an environment object:
```
env = {
API_URL: 'API_URL',
IDENTITY_POOL_ID: 'IDENTITY_POOL_ID',
USER_POOL_ID: 'USER_POOL_ID',
CLIENT_ID: 'CLIENT_ID',
REGION: 'REGION',
};
```

The `API_URL` is `"mi.scribelabs.ai/v1"`.
The `API_URL` is `"mi.scribelabs.ai/v2"`.

The `REGION` is `"eu-west-2"`.

Expand All @@ -36,9 +35,8 @@ Contact Scribe to obtain other details required for authentication.
from ScribeMi import MI

client = MI({
'API_URL': 'mi.scribelabs.ai/v1',
'API_URL': 'mi.scribelabs.ai/v2',
'REGION': 'eu-west-2',
'IDENTITY_POOL_ID': 'Contact Scribe for authentication details',
'USER_POOL_ID': 'Contact Scribe for authentication details',
'CLIENT_ID': 'Contact Scribe for authentication details',
})
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ alabaster==0.7.16
apeye==1.4.1
apeye-core==1.1.5
autodocsumm==0.2.12
aws-requests-auth==0.4.3
Babel==2.14.0
beautifulsoup4==4.12.3
boto3==1.34.93
Expand Down
74 changes: 23 additions & 51 deletions scribemi/ScribeMi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from io import BytesIO
from typing import BinaryIO, Union
from scribeauth import ScribeAuth
from aws_requests_auth.aws_auth import AWSRequestsAuth
from datetime import datetime
from typing_extensions import TypedDict, Optional, List
from hashlib import md5
Expand All @@ -19,10 +18,6 @@ class Env(TypedDict):
"""
The URL of the API.
"""
IDENTITY_POOL_ID: str
"""
The ID of the identity pool.
"""
USER_POOL_ID: str
"""
The ID of the user pool.
Expand Down Expand Up @@ -248,6 +243,14 @@ class InvalidFiletypeException(Exception):
pass


class MissingFilenameException(Exception):
"""
Exception raised when trying to upload a file without specifying a filename
"""

pass


class MI:
def __init__(self, env):
"""
Expand All @@ -261,12 +264,9 @@ def __init__(self, env):
{
"client_id": env["CLIENT_ID"],
"user_pool_id": env["USER_POOL_ID"],
"identity_pool_id": env["IDENTITY_POOL_ID"],
}
)
self.tokens = None
self.user_id = None
self.request_auth = None

def authenticate(self, param):
"""
Expand All @@ -285,50 +285,23 @@ def authenticate(self, param):
:type refresh_token: str
"""
self.tokens = self.auth_client.get_tokens(**param)
id_token = self.tokens.get("id_token")
if id_token != None:
self.user_id = self.auth_client.get_federated_id(id_token)
self.credentials = self.auth_client.get_federated_credentials(
self.user_id, id_token
)
host = self.env["API_URL"].split("/")[0]
self.request_auth = AWSRequestsAuth(
aws_access_key=self.credentials["AccessKeyId"],
aws_secret_access_key=self.credentials["SecretKey"],
aws_token=self.credentials["SessionToken"],
aws_host=host,
aws_region=self.env["REGION"],
aws_service="execute-api",
)
else:
access_token = self.tokens.get("access_token")
if access_token == None:
raise UnauthenticatedException("Authentication failed")

def reauthenticate(self):
"""
To reauthenticate a user without sending parameters. Must be called after authenticate.
"""
if self.tokens == None or self.user_id == None:
if self.tokens == None:
raise UnauthenticatedException("Must authenticate before reauthenticating")
refresh_token = self.tokens.get("refresh_token")
if refresh_token != None:
self.tokens = self.auth_client.get_tokens(refresh_token=refresh_token)
id_token = self.tokens.get("id_token")
if id_token != None:
self.credentials = self.auth_client.get_federated_credentials(
self.user_id, id_token
)
host = self.env["API_URL"].split("/")[0]
self.request_auth = AWSRequestsAuth(
aws_access_key=self.credentials["AccessKeyId"],
aws_secret_access_key=self.credentials["SecretKey"],
aws_token=self.credentials["SessionToken"],
aws_host=host,
aws_region=self.env["REGION"],
aws_service="execute-api",
)
else:
raise UnauthenticatedException("Authentication failed")
else:
if refresh_token == None:
raise UnauthenticatedException("Reuthentication failed (no refresh token)")

self.tokens = self.auth_client.get_tokens(refresh_token=refresh_token)
access_token = self.tokens.get("access_token")
if access_token == None:
raise UnauthenticatedException("Authentication failed")

def call_endpoint(self, method, path, data=None, params=None):
Expand All @@ -348,18 +321,14 @@ def call_endpoint(self, method, path, data=None, params=None):
:return: JSON response.
:rtype: str
"""
if self.request_auth == None:
if self.tokens == None:
raise UnauthenticatedException("Not authenticated")
if self.credentials["Expiration"] < datetime.now(
self.credentials["Expiration"].tzinfo
):
self.reauthenticate()
res = requests.request(
method=method,
url="https://{host}{path}".format(host=self.env["API_URL"], path=path),
params=params,
json=data,
auth=self.request_auth,
headers={"Authorization": self.tokens["access_token"]},
)
if res.status_code == 200:
return json.loads(res.text)
Expand Down Expand Up @@ -452,7 +421,7 @@ def submit_task(
To submit a task.

:param file_or_filename: file to upload -- Union[str, BytesIO, BinaryIO]
:param params: SubmitTaskParams -- Dictionary {filetype: “str”, filename: “Optional[str]”, companyname: “Optional[str]”}
:param params: SubmitTaskParams -- Dictionary {filetype: “str”, md5checksum: “str”, filename: “Optional[str]”, companyname: “Optional[str]”}

:return: jobid of the task.
:rtype: str
Expand All @@ -466,6 +435,9 @@ def submit_task(
if isinstance(file_or_filename, str) and params.get("filename") == None:
params["filename"] = file_or_filename

if params["filename"] == None:
raise MissingFilenameException()

if isinstance(file_or_filename, str):
with open(file_or_filename, "rb") as file:
file_content = file.read()
Expand Down
1 change: 0 additions & 1 deletion tests/test_scribemi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

env = {
"API_URL": os.environ["API_URL"],
"IDENTITY_POOL_ID": os.environ["IDENTITY_POOL_ID"],
"USER_POOL_ID": os.environ["USER_POOL_ID"],
"CLIENT_ID": os.environ["CLIENT_ID"],
"REGION": os.environ["REGION"],
Expand Down