forked from shadowfacts/Tusker
67 lines
2.2 KiB
Swift
67 lines
2.2 KiB
Swift
//
|
|
// Request.swift
|
|
// Pachyderm
|
|
//
|
|
// Created by Shadowfacts on 9/8/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
struct Request<ResultType: Decodable> {
|
|
let method: Method
|
|
let path: String
|
|
let body: Body
|
|
var queryParameters: [Parameter]
|
|
|
|
init(method: Method, path: String, body: Body = .empty, queryParameters: [Parameter] = []) {
|
|
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
|
|
if let max = rangeParams.first(where: { $0.name == "max_id" }) {
|
|
if let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) {
|
|
queryParameters[i] = max
|
|
} else {
|
|
queryParameters.append(max)
|
|
}
|
|
}
|
|
if let since = rangeParams.first(where: { $0.name == "since_id" }) {
|
|
if let i = queryParameters.firstIndex(where: { $0.name == "since_id" }) {
|
|
queryParameters[i] = since
|
|
} else {
|
|
queryParameters.append(since)
|
|
}
|
|
}
|
|
if let count = rangeParams.first(where: { $0.name == "count" }) {
|
|
if let i = queryParameters.firstIndex(where: { $0.name == "count" }) {
|
|
queryParameters[i] = count
|
|
} else {
|
|
queryParameters.append(count)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|