vendoo_v1.0/Vendoo/AmazonRequest.swift
2016-08-30 13:50:47 -04:00

93 lines
3.6 KiB
Swift

//
// AmazonRequest.swift
// Vendoo
//
// Created by cole alban on 8/28/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
import Foundation
/*
This class will represent a request to the Amazon MWS API.
It will contain methods for creating headers, getting the
headers back as a queryString(used to sign the request), and
metadata regarding the transaction.
*/
class AmazonRequest{
private var listing: AmazonListing
private var xmlBody: AmazonXmlBody
/*
This constructor will create a request with the given listing and then
construct the xml body with the given listing.
listing: The listing to create the request from
*/
init(listing: AmazonListing){
self.listing = listing
self.xmlBody = AmazonXmlBody(listing)
}
/*
This helper function will set the headers for the posting a new item to mws
request: The request headers will be set for
feedType: The type of feed this request will be for
*/
func setRequestHeaders(request: NSMutableURLRequest, feedType: FeedType ){
request.HTTPMethod = "POST"
request.addValue("text/xml", forHTTPHeaderField: "Content-Type")
request.addValue("SubmitFeed", forHTTPHeaderField: "Action")
request.addValue(self.awsAccessKeyId, forHTTPHeaderField: "AWSAccessKeyId")
request.addValue(self.sellerId, forHTTPHeaderField: "Merchant")
request.addValue(getIso8601DateTimeString(), forHTTPHeaderField: "Timestamp")
request.addValue("2009-01-01", forHTTPHeaderField: "Version")
request.addValue(feedType.rawValue, forHTTPHeaderField: "FeedType")
request.addValue("HmacSHA256", forHTTPHeaderField: "SignatureMethod")
request.addValue("2", forHTTPHeaderField: "SignatureVersion")
}
/*
This will set the body of the request to be the xml body
that was created in the constructor.
If the xml body is "Error" then nothing will be done as there was an
error in the parsing of the body upon creation
*/
func setRequestBody(){
if(self.xmlBody)
}
/*
This helper function will return an iso8601 formatted datetime string
return: an iso8601 formatted datetime string
*/
func getIso8601DateTimeString()->String{
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
return dateFormatter.stringFromDate(NSDate())
}
/*
This will return a properly formatted header in string form
Note: This is before the query is signed.
*/
func getRequestHeaderAsString(request: NSMutableURLRequest)->String{
var queryString: String = "POST\nmws.amazonservices.com\n\\"+
queryString += "AwsAccessKeyId="+request.valueForHTTPHeaderField("AwsAccessKeyId")
queryString += "&Action="+request.valueForHTTPHeaderField("Action")
queryString += "&FeedType="+request.valueForHTTPHeaderField("FeedType")
queryString += "&SellerId="+request.valueForHTTPHeaderField("SellerId")
queryString += "&SignatureMethod="+request.valueForHTTPHeaderField("SignatureMethod")
queryString += "&SignatureVersion="+request.valueForHTTPHeaderField("SignatureVersion")
queryString += "&Timestamp="+request.valueForHTTPHeaderField("Timestamp")
queryString += "&Version="+request.valueForHTTPHeaderField("Version")
return queryString
}
}