2018-09-11 14:52:21 +00:00
|
|
|
//
|
|
|
|
// Request.swift
|
|
|
|
// Pachyderm
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/8/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2018-09-17 23:22:37 +00:00
|
|
|
public struct Request<ResultType: Decodable> {
|
2018-09-11 14:52:21 +00:00
|
|
|
let method: Method
|
|
|
|
let path: String
|
|
|
|
let body: Body
|
|
|
|
var queryParameters: [Parameter]
|
|
|
|
|
2020-09-15 03:25:26 +00:00
|
|
|
init(method: Method, path: String, body: Body = EmptyBody(), queryParameters: [Parameter] = []) {
|
2018-09-11 14:52:21 +00:00
|
|
|
self.method = method
|
|
|
|
self.path = path
|
|
|
|
self.body = body
|
|
|
|
self.queryParameters = queryParameters
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Request {
|
|
|
|
var range: RequestRange {
|
|
|
|
get {
|
|
|
|
let max = queryParameters.first { $0.name == "max_id" }
|
|
|
|
let since = queryParameters.first { $0.name == "since_id" }
|
|
|
|
let count = queryParameters.first { $0.name == "count" }
|
|
|
|
if let max = max, let count = count {
|
|
|
|
return .before(id: max.value!, count: Int(count.value!)!)
|
|
|
|
} else if let since = since, let count = count {
|
|
|
|
return .after(id: since.value!, count: Int(count.value!)!)
|
|
|
|
} else if let count = count {
|
|
|
|
return .count(Int(count.value!)!)
|
|
|
|
} else {
|
|
|
|
return .default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
let rangeParams = newValue.queryParameters
|
2019-07-31 23:33:48 +00:00
|
|
|
for param in rangeParams {
|
|
|
|
if let i = queryParameters.firstIndex(where: { $0.name == param.name }) {
|
|
|
|
queryParameters[i] = param
|
2018-09-14 15:35:00 +00:00
|
|
|
} else {
|
2019-07-31 23:33:48 +00:00
|
|
|
queryParameters.append(param)
|
2018-09-14 15:35:00 +00:00
|
|
|
}
|
2018-09-11 14:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|