aws-amplify / aws-sdk-ios

AWS SDK for iOS. For more information, see our web site:
https://aws-amplify.github.io/docs
Other
1.68k stars 885 forks source link

No 'defaultClient' in API Gateway-generated iOS Swift SDK #1357

Closed motivus closed 5 years ago

motivus commented 5 years ago

Describe the bug There is no defaultClient in the API Gateway-generated Client.swift as per the directions in README.md.

To Reproduce Steps to reproduce the behavior:

I followed this REST API tutorial in the API Gateway docs. It's a simple API named 'calc' that calls a Lambda function that performs the calculation. It works perfectly in test invocations via the APIG console.

I generated an iOS Swift SDK as instructed here in the docs and followed the instructions for use in my simple iOS project. The problem is that README.md states that I should "grab the defaultClient from your code"... but there is NO defaultClient in the generated <myApiName>Client.swift.

The only references to defaultClient (or close) in <myApiName>Client.swift is as follows:

import AWSCore
import AWSAPIGateway

public class SVTLambdaGateClient: AWSAPIGatewayClient {

    static let AWSInfoClientKey = "SVTLambdaGateClient"

    private static let _serviceClients = AWSSynchronizedMutableDictionary()
    private static let _defaultClient:SVTLambdaGateClient = {
        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 SVTLambdaGateClient(configuration: serviceConfiguration!)
    }()

and then just below:

    /**
     Returns the singleton service client. If the singleton object does not exist, the SDK instantiates the <snip>
     @return The default service client.
    */
    public class func `default`() -> SVTLambdaGateClient{
        return _defaultClient
    }

I assume I should be looking for a public class func defaultClient(), right?

Which AWS service(s) are affected? API Gateway

Expected behavior I would expect the the API Gateway-generated iOS Swift SDK would have a defaultClient() function in <myApiName>Client.swift if the README states that the developer should use it in their iOS app.

Screenshots If applicable, add screenshots to help explain your problem.

Environment(please complete the following information):

Device Information (please complete the following information):

motivus commented 5 years ago

Just a note that I "grabbed the defaultClient" (to quote the README.md) by calling the default() function in <myApiName>Client.swift as follows:

    @IBAction func userInvokeApi(_ sender: UIButton) {
        print("You clicked invoke api...")

        // grab the defaultClient...
        let client = SVTLambdaGateClient.default()
        client.calcGet(operand2: "3", _operator: "+", operand1: "5").continueWith{ (task: AWSTask?) -> AnyObject? in
            if let error = task?.error {
                print("Error occurred: \(error)")
                return nil
            }

            if let result = task?.result {
                // Do something with result
                print("Result was returned from REST API")
            }

            return nil
        }
    }

And my REST API was successfully called, as confirmed in the Cloudwatch logs!

Is this what the README.md meant to do?

I'm going to post another usage question about how to access the result because I'm having trouble figuring out what to do with the AWSTask type.

rohandubal commented 5 years ago

Hello @motivus

Thanks for pointing out this error to us. You are right, the README intents to use the .default() and not defaultClient()

Also, as a reference point, we have updated documentation set here which uses the latest SDK versions and best practices: https://aws-amplify.github.io/docs/ios/api#rest-api

The link you have pointed out is a little old.

Best, Rohan

motivus commented 5 years ago

Thank you, Sir! Updating my reply here because I see that you linked to the REST API section of the new Amplify documentation. That's actually where I started a couple months ago... but I've had some trouble controlling how Amplify generates backend resources, so I switched to using the AWS docs to create my resources. That's why I linked the API Gateway tutorial / docs. No need to reply!

mregnauld commented 3 years ago

I don't understand why that thread is closed, since that problem is still here, almost 2 years later...