CloudEnv
public class CloudEnv
A convenience class for obtaining environment variables that are mapped to JSON strings. It is mainly used for obtaining credentials for services so Swift applications can be written in a platform agnostic way.
There is a lot of information in the README
explaining how you can use cloudEnv
to access credentials for your services; more specific information is listed in the
API documentation below.
-
Name of the mappings file. This is set to “mappings.json”.
Declaration
Swift
public static let mappingsFile: String
-
Port number the application can listen to.
Declaration
Swift
public var port: Int { get }
-
URL that can be assigned to the application.
Declaration
Swift
public var url: String { get }
-
Initialize an instance of
CloudEnv
.Usage Example:
let cloudEnv = CloudEnv()
The example below shows how to load configuration from both a
mappings.json
file and a Cloud Foundry credentials file when neither are in the default directory.let cloudEnv = CloudEnv(mappingsFilePath: "resources/mappings", cloudFoundryFile: "resources cfresources/cf.json")
Declaration
Swift
public init(mappingsFilePath: String? = nil, cloudFoundryFile: String? = nil)
Parameters
mappingsFilePath
Optional. The path to the
mappings.json
file; this path should be relative to the root folder of the Swift application.cloudFoundryFile
Optional. The path to a JSON file that contains values for Cloud Foundry environment variables (mainly used for testing); this path should be relative to the current working directory (which in most cases is the folder of the Swift application).
-
Returns the corresponding JSON dictionary value in a string.
If the service you intend to use in your Swift application is not one which we explicitly support, you can leverage the generic
getString(name: String)
method to get the corresponding credentials.Usage Example:
let cloudEnv = CloudEnv() if let credentials = cloudEnv.getString(name: "service1-credentials") { // You can now get the corresponding values from the credentials string. // The result string will follow JSON notation. }
Declaration
Swift
public func getString(name: String) -> String?
Parameters
name
The key to lookup the environment variable.
-
Returns the corresponding dictionary value.
If the service you intend to use in your Swift application is not one which we explicitly support, you can leverage the generic
getDictionary(name: String)
method to get the corresponding credentials.Usage Example:
let cloudEnv = CloudEnv() if let credentials: [String:Any] = cloudEnv.getDictionary(name: "service1-credentials") { // You can now get the corresponding values from the dictionary. }
Declaration
Swift
public func getDictionary(name: String) -> [String : Any]?
Parameters
name
The key to lookup the environment variable.
-
Returns an AlertNotificationCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getAlertNotificationCredentials(name: "AlertNotificationKey")
Declaration
Swift
public func getAlertNotificationCredentials(name: String) -> AlertNotificationCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an AppIDCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getAppIDCredentials(name: "AppIDKey")
Declaration
Swift
public func getAppIDCredentials(name: String) -> AppIDCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an AutoScalingCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getAutoScalingCredentials(name: "AutoScalingKey")
Declaration
Swift
public func getAutoScalingCredentials(name: String) -> AutoScalingCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns a CloudFunctionsCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getCloudFunctionsCredentials(name: "CloudFunctionsKey")
Declaration
Swift
public func getCloudFunctionsCredentials(name: String) -> CloudFunctionsCredentials?
Parameters
name
The key to lookup the credentials.
-
Returns a CloudantCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getCloudantCredentials(name: "CloudantKey")
Declaration
Swift
public func getCloudantCredentials(name: String) -> CloudantCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an DB2Credentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getDB2Credentials(name: "DB2Key")
Declaration
Swift
public func getDB2Credentials(name: String) -> DB2Credentials?
Parameters
name
The key to lookup the environment variable.
-
Returns a HyperSecureDBaaSCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getHyperSecureDBaaSCredentials(name: "HyperSecureDBaaSKey")
Declaration
Swift
public func getHyperSecureDBaaSCredentials(name: String) -> HyperSecureDBaaSCredentials?
Parameters
name
The key to lookup the credentials object.
-
Returns a MongoDBCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getMongoDBCredentials(name: "MongoDBKey")
Declaration
Swift
public func getMongoDBCredentials(name: String) -> MongoDBCredentials?
Parameters
name
The key to lookup the credentials object.
-
Returns an MySQLCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getMySQLCredentials(name: "MySQLKey")
Declaration
Swift
public func getMySQLCredentials(name: String) -> MySQLCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns a NaturalLanguageUnderstandingCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getNaturalLanguageUnderstandingCredentials(name: "NaturalLanguageUnderstandingKey")
Declaration
Swift
public func getNaturalLanguageUnderstandingCredentials(name: String) -> NaturalLanguageUnderstandingCredentials?
Parameters
name
The key to lookup the credentials object.
-
Returns an ObjectStorageCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getObjectStorageCredentials(name: "ObjectStorageKey")
Declaration
Swift
public func getObjectStorageCredentials(name: String) -> ObjectStorageCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an PostgreSQLCredentials object with the corresponding credentials.
This function retrieve the credentials for “Compose for PostgreSQL”. If you want to retrieve the credentials for “Databases for PostgreSQL”, use getPostgreSQLDatabaseCredentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getPostgreSQLCredentials(name: "PostgreSQLKey")
- Tag: getPostgreSQLCredentials
Declaration
Swift
public func getPostgreSQLCredentials(name: String) -> PostgreSQLCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an PostgreSQLCredentials object with the corresponding credentials.
This function retrieve the credentials for “Databases for PostgreSQL”. If you want to retrieve the credentials for “Compose for PostgreSQL”, use getPostgreSQLCredentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getPostgreSQLDatabaseCredentials(name: "PostgreSQLKey")
- Tag: getPostgreSQLDatabaseCredentials
Declaration
Swift
public func getPostgreSQLDatabaseCredentials(name: String) -> PostgreSQLCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns a PushSDKCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getPushSDKCredentials(name: "PushNotificationKey")
Declaration
Swift
public func getPushSDKCredentials(name: String) -> PushSDKCredentials?
Parameters
name
The key to lookup the credentials.
-
Returns an RedisCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getRedisCredentials(name: "RedisKey")
Declaration
Swift
public func getRedisCredentials(name: String) -> RedisCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an WatsonAssistantCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getWatsonAssistantCredentials(name: "WatsonAssistantCredentialsKey")
Declaration
Swift
public func getWatsonAssistantCredentials(name: String) -> WatsonAssistantCredentials?
Parameters
name
The key to lookup the environment variable.
-
Returns an WeatherCompanyDataCredentials object with the corresponding credentials.
Usage Example:
let cloudEnv = CloudEnv() credentials = cloudEnv.getWeatherCompanyDataCredentials(name: "WeatherCompanyDataKey")
Declaration
Swift
public func getWeatherCompanyDataCredentials(name: String) -> WeatherCompanyDataCredentials?
Parameters
name
The key to lookup the environment variable.