|
|
|
|
@ -241,23 +241,23 @@ public class Services { |
|
|
|
|
/// - Parameters: |
|
|
|
|
/// - method: the HTTP method to execute |
|
|
|
|
/// - payload: the content to put in the httpBody |
|
|
|
|
fileprivate func _baseSyncRequest(method: HTTPMethod, payload: Encodable) throws -> URLRequest { |
|
|
|
|
let urlString = baseURL + "data/" |
|
|
|
|
|
|
|
|
|
guard let url = URL(string: urlString) else { |
|
|
|
|
throw ServiceError.urlCreationError(url: urlString) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var request = URLRequest(url: url) |
|
|
|
|
request.httpMethod = method.rawValue |
|
|
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
|
|
|
|
request.httpBody = try JSON.encoder.encode(payload) |
|
|
|
|
|
|
|
|
|
let token = try self.keychainStore.getValue() |
|
|
|
|
request.addValue("Token \(token)", forHTTPHeaderField: "Authorization") |
|
|
|
|
|
|
|
|
|
return request |
|
|
|
|
} |
|
|
|
|
// fileprivate func _baseSyncRequest(method: HTTPMethod, payload: Encodable) throws -> URLRequest { |
|
|
|
|
// let urlString = baseURL + "data/" |
|
|
|
|
// |
|
|
|
|
// guard let url = URL(string: urlString) else { |
|
|
|
|
// throw ServiceError.urlCreationError(url: urlString) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// var request = URLRequest(url: url) |
|
|
|
|
// request.httpMethod = method.rawValue |
|
|
|
|
// request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
|
|
|
|
// request.httpBody = try JSON.encoder.encode(payload) |
|
|
|
|
// |
|
|
|
|
// let token = try self.keychainStore.getValue() |
|
|
|
|
// request.addValue("Token \(token)", forHTTPHeaderField: "Authorization") |
|
|
|
|
// |
|
|
|
|
// return request |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
/// Runs a request using a traditional URLRequest |
|
|
|
|
/// - Parameters: |
|
|
|
|
@ -392,19 +392,13 @@ public class Services { |
|
|
|
|
request.addValue("Token \(token)", forHTTPHeaderField: "Authorization") |
|
|
|
|
|
|
|
|
|
let modelName = String(describing: T.self) |
|
|
|
|
let operations = try apiCalls.map { apiCall in |
|
|
|
|
|
|
|
|
|
if let body = apiCall.body, let data = body.data(using: .utf8) { |
|
|
|
|
let object = try JSON.decoder.decode(T.self, from: data) |
|
|
|
|
return Operation(apiCallId: apiCall.id, |
|
|
|
|
operation: apiCall.method.rawValue, |
|
|
|
|
modelName: modelName, |
|
|
|
|
data: object, |
|
|
|
|
storeId: object.getStoreId()) |
|
|
|
|
} else { |
|
|
|
|
throw ServiceError.cantDecodeData(resource: T.resourceName(), method: apiCall.method.rawValue, content: apiCall.body) |
|
|
|
|
} |
|
|
|
|
let operations = apiCalls.map { apiCall in |
|
|
|
|
|
|
|
|
|
return Operation(apiCallId: apiCall.id, |
|
|
|
|
operation: apiCall.method.rawValue, |
|
|
|
|
modelName: modelName, |
|
|
|
|
data: apiCall.data, |
|
|
|
|
storeId: apiCall.data?.getStoreId()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let payload = SyncPayload(operations: operations, |
|
|
|
|
@ -538,7 +532,7 @@ public class Services { |
|
|
|
|
let url = try self._url(from: apiCall) |
|
|
|
|
var request = URLRequest(url: url) |
|
|
|
|
request.httpMethod = apiCall.method.rawValue |
|
|
|
|
request.httpBody = apiCall.body?.data(using: .utf8) |
|
|
|
|
request.httpBody = try apiCall.data?.jsonData() |
|
|
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
|
|
|
|
|
|
|
|
|
if self._isTokenRequired(type: T.self, method: apiCall.method) { |
|
|
|
|
@ -749,25 +743,26 @@ public class Services { |
|
|
|
|
try self._storeToken(username: userName, token: services.keychainStore.getValue()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests |
|
|
|
|
// MARK: - Convenience method for tests |
|
|
|
|
|
|
|
|
|
/// Executes a POST request |
|
|
|
|
public func post<T: Storable>(_ instance: T) async throws -> T { |
|
|
|
|
var postRequest = try self._postRequest(type: T.self) |
|
|
|
|
postRequest.httpBody = try JSON.encoder.encode(instance) |
|
|
|
|
return try await self._runRequest(postRequest) |
|
|
|
|
public func post<T: SyncedStorable>(_ instance: T) async throws -> T? { |
|
|
|
|
let apiCall: ApiCall<T> = ApiCall(method: .post, data: instance) |
|
|
|
|
let results: [T] = try await self.runApiCalls([apiCall]) |
|
|
|
|
return results.first |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Executes a PUT request |
|
|
|
|
public func put<T: Storable>(_ instance: T) async throws -> T { |
|
|
|
|
var postRequest = try self._putRequest(type: T.self, id: instance.stringId) |
|
|
|
|
postRequest.httpBody = try JSON.encoder.encode(instance) |
|
|
|
|
return try await self._runRequest(postRequest) |
|
|
|
|
public func put<T: SyncedStorable>(_ instance: T) async throws -> T { |
|
|
|
|
let apiCall: ApiCall<T> = ApiCall(method: .put, data: instance) |
|
|
|
|
let results: [T] = try await self.runApiCalls([apiCall]) |
|
|
|
|
return results.first! |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func delete<T: Storable>(_ instance: T) async throws -> T { |
|
|
|
|
let deleteRequest = try self._deleteRequest(type: T.self, id: instance.stringId) |
|
|
|
|
return try await self._runRequest(deleteRequest) |
|
|
|
|
public func delete<T: SyncedStorable>(_ instance: T) async throws -> T { |
|
|
|
|
let apiCall: ApiCall<T> = ApiCall(method: .delete, data: instance) |
|
|
|
|
let results: [T] = try await self.runApiCalls([apiCall]) |
|
|
|
|
return results.first! |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns a POST request for the resource |
|
|
|
|
|