Skip to content

Commit

Permalink
fix: path parameters in front of query #47 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahKamara committed Mar 13, 2024
1 parent 7bda5d3 commit ab5f147
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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")
}
}

0 comments on commit ab5f147

Please sign in to comment.