Skip to content

Commit

Permalink
Merge pull request #66 from futuredapp/feature/url-query-compatibility
Browse files Browse the repository at this point in the history
URL query compatibility
  • Loading branch information
mkj-is authored Jan 27, 2021
2 parents f4d9bfb + d07a51b commit 9ffd4b1
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
- uses: actions/checkout@v2
- name: Lint
run: |
brew install swiftlint
swiftlint --strict
- name: Pod lib lint
run: |
Expand Down
2 changes: 1 addition & 1 deletion FTAPIKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "FTAPIKit"
s.version = "1.2.0"
s.version = "1.2.1"
s.summary = "Declarative, generic and protocol-oriented REST API framework using URLSession and Codable"
s.description = <<-DESC
Protocol-oriented framework for communication with REST APIs.
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Futured apps s.r.o.
Copyright (c) 2021 Futured apps s.r.o.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ When using Swift package manager install using Xcode 11+
or add following line to your dependencies:

```swift
.package(url: "https://github.com/futuredapp/FTAPIKit.git", from: "1.2.0")
.package(url: "https://github.com/futuredapp/FTAPIKit.git", from: "1.2.1")
```

When using CocoaPods add following line to your `Podfile`:
Expand Down
6 changes: 3 additions & 3 deletions Sources/FTAPIKit/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public protocol Endpoint {

var headers: [String: String] { get }

var query: KeyValuePairs<String, String> { get }
var query: URLQuery { get }

/// HTTP method/verb describing the action.
var method: HTTPMethod { get }
}

public extension Endpoint {
var headers: [String: String] { [:] }
var query: KeyValuePairs<String, String> { [:] }
var query: URLQuery { URLQuery() }
var method: HTTPMethod { .get }
}

Expand All @@ -42,7 +42,7 @@ public protocol MultipartEndpoint: Endpoint {
}

public protocol URLEncodedEndpoint: Endpoint {
var body: KeyValuePairs<String, String> { get }
var body: URLQuery { get }
}

/// Endpoint protocol extending `Endpoint` having decodable associated type, which is used
Expand Down
23 changes: 23 additions & 0 deletions Sources/FTAPIKit/URLQuery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

public struct URLQuery: ExpressibleByDictionaryLiteral {
let items: [URLQueryItem]

init() {
self.items = []
}

public init(items: [(String, String)]) {
self.items = items.compactMap { key, value in
guard let encodedKey = key.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed),
let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed) else {
return nil
}
return URLQueryItem(name: encodedKey, value: encodedValue)
}
}

public init(dictionaryLiteral elements: (String, String)...) {
self.init(items: elements)
}
}
14 changes: 2 additions & 12 deletions Sources/FTAPIKit/URLRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct URLRequestBuilder<S: URLServer> {
func build() throws -> URLRequest {
let url = server.baseUri
.appendingPathComponent(endpoint.path)
.appendingQuery(parameters: queryItems(parameters: endpoint.query))
.appendingQuery(parameters: endpoint.query.items)
var request = URLRequest(url: url)

request.httpMethod = endpoint.method.description
Expand All @@ -37,7 +37,7 @@ struct URLRequestBuilder<S: URLServer> {
case let endpoint as URLEncodedEndpoint:
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
var urlComponents = URLComponents()
urlComponents.queryItems = queryItems(parameters: endpoint.body)
urlComponents.queryItems = endpoint.body.items
request.httpBody = urlComponents.query?.data(using: .ascii)
case let endpoint as MultipartEndpoint:
let formData = MultipartFormData(parts: endpoint.parts)
Expand All @@ -50,14 +50,4 @@ struct URLRequestBuilder<S: URLServer> {
break
}
}

private func queryItems(parameters: KeyValuePairs<String, String>) -> [URLQueryItem] {
parameters.compactMap { key, value in
guard let encodedKey = key.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed),
let encodedValue = value.addingPercentEncoding(withAllowedCharacters: .urlQueryNameValueAllowed) else {
return nil
}
return URLQueryItem(name: encodedKey, value: encodedValue)
}
}
}
2 changes: 1 addition & 1 deletion Tests/FTAPIKitTests/Mockups/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct TestMultipartEndpoint: MultipartEndpoint {
struct TestURLEncodedEndpoint: URLEncodedEndpoint {
let path = "post"
let method: HTTPMethod = .post
let body: KeyValuePairs<String, String> = [
let body: URLQuery = [
"param1": "value1",
"param2": "value2"
]
Expand Down

0 comments on commit 9ffd4b1

Please sign in to comment.