Skip to content

Commit

Permalink
Add buildEither and URL concatenation with + (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-nnerb authored Nov 15, 2020
1 parent ab338d9 commit 2c44f5e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.1
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
8 changes: 8 additions & 0 deletions Sources/Request/Request/RequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public struct RequestBuilder {
}
return CombinedParams(children: [])
}

public static func buildEither(first: RequestParam) -> RequestParam {
return first
}

public static func buildEither(second: RequestParam) -> RequestParam {
return second
}
}
8 changes: 8 additions & 0 deletions Sources/Request/Request/RequestParams/Url/Url.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ public struct Url: RequestParam {
self.value = "\(type.rawValue)://\(url)"
}
}

public func + (_ url: Url, _ complementary: String) -> Url {
Url("\(url.value ?? "")\(complementary)")
}

public func + (_ lhs: Url, _ rhs: Url) -> Url {
Url("\(lhs.value ?? "")\(rhs.value ?? "")")
}
49 changes: 48 additions & 1 deletion Tests/RequestTests/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ final class RequestTests: XCTestCase {
))
})
}

func testQuery() {
performRequest(Request {
Url("https://jsonplaceholder.typicode.com/todos")
Expand All @@ -74,6 +74,53 @@ final class RequestTests: XCTestCase {
})
}

func testURLConcatenatedStringQuery() {
let baseUrl = Url("https://jsonplaceholder.typicode.com")
let todosEndpoint = "/todos"

performRequest(Request {
baseUrl + todosEndpoint
Method(.get)
Query(["userId":"1", "password": "2"])
Query([QueryParam("key", value: "value"), QueryParam("key2", value: "value2")])
})
}

func testURLConcatenatedURLQuery() {
let baseUrl = Url("https://jsonplaceholder.typicode.com")
let todosEndpoint = Url("/todos")

performRequest(Request {
baseUrl + todosEndpoint
Method(.get)
Query(["userId":"1", "password": "2"])
Query([QueryParam("key", value: "value"), QueryParam("key2", value: "value2")])
})
}

func testBuildEitherQuery() {
enum AuthProvider {
case explicity(userId: String, password: String)
case barrer(String)
}

let provider = AuthProvider.explicity(userId: "1", password: "2")

performRequest(Request {
Url("https://jsonplaceholder.typicode.com/todos")
Method(.get)

switch provider {
case .explicity(let userId, let password):
Query(["userId":userId, "password": password])
case .barrer(let token):
Header.Authorization(.bearer(token))
}

Query([QueryParam("key", value: "value"), QueryParam("key2", value: "value2")])
})
}

func testComplexRequest() {
performRequest(Request {
Url("https://jsonplaceholder.typicode.com/todos")
Expand Down

0 comments on commit 2c44f5e

Please sign in to comment.