Skip to content

Commit

Permalink
Merge pull request #5 from ktatroe/embedded_query_fix
Browse files Browse the repository at this point in the history
Handle case where endpoint path has embedded query string
  • Loading branch information
ktatroe committed Nov 22, 2016
2 parents 490797f + 9d5e748 commit 27464fe
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Horatio/Services/ServiceRequestConfigurator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public extension ServiceRequestConfigurator {
for transformer in endpointPathTransformers(serviceRequest) {
components = transformer.transformedPath(components)
}


// must do this after transforms have happened
rectifyEmbeddedQuery(components)

return components.URL

case .absolutePath(let urlString):
Expand All @@ -48,6 +51,32 @@ public extension ServiceRequestConfigurator {

return urlRequest
}

/**
Handles the case where there are query parameters embedded in the path. Properly
pulls them out and merges them into the query.
*/
private func rectifyEmbeddedQuery(components: NSURLComponents) {
guard let path = components.path, range = path.rangeOfString("?") else { return }

components.path = path.substringToIndex(range.startIndex)
let queryString = path.substringFromIndex(range.startIndex.advancedBy(1))

var queryItems = [NSURLQueryItem]()

if let existingQuery = components.queryItems {
queryItems.appendContentsOf(existingQuery)
}

// let it parse the query for us
components.query = queryString

if let newQuery = components.queryItems {
queryItems.appendContentsOf(newQuery)
}

components.queryItems = queryItems
}
}


Expand Down

0 comments on commit 27464fe

Please sign in to comment.