-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseClient.swift
109 lines (83 loc) · 4.46 KB
/
ParseClient.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// ParseClient.swift
// On the Map
//
// Created by Mario Preishuber on 31/03/16.
// Copyright © 2016 Preishuber. All rights reserved.
//
import Foundation
class ParseClient: NetworkUtils {
var userDataList: [UserData] = []
func taskForGETMethod(method: String, parameters: [String:String], completionHandlerForGET: (result: AnyObject!, error: NSError?) -> Void) -> NSURLSessionDataTask {
let request = NSMutableURLRequest(URL:
self.parseURLFromParameters(parameters, withPathExtension:method))
requestAddValueXParse(request)
return self.task(request, completionHandler: completionHandlerForGET)
}
func taskForPOSTMethod(method: String, jsonBody: String, completionHandlerForPOST: (result: AnyObject!, error: NSError?) -> Void) -> NSURLSessionDataTask {
let request = NSMutableURLRequest(URL: parseURLWithPathExtension(method))
request.HTTPMethod = "POST"
requestAddValueContentTypes(request)
requestAddValueXParse(request)
request.HTTPBody = jsonBody.dataUsingEncoding(NSUTF8StringEncoding)
return self.task(request, completionHandler: completionHandlerForPOST)
}
func taskForPUTMethod(method: String, jsonBody: String, completionHandlerForPUT: (result: AnyObject!, error: NSError?) -> Void) -> NSURLSessionDataTask {
let request = NSMutableURLRequest(URL: parseURLWithPathExtension(method))
request.HTTPMethod = "PUT"
requestAddValueContentTypes(request)
requestAddValueXParse(request)
request.HTTPBody = jsonBody.dataUsingEncoding(NSUTF8StringEncoding)
return self.task(request, completionHandler: completionHandlerForPUT)
}
func taskForDELETEMethod(method: String, completionHandlerForDELETE: (result: AnyObject!, error: NSError?) -> Void) -> NSURLSessionDataTask {
let request = NSMutableURLRequest(URL:
self.parseURLFromParameters([:], withPathExtension:method))
request.HTTPMethod = "DELETE"
requestAddValueXParse(request)
return self.task(request, completionHandler: completionHandlerForDELETE)
}
private func requestAddValueXParse(request: NSMutableURLRequest) {
request.addValue(ReqeustValues.XParseAppId, forHTTPHeaderField: RequestKeys.XParseAppId)
request.addValue(ReqeustValues.XParseRestApiKey, forHTTPHeaderField: RequestKeys.XParseRestApiKey)
}
// create a URL from parameters
private func parseURLFromParameters(parameters: [String:AnyObject], withPathExtension: String) -> NSURL {
let components = NSURLComponents()
components.scheme = ParseClient.APIConstants.ApiScheme
components.host = ParseClient.APIConstants.ApiHost
components.path = ParseClient.APIConstants.ApiPath + (withPathExtension ?? "")
components.queryItems = [NSURLQueryItem]()
for (key, value) in parameters {
let queryItem = NSURLQueryItem(name: key, value: "\(value)")
components.queryItems!.append(queryItem)
}
return components.URL!
}
private func parseURLWithPathExtension(withPathExtension: String) -> NSURL {
let components = NSURLComponents()
components.scheme = ParseClient.APIConstants.ApiScheme
components.host = ParseClient.APIConstants.ApiHost
components.path = ParseClient.APIConstants.ApiPath + withPathExtension
components.queryItems = [NSURLQueryItem]()
return components.URL!
}
func userDataToJSONBody() -> String {
return "{" +
"\"\(JSONBodyKeys.UniqueKey)\": \"\(AppVariables.userData.getUniqueKey())\"," +
"\"\(JSONBodyKeys.FirstName)\": \"\(AppVariables.userData.getFirstName())\"," +
"\"\(JSONBodyKeys.LastName)\" : \"\(AppVariables.userData.getLastName())\"," +
"\"\(JSONBodyKeys.MapString)\": \"\(AppVariables.userData.getMapString())\"," +
"\"\(JSONBodyKeys.MediaURL)\" : \"\(AppVariables.userData.getMediaURL())\"," +
"\"\(JSONBodyKeys.Latitude)\" : \(AppVariables.userData.getLatitude())," +
"\"\(JSONBodyKeys.Longitude)\": \(AppVariables.userData.getLongitude())" +
"}"
}
// shared instance
override class func sharedInstance() -> ParseClient {
struct Singleton {
static var sharedInstance = ParseClient()
}
return Singleton.sharedInstance
}
}