2020-02-18 01:44:30 +00:00
|
|
|
//
|
|
|
|
// Network.swift
|
2022-08-07 13:31:15 +01:00
|
|
|
// Mixonomer
|
2020-02-18 01:44:30 +00:00
|
|
|
//
|
|
|
|
// Created by Andy Pack on 18/02/2020.
|
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Alamofire
|
2020-02-18 21:43:30 +00:00
|
|
|
import SwiftyJSON
|
|
|
|
import KeychainAccess
|
2020-02-18 01:44:30 +00:00
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
public enum AuthMethod {
|
|
|
|
case basic
|
2022-08-08 20:40:38 +01:00
|
|
|
case jwt
|
|
|
|
case none
|
2020-02-18 01:44:30 +00:00
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
func auth(headers: Alamofire.HTTPHeaders?) -> Alamofire.HTTPHeaders {
|
|
|
|
switch self {
|
|
|
|
case .basic:
|
|
|
|
var txHeaders = headers ?? HTTPHeaders()
|
|
|
|
|
|
|
|
let keychain = Keychain(service: "xyz.sarsoo.music.login")
|
|
|
|
txHeaders.add(.authorization(username: keychain["username"] ?? "", password: keychain["password"] ?? ""))
|
|
|
|
return txHeaders
|
2022-08-08 20:40:38 +01:00
|
|
|
|
|
|
|
case .jwt:
|
|
|
|
var txHeaders = headers ?? HTTPHeaders()
|
|
|
|
|
|
|
|
let keychain = Keychain(service: "xyz.sarsoo.music.login")
|
|
|
|
txHeaders.add(.authorization(bearerToken: keychain["jwt"] ?? ""))
|
|
|
|
return txHeaders
|
|
|
|
|
|
|
|
case .none:
|
|
|
|
let txHeaders = headers ?? HTTPHeaders()
|
|
|
|
|
|
|
|
return txHeaders
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
struct RequestBuilder {
|
|
|
|
static func buildRequest(apiRequest: ApiRequest) -> Alamofire.DataRequest {
|
2020-02-18 01:44:30 +00:00
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
let txHeaders = apiRequest.authMethod?.auth(headers: apiRequest.headers)
|
2020-02-18 01:44:30 +00:00
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
if apiRequest.parameters != nil {
|
|
|
|
if apiRequest.parameterType != nil {
|
|
|
|
|
|
|
|
let txEncoder = apiRequest.parameterType ?? JSONParameterEncoder.default
|
|
|
|
|
|
|
|
return AF.request(apiRequest.domain + apiRequest.path,
|
|
|
|
method: apiRequest.httpMethod,
|
|
|
|
parameters: apiRequest.parameters,
|
|
|
|
encoder: txEncoder,
|
|
|
|
headers: txHeaders)
|
|
|
|
} else {
|
|
|
|
return AF.request(apiRequest.domain + apiRequest.path,
|
|
|
|
method: apiRequest.httpMethod,
|
|
|
|
parameters: apiRequest.parameters,
|
|
|
|
headers: txHeaders)
|
|
|
|
}
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
2020-02-18 21:43:30 +00:00
|
|
|
return AF.request(apiRequest.domain + apiRequest.path,
|
|
|
|
method: apiRequest.httpMethod,
|
|
|
|
headers: txHeaders)
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:43:30 +00:00
|
|
|
struct ApiRequestDefaults {
|
2022-08-08 20:40:38 +01:00
|
|
|
static let authMethod: AuthMethod = .jwt
|
2022-04-21 13:59:29 +01:00
|
|
|
static var domain: String { get {
|
|
|
|
|
2022-08-07 19:34:14 +01:00
|
|
|
let default_url = "https://mixonomer.sarsoo.xyz/"
|
2022-04-21 13:59:29 +01:00
|
|
|
|
|
|
|
switch getenv("MTOOLS_SERVER") {
|
|
|
|
case .none:
|
|
|
|
return default_url
|
|
|
|
case .some(let url):
|
|
|
|
return String(utf8String: url) ?? default_url
|
|
|
|
}
|
|
|
|
} }
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protocol ApiRequest {
|
|
|
|
var domain: String { get }
|
|
|
|
var path: String { get }
|
|
|
|
var httpMethod: Alamofire.HTTPMethod { get }
|
|
|
|
var parameters: JSON? { get }
|
|
|
|
var parameterType: Alamofire.ParameterEncoder? { get }
|
|
|
|
var headers: HTTPHeaders? { get }
|
|
|
|
var authMethod: AuthMethod? { get }
|
2020-02-18 01:44:30 +00:00
|
|
|
}
|