Last active
February 19, 2017 18:17
-
-
Save sclark39/e611331b4fbb8ad32832275ec551819d to your computer and use it in GitHub Desktop.
Simple extension to AWSAPIGatewayClient rewritten for Swift 3 to automatically get configuration, and have a shared lazy instantiated client. Other code will call invokeHTTPRequest directly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
Licensed under the Apache License, Version 2.0 (the "License"). | |
You may not use this file except in compliance with the License. | |
A copy of the License is located at | |
http://aws.amazon.com/apache2.0 | |
or in the "license" file accompanying this file. This file is distributed | |
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | |
express or implied. See the License for the specific language governing | |
permissions and limitations under the License. | |
*/ | |
import AWSCore | |
import AWSAPIGateway | |
extension AWSAPIGatewayClient | |
{ | |
static let AWSInfoClientKey = "ApiClientKey" | |
static let AWSPath = "https://apipath" | |
/** | |
Returns the singleton service client. If the singleton object does not exist, the SDK instantiates the default service client with `defaultServiceConfiguration` from `AWSServiceManager.defaultServiceManager()`. The reference to this object is maintained by the SDK, and you do not need to retain it manually. | |
If you want to enable AWS Signature, set the default service configuration in `func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)` | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YourIdentityPoolId") | |
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider) | |
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration | |
return true | |
} | |
Then call the following to get the default service client: | |
let serviceClient = AWSAPIGatewayClient.default() | |
Alternatively, this configuration could also be set in the `info.plist` file of your app under `AWS` dictionary with a configuration dictionary by name `AWSAPIGatewayClient`. | |
@return The default service client. | |
*/ | |
static let _defaultClient:AWSAPIGatewayClient = | |
{ | |
var serviceConfiguration: AWSServiceConfiguration? = nil | |
let serviceInfo = AWSInfo.default().defaultServiceInfo(AWSInfoClientKey) | |
if let serviceInfo = serviceInfo { | |
serviceConfiguration = AWSServiceConfiguration(region: serviceInfo.region, credentialsProvider: serviceInfo.cognitoCredentialsProvider) | |
} else if (AWSServiceManager.default().defaultServiceConfiguration != nil) { | |
serviceConfiguration = AWSServiceManager.default().defaultServiceConfiguration | |
} else { | |
serviceConfiguration = AWSServiceConfiguration(region: .unknown, credentialsProvider: nil) | |
} | |
return AWSAPIGatewayClient(configuration: serviceConfiguration!) | |
}() | |
public class func `default`() -> AWSAPIGatewayClient { return _defaultClient } | |
convenience init(configuration: AWSServiceConfiguration) { | |
self.init() | |
self.configuration = configuration.copy() as! AWSServiceConfiguration | |
var URLString: String = AWSAPIGatewayClient.AWSPath | |
if URLString.hasSuffix("/") | |
{ | |
URLString.remove(at: URLString.index(before: URLString.endIndex)) | |
} | |
self.configuration.endpoint = AWSEndpoint(region: configuration.regionType, service: .apiGateway, url: URL( string:URLString )!) | |
let signer: AWSSignatureV4Signer = AWSSignatureV4Signer(credentialsProvider: configuration.credentialsProvider, endpoint: self.configuration.endpoint) | |
if let endpoint = self.configuration.endpoint | |
{ | |
self.configuration.baseURL = endpoint.url | |
} | |
self.configuration.requestInterceptors = [AWSNetworkingRequestInterceptor(), signer] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment