Skip to content

Commit 9ffd4b1

Browse files
authored
Merge pull request #66 from futuredapp/feature/url-query-compatibility
URL query compatibility
2 parents f4d9bfb + d07a51b commit 9ffd4b1

8 files changed

Lines changed: 32 additions & 20 deletions

File tree

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jobs:
1111
- uses: actions/checkout@v2
1212
- name: Lint
1313
run: |
14-
brew install swiftlint
1514
swiftlint --strict
1615
- name: Pod lib lint
1716
run: |

FTAPIKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "FTAPIKit"
3-
s.version = "1.2.0"
3+
s.version = "1.2.1"
44
s.summary = "Declarative, generic and protocol-oriented REST API framework using URLSession and Codable"
55
s.description = <<-DESC
66
Protocol-oriented framework for communication with REST APIs.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Futured apps s.r.o.
3+
Copyright (c) 2021 Futured apps s.r.o.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ When using Swift package manager install using Xcode 11+
1616
or add following line to your dependencies:
1717

1818
```swift
19-
.package(url: "https://github.com/futuredapp/FTAPIKit.git", from: "1.2.0")
19+
.package(url: "https://github.com/futuredapp/FTAPIKit.git", from: "1.2.1")
2020
```
2121

2222
When using CocoaPods add following line to your `Podfile`:

Sources/FTAPIKit/Endpoint.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public protocol Endpoint {
1717

1818
var headers: [String: String] { get }
1919

20-
var query: KeyValuePairs<String, String> { get }
20+
var query: URLQuery { get }
2121

2222
/// HTTP method/verb describing the action.
2323
var method: HTTPMethod { get }
2424
}
2525

2626
public extension Endpoint {
2727
var headers: [String: String] { [:] }
28-
var query: KeyValuePairs<String, String> { [:] }
28+
var query: URLQuery { URLQuery() }
2929
var method: HTTPMethod { .get }
3030
}
3131

@@ -42,7 +42,7 @@ public protocol MultipartEndpoint: Endpoint {
4242
}
4343

4444
public protocol URLEncodedEndpoint: Endpoint {
45-
var body: KeyValuePairs<String, String> { get }
45+
var body: URLQuery { get }
4646
}
4747

4848
/// Endpoint protocol extending `Endpoint` having decodable associated type, which is used

Sources/FTAPIKit/URLQuery.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Foundation
2+
3+
public struct URLQuery: ExpressibleByDictionaryLiteral {
4+
let items: [URLQueryItem]
5+
6+
init() {
7+
self.items = []
8+
}
9+
10+
public init(items: [(String, String)]) {
11+
self.items = items.compactMap { key, value in
12+
guard let encodedKey = key.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed),
13+
let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed) else {
14+
return nil
15+
}
16+
return URLQueryItem(name: encodedKey, value: encodedValue)
17+
}
18+
}
19+
20+
public init(dictionaryLiteral elements: (String, String)...) {
21+
self.init(items: elements)
22+
}
23+
}

Sources/FTAPIKit/URLRequestBuilder.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct URLRequestBuilder<S: URLServer> {
1818
func build() throws -> URLRequest {
1919
let url = server.baseUri
2020
.appendingPathComponent(endpoint.path)
21-
.appendingQuery(parameters: queryItems(parameters: endpoint.query))
21+
.appendingQuery(parameters: endpoint.query.items)
2222
var request = URLRequest(url: url)
2323

2424
request.httpMethod = endpoint.method.description
@@ -37,7 +37,7 @@ struct URLRequestBuilder<S: URLServer> {
3737
case let endpoint as URLEncodedEndpoint:
3838
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
3939
var urlComponents = URLComponents()
40-
urlComponents.queryItems = queryItems(parameters: endpoint.body)
40+
urlComponents.queryItems = endpoint.body.items
4141
request.httpBody = urlComponents.query?.data(using: .ascii)
4242
case let endpoint as MultipartEndpoint:
4343
let formData = MultipartFormData(parts: endpoint.parts)
@@ -50,14 +50,4 @@ struct URLRequestBuilder<S: URLServer> {
5050
break
5151
}
5252
}
53-
54-
private func queryItems(parameters: KeyValuePairs<String, String>) -> [URLQueryItem] {
55-
parameters.compactMap { key, value in
56-
guard let encodedKey = key.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed),
57-
let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed) else {
58-
return nil
59-
}
60-
return URLQueryItem(name: encodedKey, value: encodedValue)
61-
}
62-
}
6353
}

Tests/FTAPIKitTests/Mockups/Endpoints.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct TestMultipartEndpoint: MultipartEndpoint {
7878
struct TestURLEncodedEndpoint: URLEncodedEndpoint {
7979
let path = "post"
8080
let method: HTTPMethod = .post
81-
let body: KeyValuePairs<String, String> = [
81+
let body: URLQuery = [
8282
"param1": "value1",
8383
"param2": "value2"
8484
]

0 commit comments

Comments
 (0)