Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: path parameters in front of query #47 #48

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions PapyrusCore/Sources/RequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,23 @@ public struct RequestBuilder {
}

private func parameterizedPath() throws -> String {
try parameters.reduce(into: path.split(separator: "/")) { newPath, component in
var pathComponents = path.split(separator: "/")
var staticQuery: Substring? = nil

if let lastComponent = pathComponents.last, let startOfQuery = lastComponent.lastIndex(of: "?") {
pathComponents.removeLast()
staticQuery = lastComponent.suffix(from: startOfQuery)
pathComponents.append(lastComponent.prefix(upTo: startOfQuery))
}

return try parameters.reduce(into: pathComponents) { newPath, component in
print(newPath, component)
guard let index = newPath.firstIndex(of: ":\(component.key)") else {
throw PapyrusError("Tried to set path parameter `\(component.key)` but did not find `:\(component.key)` in path `\(path)`.")
}

newPath[index] = component.value[...]
}.joined(separator: "/")
}.joined(separator: "/") + (staticQuery ?? "")
}

private func bodyData() throws -> Data? {
Expand Down
13 changes: 13 additions & 0 deletions PapyrusCore/Tests/ParameterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ final class ParameterTests: XCTestCase {
req.addParameter("partTwo", value: "valueTwo")
XCTAssertEqual(try req.fullURL().absoluteString, "foo/bar/valueOne/valueTwo")
}

func testPathWithStaticQuery() {
var req = RequestBuilder(baseURL: "foo/", method: "GET", path: "bar/:baz?query=1")
req.addParameter("baz", value: "value")

XCTAssertEqual(try req.fullURL().absoluteString, "foo/bar/value?query=1")


var reqWithTermination = RequestBuilder(baseURL: "foo/", method: "GET", path: "bar/:baz/?query=1")
reqWithTermination.addParameter("baz", value: "value")

XCTAssertEqual(try reqWithTermination.fullURL().absoluteString, "foo/bar/value/?query=1")
}
}
Loading