Skip to content

Commit

Permalink
Merge pull request #5 from gonzalezreal/direct-line
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
gonzalezreal committed Feb 18, 2020
2 parents d96a3d2 + 0ae26a2 commit a420dc3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ func popularItems() -> AnyPublisher<[MovieItem], Error> {
The `APIClient` class uses [SwiftLog](https://github.com/apple/swift-log) to log requests and responses. If you set its `logger.logLevel` to `.debug` you will start seeing requests and responses as they happen in your logs.

```Swift
let apiClient = APIClient(baseURL: URL(string: "https://api.themoviedb.org/3")!)
apiClient.logger.logLevel = .debug
let apiClient = APIClient(baseURL: URL(string: "https://api.themoviedb.org/3")!, logLevel: .debug)
```

Here is an example of the output using the default `StreamLogHandler`:
Expand Down
3 changes: 2 additions & 1 deletion Sources/SimpleNetworking/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public class APIClient {

private let session: URLSession

public init(baseURL: URL, configuration: APIClientConfiguration = APIClientConfiguration(), session: URLSession = URLSession(configuration: .default)) {
public init(baseURL: URL, configuration: APIClientConfiguration = APIClientConfiguration(), session: URLSession = URLSession(configuration: .default), logLevel: Logger.Level = .info) {
self.baseURL = baseURL
self.configuration = configuration
self.session = session
logger.logLevel = logLevel
}

public func response<Output>(for endpoint: Endpoint<Output>) -> AnyPublisher<Output, Error> {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SimpleNetworking/Data+LogDescription.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

internal extension Data {
public extension Data {
var logDescription: String {
String(data: prettyPrintedJSON ?? self, encoding: .utf8).map {
$0.components(separatedBy: .newlines)
Expand Down
9 changes: 6 additions & 3 deletions Sources/SimpleNetworking/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ public struct Endpoint<Output> {
public extension Endpoint where Output: Decodable {
init(method: Method,
path: String,
headers: [HeaderField: String] = [:],
queryParameters: [String: String] = [:],
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate) {
self.init(method: method,
path: path,
headers: [.accept: ContentType.json.rawValue],
headers: [.accept: ContentType.json.rawValue].merging(headers) { _, new in new },
queryParameters: queryParameters,
body: nil,
output: decode(with: dateDecodingStrategy))
}

init<Input>(method: Method,
path: String,
headers: [HeaderField: String] = [:],
body: Input,
dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate) where Input: Encodable {
self.init(method: method,
path: path,
headers: [.accept: ContentType.json.rawValue, .contentType: ContentType.json.rawValue],
headers: [.accept: ContentType.json.rawValue, .contentType: ContentType.json.rawValue].merging(headers) { _, new in new },
queryParameters: [:],
body: try! encode(body, with: dateEncodingStrategy),
output: decode(with: dateDecodingStrategy))
Expand All @@ -47,11 +49,12 @@ public extension Endpoint where Output: Decodable {
public extension Endpoint where Output == Void {
init<Input>(method: Method,
path: String,
headers: [HeaderField: String] = [:],
body: Input,
dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate) where Input: Encodable {
self.init(method: method,
path: path,
headers: [.contentType: ContentType.json.rawValue],
headers: [.contentType: ContentType.json.rawValue].merging(headers) { _, new in new },
queryParameters: [:],
body: try! encode(body, with: dateEncodingStrategy),
output: { _ in () })
Expand Down
15 changes: 11 additions & 4 deletions Tests/SimpleNetworkingTests/EndpointTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import XCTest
final class EndpointTest: XCTestCase {
func testEndpointWithoutQuery() {
// given
let endpoint = Endpoint<User>(method: .get, path: "test")
let endpoint = Endpoint<User>(method: .get, path: "test", headers: [.authorization: "Bearer 3xpo"])
var expected = URLRequest(url: Fixtures.anyURLWithPath("test"))
expected.addValue("Bearer 3xpo", forHTTPHeaderField: "Authorization")
expected.addValue(ContentType.json.rawValue, forHTTPHeaderField: "Accept")

// when
Expand All @@ -17,8 +18,9 @@ final class EndpointTest: XCTestCase {

func testEndpointWithQuery() {
// given
let endpoint = Endpoint<User>(method: .get, path: "test", queryParameters: ["foo": "bar"])
let endpoint = Endpoint<User>(method: .get, path: "test", headers: [.authorization: "Bearer 3xpo"], queryParameters: ["foo": "bar"])
var expected = URLRequest(url: Fixtures.anyURLWithPath("test", query: "foo=bar"))
expected.addValue("Bearer 3xpo", forHTTPHeaderField: "Authorization")
expected.addValue(ContentType.json.rawValue, forHTTPHeaderField: "Accept")

// when
Expand All @@ -31,11 +33,15 @@ final class EndpointTest: XCTestCase {
func testEndpointWithBodyAndOutput() {
// given
let user = User(name: "test")
let endpoint = Endpoint<User>(method: .post, path: "user/new", body: user)
let endpoint = Endpoint<User>(method: .post,
path: "user/new",
headers: [.authorization: "Bearer 3xpo"],
body: user)

var expected = URLRequest(url: Fixtures.anyURLWithPath("user/new"))
expected.httpMethod = "POST"
expected.httpBody = try! JSONEncoder().encode(user)
expected.addValue("Bearer 3xpo", forHTTPHeaderField: "Authorization")
expected.addValue(ContentType.json.rawValue, forHTTPHeaderField: "Accept")
expected.addValue(ContentType.json.rawValue, forHTTPHeaderField: "Content-Type")

Expand All @@ -49,11 +55,12 @@ final class EndpointTest: XCTestCase {
func testEndpointWithBody() {
// given
let user = User(name: "test")
let endpoint = Endpoint<Void>(method: .post, path: "user/new", body: user)
let endpoint = Endpoint<Void>(method: .post, path: "user/new", headers: [.authorization: "Bearer 3xpo"], body: user)

var expected = URLRequest(url: Fixtures.anyURLWithPath("user/new"))
expected.httpMethod = "POST"
expected.httpBody = try! JSONEncoder().encode(user)
expected.addValue("Bearer 3xpo", forHTTPHeaderField: "Authorization")
expected.addValue(ContentType.json.rawValue, forHTTPHeaderField: "Content-Type")

// when
Expand Down

0 comments on commit a420dc3

Please sign in to comment.