In order to be able to perform their tasks, many classes in the SDK rely on the global
availability of several objects. For example, the URL and credentials used to communicate
with the platform are defined in an instance of the connect.Config
class, while the
ability to perform Http requests is performed by an instance of a class that implements
the connect.api.IApiClient
interface.
Since these dependencies must be globally available, the Env
class contains static
methods to obtain the default instances of these classes from anywhere. To minimize the
side-effects that can be caused by changes in the values of global objects in a program,
all environment objects are immutable, providing a side-effect free context for the program
to run.
All objects returned here are lazy-initialized, meaning that they are not created until they
are requested. In order to provide the connector configuration, a call to
Env.initConfig
or Env.loadConfig
can be provided at the top of the program.
Otherwise, the configuration will be automatically loaded from the "config.json" file.
Likewise, the Logger
can be initialized with a call to Env.initLogger
, assuming it has not
been done yet.
Many of the objects returned by this class are defined in a public interface, with a default implementation provided by the environment. This is because when unit testing, these classes get replaced through dependency injection by mocked ones, allowing to a sandboxed unit testing environment.
Static methods
staticgetApiClient():IApiClient
Returns:
The API Client, used to make all low level Http requests to the platform.
Throws:
String | If a class implementing the IApiClient interface cannot be instanced. |
---|
staticgetConfig():Config
Returns the configuration object. If it is not initialized, it tries to initialize it from the file "config.json".
Returns:
The environment config.
Throws:
Exception | If the instance is not initialized and the file "config.json" cannot be parsed. |
---|
staticgetLogger():Logger
Returns the logger object. If it is not initialized, it will initialize it in the level
Info
with the path "logs".
Returns:
The environment logger.
staticgetLoggerForRequest(request:Null<IdModel>):Logger
Get logger for given request, if it doesnt exists it will be created and context specified.
staticinitConfig(apiUrl:String, apiKey:String, products:Collection<String>):Void
Initializes the configuration object. It must have not been previously configured.
Parameters:
apiUrl | Value for the apiUrl property. |
---|---|
apiKey | Value for the apiKey property. |
products | Collection of product ids that can be processed with this configuration. |
Throws:
String | If the configuration is already initialized. |
---|
staticinitDefaultQuery(query:Query):Void
Initializes the default Query
. This query can contain common filters and be easily
embedded in any other query by using the Query.default
method.
Parameters:
query | The default |
---|
staticinitLogger(config:LoggerConfig):Void
Initializes the logger. It must have not been previously configured.
Parameters:
config | The configuration of the logger. |
---|
staticisConfigInitialized():Bool
Returns:
true
if config has already been initialized, false
otherwise.
staticisDefaultQueryInitialized():Bool
Returns:
true
if default query has already been set, false
otherwise;
staticisLoggerInitialized():Bool
Returns:
true
if logger has already been initialized, false
otherwise.
staticloadConfig(filename:String):Void
Initializes the configuration object using a JSON file. It must have not been previously configured.
Parameters:
filename | Name of the configuration JSON file to parse. |
---|
Throws:
Exception | If the file cannot be parsed. |
---|---|
String | If the configuration is already initialized. |