1- import os
21import base64
32import hashlib
4- import json
53import hmac
4+ import json
5+ import os
66import time
77from pathlib import Path
88from urllib .parse import quote_plus , urlencode
9+
910from werkzeug .exceptions import NotFound
11+
1012from microSALT import app , logger
11- from microSALT .utils .pubmlst .exceptions import PUBMLSTError , PathResolutionError , CredentialsFileNotFound , InvalidCredentials , SaveSessionError , InvalidURLError
1213from microSALT .utils .pubmlst .constants import Encoding , url_map
14+ from microSALT .utils .pubmlst .exceptions import (
15+ CredentialsFileNotFound ,
16+ InvalidCredentials ,
17+ InvalidURLError ,
18+ PathResolutionError ,
19+ PUBMLSTError ,
20+ SaveSessionError ,
21+ )
1322
1423BASE_WEB = "https://pubmlst.org/bigsdb"
1524BASE_API = "https://rest.pubmlst.org"
2130pubmlst_config = app .config ["pubmlst" ]
2231folders_config = app .config ["folders" ]
2332
33+
2434def get_path (config , config_key : str ):
2535 """Get and expand the file path from the configuration."""
2636 try :
@@ -41,8 +51,7 @@ def load_auth_credentials():
4151 """Load client ID, client secret, access token, and access secret from credentials file."""
4252 try :
4353 credentials_file = os .path .join (
44- get_path (folders_config , credentials_path_key ),
45- pubmlst_auth_credentials_file_name
54+ get_path (folders_config , credentials_path_key ), pubmlst_auth_credentials_file_name
4655 )
4756
4857 if not os .path .exists (credentials_file ):
@@ -81,37 +90,7 @@ def load_auth_credentials():
8190 raise
8291 except Exception as e :
8392 raise PUBMLSTError ("An unexpected error occurred while loading credentials: {e}" )
84-
85-
86- def generate_oauth_header (url : str , oauth_consumer_key : str , oauth_consumer_secret : str , oauth_token : str , oauth_token_secret : str ):
87- """Generate the OAuth1 Authorization header."""
88- oauth_timestamp = str (int (time .time ()))
89- oauth_nonce = base64 .urlsafe_b64encode (os .urandom (32 )).decode (Encoding .UTF8 .value ).strip ("=" )
90- oauth_signature_method = "HMAC-SHA1"
91- oauth_version = "1.0"
92-
93- oauth_params = {
94- "oauth_consumer_key" : oauth_consumer_key ,
95- "oauth_token" : oauth_token ,
96- "oauth_signature_method" : oauth_signature_method ,
97- "oauth_timestamp" : oauth_timestamp ,
98- "oauth_nonce" : oauth_nonce ,
99- "oauth_version" : oauth_version ,
100- }
101-
102- params_encoded = urlencode (sorted (oauth_params .items ()))
103- base_string = f"GET&{ quote_plus (url )} &{ quote_plus (params_encoded )} "
104- signing_key = f"{ oauth_consumer_secret } &{ oauth_token_secret } "
105-
106- hashed = hmac .new (signing_key .encode (Encoding .UTF8 .value ), base_string .encode (Encoding .UTF8 .value ), hashlib .sha1 )
107- oauth_signature = base64 .b64encode (hashed .digest ()).decode (Encoding .UTF8 .value )
108-
109- oauth_params ["oauth_signature" ] = oauth_signature
110-
111- auth_header = "OAuth " + ", " .join (
112- [f'{ quote_plus (k )} ="{ quote_plus (v )} "' for k , v in oauth_params .items ()]
113- )
114- return auth_header
93+
11594
11695def save_session_token (db : str , token : str , secret : str , expiration_date : str ):
11796 """Save session token, secret, and expiration to a JSON file for the specified database."""
@@ -123,8 +102,7 @@ def save_session_token(db: str, token: str, secret: str, expiration_date: str):
123102 }
124103
125104 credentials_file = os .path .join (
126- get_path (folders_config , credentials_path_key ),
127- pubmlst_session_credentials_file_name
105+ get_path (folders_config , credentials_path_key ), pubmlst_session_credentials_file_name
128106 )
129107
130108 if os .path .exists (credentials_file ):
@@ -141,16 +119,15 @@ def save_session_token(db: str, token: str, secret: str, expiration_date: str):
141119 with open (credentials_file , "w" ) as f :
142120 json .dump (all_sessions , f , indent = 4 )
143121
144- logger .debug (
145- f"Session token for database '{ db } ' saved to '{ credentials_file } '."
146- )
122+ logger .debug (f"Session token for database '{ db } ' saved to '{ credentials_file } '." )
147123 except (IOError , OSError ) as e :
148124 raise SaveSessionError (db , f"I/O error: { e } " )
149125 except ValueError as e :
150126 raise SaveSessionError (db , f"Invalid data format: { e } " )
151127 except Exception as e :
152128 raise SaveSessionError (db , f"Unexpected error: { e } " )
153129
130+
154131def parse_pubmlst_url (url : str ):
155132 """
156133 Match a URL against the URL map and return extracted parameters.
0 commit comments