Skip to content

Commit

Permalink
make access key optional
Browse files Browse the repository at this point in the history
  • Loading branch information
thehung111 committed Oct 10, 2016
1 parent 6b63c6e commit 1b061ef
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SearchResultsCollectionViewController: UICollectionViewController {

// call tracking api here to register the click
// alternately, you can present similar images or recommend them to users in the image detail page
let params = ViTrackParams(accessKey: ViSearch.sharedInstance.client!.accessKey, reqId: self.reqId, action: "click")
let params = ViTrackParams(reqId: self.reqId, action: "click")
params?.imName = im_name

ViSearch.sharedInstance.track(params: params!) { (success, error) in
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ User action (e.g. click on an image after search) can be sent in this way:

```swift

let params = ViTrackParams(accessKey: ViSearch.sharedInstance.client!.accessKey, reqId: recentReqId, action: "click" )
let params = ViTrackParams(reqId: recentReqId, action: "click" )

// You can also append an im_name field
params.imName = "example_clicked_im_name"
Expand Down
25 changes: 15 additions & 10 deletions ViSearchSDK/ViSearchSDK/Classes/Request/ViTrackParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,45 @@ public class ViTrackParams : ViSearchParamsProtocol{
public var imName : String?
public var reqId : String
public var cuid : String?
var cid : String

//MARK: init
var cid : String?

/// Init tracking parameters. For example, after user search for images, we want to track which image they clicked
///
/// - parameter accessKey: API access key
/// - parameter reqId: recent request id e.g. search for similar images
/// - parameter action: tracking action e.g. click, add to cart, etc. Currently only click is supported
public init?(accessKey: String, reqId : String , action: String ) {
public convenience init?(accessKey: String, reqId : String , action: String ) {

if accessKey.isEmpty {
print("\(type(of: self)).\(#function)[line:\(#line)] - error: accessKey parameter is missing")

return nil
}

self.init(reqId: reqId, action: action)

self.cid = accessKey
self.cuid = nil
self.imName = nil
}

/// Init tracking parameters. For example, after user search for images, we want to track which image they clicked
/// Assumption: access key will be passed by client later
/// - parameter reqId: recent request id e.g. search for similar images
/// - parameter action: tracking action e.g. click, add to cart, etc. Currently only click is supported
public init?(reqId : String , action: String){
if reqId.isEmpty {
print("\(type(of: self)).\(#function)[line:\(#line)] - error: reqId parameter is missing")

return nil
}

if action.isEmpty {
print("\(type(of: self)).\(#function)[line:\(#line)] - error: action parameter is missing")

return nil
}

self.cid = accessKey

self.reqId = reqId
self.action = action
self.cuid = nil
self.imName = nil
}

//MARK: search protocol
Expand Down
2 changes: 1 addition & 1 deletion ViSearchSDK/ViSearchSDK/Classes/ViSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ open class ViSearch: NSObject {
return nil
}

// track the API calls and various actions
/// track the API calls and various actions
/// Tracking API
@discardableResult public func track(params: ViTrackParams,
handler: ( (_ success: Bool, Error?) -> Void )?
Expand Down
2 changes: 2 additions & 0 deletions ViSearchSDK/ViSearchSDK/Classes/ViSearchClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ open class ViSearchClient: NSObject, URLSessionDelegate {
handler: ( (_ success: Bool, Error?) -> Void )?
) -> Void {

params.cid = accessKey

// different url for tracking
let url = requestSerialization.generateRequestUrl(baseUrl: trackUrl , apiEndPoint: .TRACK , searchParams: params)
let request = NSMutableURLRequest(url: URL(string: url)! , cachePolicy: .useProtocolCachePolicy , timeoutInterval: timeoutInterval)
Expand Down

0 comments on commit 1b061ef

Please sign in to comment.