-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABNetworking.swift
More file actions
79 lines (60 loc) · 2 KB
/
Copy pathABNetworking.swift
File metadata and controls
79 lines (60 loc) · 2 KB
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
//
// ABNetworking.swift
// Absinthe-iOS
//
// Interface to AmstelBright APIs
//
// Created by Mitchell Kahn on 8/31/16.
// Copyright © 2016 AppDelegates. All rights reserved.
//
import Foundation
import Alamofire
import PromiseKit
import SwiftyJSON
class ABNetworking {
var ipAddress: String = ""
var useHttps = false
let q = DispatchQueue.global()
let token = "FAKETOKEN$$"
var apiPath: String {
return ( self.useHttps ? "https://" : "http://" ) + self.ipAddress + "api/"
}
enum ABErrors: Error {
case badUrl
case notAuthorized
case weird
}
// Promisified JSON getter
func getJson(_ endpoint: String, parameters: [String: Any] = [:]) -> Promise<JSON> {
return firstly {
Alamofire.request( endpoint, method: .get, parameters: parameters )
.validate()
.responseJSON()
}
.then( on:q){ value in
JSON(value)
}
}
func postOrPutJson( _ endpoint: String, data: Dictionary<String, Any>, method: HTTPMethod ) -> Promise<JSON> {
return firstly{
Alamofire.request(endpoint, method: method, parameters: data,
headers: [ "X-AB-Token" : self.token ])
.validate() //Checks for non-200 response
.responseJSON()
}
.then( on:q){ value in
JSON(value)
}
}
// Promisified JSON Poster
func postJson( _ endpoint: String, data: Dictionary<String, Any> ) -> Promise<JSON> {
return postOrPutJson(endpoint, data: data, method: .post)
}
// Promisified JSON Putter
func putJson( _ endpoint: String, data: Dictionary<String, Any> ) -> Promise<JSON> {
return postOrPutJson(endpoint, data: data, method: .put)
}
func getSystemInfo() -> Promise<JSON> {
return getJson(apiPath+"/system/device")
}
}