1

I am trying to make an API request to AWS to get some product information. I used the swift request signing code from this answer on SO: https://stackoverflow.com/a/33084842/4208349.

I have modified it so that it works within my project as such:

class AWSTest: UIViewController {


override func viewDidLoad() {



}


func AWSRequest(){

let operationParams: [String: String] = ["Service": "AWSECommerceService", "Operation": "ItemLookup", "ItemId": "045242127733", "IdType": "UPC", "ResponseGroup": "Images,ItemAttributes", "SearchIndex": "All"]

let keyParams = ["AWSAccessKeyId": "accesskey", "AssociateTag": "associatetag", "Timestamp": timestampFormatter.stringFromDate(NSDate())]

let fullParams = operationParams + keyParams

let signedParams = signedParametersForParameters(fullParams)

Alamofire.request(.GET, "http://webservices.amazon.com/onca/xml", parameters: signedParams).responseString { (response) in
print("Success: \(response.result.isSuccess)")
print("Response String: \(response.result.value)")
}

}

let timestampFormatter: NSDateFormatter

init() {
    timestampFormatter = NSDateFormatter()
    timestampFormatter.dateFormat = AWSDateISO8601DateFormat3
    timestampFormatter.timeZone = NSTimeZone(name: "GMT")
    timestampFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func signedParametersForParameters(parameters: [String: String]) -> [String: String] {
    let sortedKeys = Array(parameters.keys).sort(<)

    var components: [(String, String)] = []
    for key in sortedKeys {
        components += ParameterEncoding.URLEncodedInURL.queryComponents(key, parameters[key]!)
    }

    let query = (components.map { "\($0)=\($1)" } as [String]).joinWithSeparator("&")

    let stringToSign = "GET\nwebservices.amazon.com\n/onca/xml\n\(query)"
    let dataToSign = stringToSign.dataUsingEncoding(NSUTF8StringEncoding)
    let signature = AWSSignatureSignerUtility.HMACSign(dataToSign, withKey: "secretKey", usingAlgorithm: UInt32(kCCHmacAlgSHA256))!

    let signedParams = parameters + ["Signature": signature]

    return signedParams
 }
}

For some reason I keep getting the error, "Binary operator '+' cannot be applied to two '[String : String]' operands" on these two lines :

let fullParams = operationParams + keyParams

and

let signedParams = parameters + ["Signature": signature]

In addition if I expand the error message in Xcode I can see this message, "Overloads for '+' exist with these partially matching parameter lists: (UInt8, UInt8), (Int8, Int8), (UInt16, UInt16), (Int16, Int16), (UInt32, UInt32), (Int32, Int32), (UInt64, UInt64), (Int64, Int64), (UInt, UInt), (Int, Int), (Float, Float), (Double, Double), (Float80, Float80), (String, String), (CGFloat, CGFloat), (T, T), (C, S), (S, C), (RRC1, RRC2), (T, T.Stride), (T.Stride, T), (T, T._DisallowMixedSignArithmetic), (T._DisallowMixedSignArithmetic, T), (UnsafeMutablePointer, Int), (Int, UnsafeMutablePointer), (UnsafePointer, Int), (Int, UnsafePointer), (Self, Self)"

Anything that can help me find a way to correct these errors would be greatly appreciated!

Community
  • 1
  • 1
Dominic
  • 95
  • 1
  • 1
  • 14
  • 1
    Be aware that all solutions to adding 2 dictionaries together share the same problem: what to do if a key exists in both dictionaries? You have to choose one or the other. The `+` operation is not commutative, that is: `dictA + dictB != dictB + dictA` – Code Different Aug 30 '16 at 16:52
  • Thanks for the help guys! The linked question about adding items into dictionaries helped me create a dictionary extension and solved my problem! – Dominic Aug 30 '16 at 17:12

0 Answers0