vendoo_v1.0/Vendoo/AmazonWebServiceManager.swift
2016-08-27 12:55:41 -04:00

83 lines
3.0 KiB
Swift

//
// AmazonWebServiceManager.swift
// Vendoo
//
// Created by cole alban on 8/26/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
import Foundation
/*
This class will manage the posting of items to Amazon using the MWS
(MarketPlaceWebServicesAPI).
*/
class AmazonWebServiceManager{
/*
These values all correspond to authentication information
*/
private var sellerId: String
private var marketPlaceId: String
private var awsAccessKeyId: String
private var secretKey: String
private var developerAccountNumber: String
/*
These values are currently hardcoded in and map to the Kroleo
seller account. Once authnetication for amazon is solved, these
will be able to dynamically update to the respective user information
Usually, this will take in parameters with the right info for init.
*/
init(){
self.sellerId = "A2RZVRHXWN051E"
self.marketPlaceId = "A2RZVRHXWN051E"
self.awsAccessKeyId = "AKIAJL6QFNV3AHMJMBRA"
self.secretKey = "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"
self.developerAccountNumber = "6187-7922-4188"
}
/*
This function will take an item and post it to amazon.
params: Hash of information used to post an item to amazon
*/
func listItem(params: [String: AnyObject]){
let mwsApiEndpointUrl: NSURL = NSURL(fileURLWithPath: "https://mws.amazonservices.com")
let amazonMWSRequest: NSMutableURLRequest = NSMutableURLRequest(URL: mwsApiEndpointUrl)
setRequestHeaders(amazonMWSRequest, feedType: FeedType.POST_PRODUCT_DATA)
}
/*
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
*/
private func setRequestHeaders(request: NSMutableURLRequest, feedType: FeedType ){
request.HTTPMethod = "POST"
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 helper function will return an iso8601 formatted datetime string
return: an iso8601 formatted datetime string
*/
private 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())
}
}