AWS get response as data, not JSON (for using with Codable)

Viewed 47

Not familiar enough with AWS, but I have some Codable models I need to initialize from AWS. I'm getting JSON result from AWSTask.result (which is AnyObject). I'm trying to avoid creating Data from Dictionaty and back to a struct (to be able to use Codable).
I tied to use AWSNetworkingHTTPResponseInterceptor, but it was never got called and I couldn't find any example of using it.

self.getGatewayClient { (apiGatewayClient: AWSAPIGatewayClient?) in
    
    let queryParameters = ...
    let headerParameters = ...
    
    apiGatewayClient?.invokeHTTPRequest(
        "GET",
        urlString: "/path",
        pathParameters: [:],
        queryParameters: queryParameters,
        headerParameters: headerParameters,
        body: nil,
        responseClass: nil
    ).continueWith { (task: AWSTask<AnyObject>) -> Any? in
        
        if let data = task... { // Get response as Data type??
            
        }
        
        if let result = task.result as? [String: Any] {
            // Thanks, but I have a Codable, so I'll just take the data thank you.
        }
        
        return task
    }
}
1 Answers

AWS's AWSAPIGatewayClient has two functions, one is: invokeHTTPRequest (which was what was used). There is another one called invoke, which returns data. It takes a AWSAPIGatewayRequest request:

func someTask(completion: @escaping (String?) -> ()) {
    self.getGatewayClient { (apiGatewayClient: AWSAPIGatewayClient?) in
        let request = AWSAPIGatewayRequest(httpMethod: "GET",
                                           urlString: "/path",
                                           queryParameters: queryParameters,
                                           headerParameters: headerParameters,
                                           httpBody: nil)
        apiGatewayClient?.invoke(request).continueOnSuccessWith { response in
            
            if let data = response.result?.responseData {
                // Init Codable using data
            }
        }
    }
}
Related