65 lines
2.0 KiB
Swift
65 lines
2.0 KiB
Swift
//
|
|
// DeviceService.swift
|
|
//
|
|
//
|
|
// Created by Juraldinio on 01.08.2022.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
|
|
public struct DeviceService: NetworkService {
|
|
|
|
let enmironment: NetworkEnvironment
|
|
|
|
public init(enmironment: NetworkEnvironment) {
|
|
self.enmironment = enmironment
|
|
}
|
|
|
|
public func createDevice(uuid: String) async throws -> Device {
|
|
|
|
let variables = RegisterDeviceRequest.Variables(cid: uuid)
|
|
|
|
let result = try? await AF.request(
|
|
self.enmironment.url,
|
|
method: .post,
|
|
parameters: RegisterDeviceRequest(variables: variables),
|
|
encoder: JSONParameterEncoder.default,
|
|
headers: self.enmironment.httpHeaders
|
|
)
|
|
// .cURLDescription { print($0) }
|
|
.serializingDecodable(GraphQLResult<RegisterDeviceResponse>.self)
|
|
.value
|
|
|
|
switch result?.result {
|
|
case let .firstType(device): return Device(uuid: uuid, id: device.uid, isTrusted: device.isTrustedNow)
|
|
case let .secondType(error): throw error.networkServiceError
|
|
default: throw NetworkServiceError.uncatched
|
|
}
|
|
}
|
|
|
|
public func checkDevice(id: String, token: String) async throws -> DeviceStatus {
|
|
|
|
let variables = CheckDeviceRequest.Variables(uid: id, token: token)
|
|
|
|
let result = try? await AF.request(
|
|
self.enmironment.url,
|
|
method: .post,
|
|
parameters: CheckDeviceRequest(variables: variables),
|
|
encoder: JSONParameterEncoder.default,
|
|
headers: self.enmironment.httpHeaders
|
|
)
|
|
.responseString { print($0) }
|
|
// .cURLDescription { print($0) }
|
|
.serializingDecodable(GraphQLResult<CheckDeviceResponse>.self)
|
|
.value
|
|
|
|
switch result?.result {
|
|
case let .firstType(status): return status.status
|
|
case let .secondType(error): throw error.networkServiceError
|
|
default: throw NetworkServiceError.uncatched
|
|
}
|
|
}
|
|
|
|
}
|