Skip to content

Commit

Permalink
Merge pull request #107 from hyperoslo/feature/request-adding
Browse files Browse the repository at this point in the history
Improve: request modification
  • Loading branch information
vadymmarkov authored Aug 28, 2018
2 parents d282ec4 + 253044d commit 68b168d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Malibu.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Malibu"
s.summary = "A networking library built on promises."
s.version = "7.0.0"
s.version = "7.0.1"
s.homepage = "https://github.com/hyperoslo/Malibu"
s.license = 'MIT'
s.author = { "Hyper Interaktiv AS" => "ios@hyper.no" }
Expand Down
15 changes: 14 additions & 1 deletion MalibuTests/Specs/Request/RequestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ final class RequestSpec: QuickSpec {
request = Request.post(
"http://api.loc/posts",
parameters: ["key": "value"],
headers: ["key": "value"])
headers: ["key": "value"]
)
}

afterSuite {
Expand Down Expand Up @@ -263,6 +264,18 @@ final class RequestSpec: QuickSpec {
expect(request.key).to(equal("POST http://api.loc/posts"))
}
}

describe("#adding:parameters:headers") {
it("creates a new request by adding parameters and headers") {
let newRequest = request.adding(parameters: ["foo": "bar"], headers: ["header": "test"])
let expectedRequest = Request.post(
"http://api.loc/posts",
parameters: ["key": "value", "foo": "bar"],
headers: ["key": "value", "header": "test"]
)
expect(newRequest).to(equal(expectedRequest))
}
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ types, your custom `URLSessionConfiguration` goes here.
// Use this closure to modify your `Request` value before `URLRequest`
// is created on base of it
networking.beforeEach = { request in
var request = request
request.message.parameters["userId"] = "12345"

return request
return request.adding(
parameters: ["userId": "12345"],
headers: ["token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"]
)
}

// Use this closure to modify generated `URLRequest` object
Expand Down
29 changes: 29 additions & 0 deletions Sources/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,23 @@ public extension Request {
}
}

// MARK: - Factory helpers

public extension Request {
func adding(parameters: [String: Any], headers: [String: String]) -> Request {
return Request(
method: method,
resource: resource,
contentType: contentType,
task: task,
parameters: self.parameters.merged(with: parameters),
headers: self.headers.merged(with: headers),
storePolicy: storePolicy,
cachePolicy: cachePolicy
)
}
}

// MARK: - Equatable

public func == (lhs: Request, rhs: Request) -> Bool {
Expand All @@ -299,3 +316,15 @@ public func == (lhs: Request, rhs: Request) -> Bool {
&& lhs.storePolicy == rhs.storePolicy
&& lhs.cachePolicy == rhs.cachePolicy
}

// MARK: - Private

private extension Dictionary {
func merged(with dictionary: Dictionary) -> Dictionary {
var mergedDictionary = self
dictionary.forEach {
mergedDictionary.updateValue($1, forKey: $0)
}
return mergedDictionary
}
}

0 comments on commit 68b168d

Please sign in to comment.