11import logging
22import platform
3+ from typing import Optional
34
45from . import endpoints
56from .api_modes import ApiModes
67from .context import Context
8+ from .credentials import (
9+ MerchantIdCredentials ,
10+ AlmaSessionCredentials ,
11+ ApiKeyCredentials ,
12+ Credentials ,
13+ )
714from .version import __version__ as alma_version
815
916
1017class Client :
1118 SANDBOX_API_URL = "https://api.sandbox.getalma.eu"
1219 LIVE_API_URL = "https://api.getalma.eu"
1320
14- def __init__ (self , api_key , ** options ):
15- if not api_key :
16- raise ValueError ("An API key is required to instantiate a new Client" )
21+ @classmethod
22+ def with_api_key (cls , api_key : str , ** options ):
23+ return cls (credentials = ApiKeyCredentials (api_key ), ** options )
24+
25+ @classmethod
26+ def with_merchant_id (cls , merchant_id : str , mode : ApiModes = ApiModes .LIVE , ** options ):
27+ return cls (credentials = MerchantIdCredentials (mode , merchant_id ), ** options )
28+
29+ @classmethod
30+ def with_alma_session (
31+ cls ,
32+ session_id : str ,
33+ cookie_name : str = "alma_sess" ,
34+ mode : ApiModes = ApiModes .LIVE ,
35+ ** options
36+ ):
37+ return cls (credentials = AlmaSessionCredentials (mode , session_id , cookie_name ), ** options )
38+
39+ def __init__ (
40+ self ,
41+ api_key : Optional [str ] = None ,
42+ credentials : Optional [Credentials ] = None ,
43+ mode : Optional [ApiModes ] = None ,
44+ ** kwargs
45+ ):
46+ """
47+ Create a new instance of the Alma API Client.
48+
49+ It is recommended to use one the convenience methods instead of the default constructor:
50+ - Client.with_api_key
51+ - Client.with_merchant_id
52+ - Client.with_alma_session
53+
54+ :param api_key: Deprecated - use Client.with_api_key("<api_key>") instead
55+ :type api_key: str
56+
57+ :param credentials A `Credentials` instance to be used to configure requests made to
58+ the API. This would typically be set by one of the convenience
59+ methods mentioned above.
60+ :type credentials Credentials
61+
62+ :param mode Deprecated. Use `mode` param of convenience methods above
63+ API mode to be used: either ApiModes.LIVE or ApiModes.TEST
64+ :type mode ApiModes
65+
66+ :keyword logger A logger instance to be used instead of the default one
67+ :keyword api_root root URL(s) to call Alma's API at.
68+ You probably don't want to change it!
69+
70+ Expected types:
71+ -------------
72+ str: the provided URL will be used for both LIVE and TEST modes
73+ dict: must have two keys, ApiModes.LIVE and ApiModes.TEST, each
74+ value must be the URL to be used for each mode
75+
76+ """
77+ if isinstance (credentials , ApiKeyCredentials ):
78+ api_key = credentials .api_key
79+
80+ if not credentials :
81+ if not api_key :
82+ raise ValueError ("Valid credentials are required to instantiate a new Client" )
83+
84+ # Backward compatibility with the older init method
85+ credentials = ApiKeyCredentials (api_key )
1786
1887 options = {
1988 "api_root" : {ApiModes .TEST : self .SANDBOX_API_URL , ApiModes .LIVE : self .LIVE_API_URL },
20- "mode" : ApiModes . LIVE if api_key . startswith ( "sk_live" ) else ApiModes . TEST ,
89+ "mode" : mode if mode is not None else credentials . mode ,
2190 "logger" : logging .getLogger ("alma-python-client" ),
22- ** options ,
91+ "credentials" : credentials ,
92+ ** kwargs ,
2393 }
2494
2595 if type (options ["api_root" ]) is str :
@@ -37,10 +107,9 @@ def __init__(self, api_key, **options):
37107 )
38108 )
39109
40- self .context = Context (api_key , options )
41-
110+ self .context = Context (options )
42111 self .init_user_agent ()
43- self ._endpoints = {}
112+ self ._endpoints = {} # type: ignore
44113
45114 def add_user_agent_component (self , component , version ):
46115 self .context .add_user_agent_component (component , version )
0 commit comments