-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPICaller.swift
163 lines (118 loc) · 4.94 KB
/
APICaller.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// FMAPI.swift
// MySmartRestaurant
//
// Created by Francesco De Marco on 19/01/23.
//
import Foundation
public class APICaller: ObservableObject {
static let shared = APICaller(username: "Admin", password: "admin")
public let baseURI = "https://napoli.fm-testing.com/fmi/odata/v4/MySmartRestaurant"
// Because FileMaker gives you an array of value.
struct JSONValue<T: Codable>: Codable {
var value: [T]
}
var username: String
var password: String
var auth: String {(username + ":" + password).data(using: .utf8)?.base64EncodedString() ?? ""}
// To set username and password
public init(username: String, password: String) {
self.username = username
self.password = password
}
public func getFromFM<T: Codable>(urlTmp: String) async throws -> [T] {
guard let url = URL(string: urlTmp) else {
throw URLError(.badURL)
}
var request = URLRequest(url: url)
request.setValue("Basic \(auth)", forHTTPHeaderField: "Authorization")
request.httpMethod = "GET"
// Execution of the API call
let (data, response) = try await URLSession.shared.data(for: request)
// Checking for an error
if (response as? HTTPURLResponse)?.statusCode ?? 500 < 300 {
} else {
throw HTTPErrors.httpError
}
do {
let fetchedData = try JSONDecoder().decode(JSONValue<T>.self, from: data)
// In this way we receive an array of elements, not value but its content.
return fetchedData.value
} catch {
print("Decode Error")
print(String(data: data, encoding: .utf8) ?? "")
throw error
}
}
public func createRecordInFM<T: Codable>(urlTmp: String, data: T) async throws {
guard let url = URL(string: urlTmp) else {
throw URLError(.badURL)
}
var request = URLRequest(url: URL(string: urlTmp)!)
request.url = url
request.setValue("Basic \(auth)", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
guard let encoded = try? JSONEncoder().encode(data) else {
throw CodableError.encode
}
print(String(data: encoded, encoding: .utf8))
// Execution of the API call
let (_, response) = try await URLSession.shared.upload(for: request, from: encoded)
// Checking for an error
if (response as? HTTPURLResponse)?.statusCode ?? 500 < 300 {
} else {
print((response as? HTTPURLResponse)!.statusCode)
throw HTTPErrors.httpError
}
}
public func editRecordInFM<T: Codable>(urlTmp: String, data: T) async throws {
print(urlTmp)
guard let url = URL(string: urlTmp) else {
print("url Error")
throw URLError(.badURL)
}
var request = URLRequest(url: URL(string: urlTmp)!)
request.url = url
request.setValue("Basic \(auth)", forHTTPHeaderField: "Authorization")
request.httpMethod = "PATCH"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
guard let encoded = try? JSONEncoder().encode(data) else {
throw CodableError.encode
}
print(request.url)
print(String(data: encoded, encoding: .utf8))
// Execution of the API call
let (_, response) = try await URLSession.shared.upload(for: request, from: encoded)
// Checking for an error
if (response as? HTTPURLResponse)?.statusCode ?? 500 < 300 {
} else {
print((response as? HTTPURLResponse)!.statusCode)
throw HTTPErrors.httpError
}
}
public func deleteRecordInFM(urlTmp: String) async throws {
guard let url = URL(string: urlTmp) else {
throw URLError(.badURL)
}
var request = URLRequest(url: URL(string: urlTmp)!)
request.url = url
request.setValue("Basic \(auth)", forHTTPHeaderField: "Authorization")
request.httpMethod = "DELETE"
// Execution of the API call
let (_, response) = try await URLSession.shared.data(for: request)
// Checking for an error
if (response as? HTTPURLResponse)?.statusCode ?? 500 < 300 {
} else {
throw HTTPErrors.httpError
}
}
}
/// Enumeration of all the possible HTTP method of the FMOData protocol
public enum HTTPErrors: Error {
case httpError
}
public enum CodableError: Error {
case encode
case decode
}