【问题标题】:How to call an AWS webservice (API Gateway) on iOS using SOTO?如何使用 SOTO 在 iOS 上调用 AWS Web 服务(API 网关)?
【发布时间】:2021-05-06 15:01:57
【问题描述】:

在我的 iOS 项目中,我需要实现 AWS 开发工具包,就像我在该项目的 Android 版本中成功完成的那样,如果可能的话,使用 Swift Package Manager 而不是 Cocoapods。

由于 SPM 似乎还不可用,我尝试了Soto,它允许我使用 SPM 实现 AWS 开发工具包。

这是我在 Android 项目中所做的,我想在 iOS 上复制:

  • 我创建了一个AWSInterface 接口,其中我所有的api端点都是:
@Service(endpoint = "https://abcde12345.execute-api.eu-central-1.amazonaws.com/dev")
interface AWSInterface
{
    @Operation(path = "/api-demo", method = "POST")
    fun apiDemo(
        @Parameter(name = "Content-Type", location = "header") contentType: String,
        body: ApiDemoModel): RetourStatutWS
}
  • 这里是ApiDemoModel,很简单:
class ApiDemoModel(val code: String)
  • 我创建了一个AWSInterfaceHolder 类,所以我可以调用api:
class AWSInterfaceHolder {

    var awsInterface: AWSInterface = ApiClientFactory()
        .credentialsProvider(AWSMobileClient.getInstance())
        .clientConfiguration(ClientConfiguration().withConnectionTimeout(30000))
        .build(AWSInterface::class.java)
}
  • 我初始化了AWSMobileClient,并调用了我的api:
AWSMobileClient.getInstance().initialize(
    applicationContext,
    object : Callback<UserStateDetails> {

        override fun onResult(result: UserStateDetails?) {
            
            // AWSMobileClient is successfully initialized, I can call my api:
            val awsInterfaceHolder = AWSInterfaceHolder()
             awsInterfaceHolder.awsInterface.apiDemo(
                 "application/json",
                 ApiDemoModel("123456"))
        }

        override fun onError(e: Exception?) {
            e.printStackTrace()
        }
}

如果可能的话,我如何为我的 iOS Swift 项目做同样的事情,使用 Soto,因为默认的 AWS 开发工具包还不能用于 SPM?

谢谢。

【问题讨论】:

    标签: android ios kotlin aws-sdk soto


    【解决方案1】:

    Soto 没有为 APIGateway 生成任何代码,但如果您希望签署 APIGateway REST 接口的请求,您可以执行以下操作。该代码使用 swift-server AsyncHTTPClient,但它应该很容易将 signHeaders 的结果转换为 URLRequest 以与 URLSession 一起使用。

    import SotoSignerV4
    
    func apiGatewayExecute(
        url: URL, 
        method: HTTPMethod, 
        headers: HTTPHeaders, 
        body: ByteBuffer? = nil
    ) -> EventLoopFuture<HTTPClient.Response> {
        let credentials: Credential = StaticCredential(
            accessKeyId: "_MYACCESSKEY_", 
            secretAccessKey: "_MYSECRETACCESSKEY_"
        )
        let signer = AWSSigner(credentials: credentials, name: "execute-api", region: "us-east-1")
        // clean up URL
        let processedURL = signer.processURL(url: url)!
        let signedHeaders = signer.signHeaders(
            url: processedURL,
            method: method,
            headers: headers,
            body: body.map { .byteBuffer($0) }
        )
        let request = try! HTTPClient.Request(
            url: processedURL,
            method: method,
            headers: signedHeaders,
            body: body.map { .byteBuffer($0) }
        )
        return httpClient.execute(request: request, logger: logger)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2018-02-09
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多