Frank icon indicating copy to clipboard operation
Frank copied to clipboard

Query parameters

Open hsjunnesson opened this issue 8 years ago • 3 comments

Am I understanding it correctly that Frank doesn't include the notion of query parameters for routes? They're not part of Nest are they?

hsjunnesson avatar Apr 22 '16 09:04 hsjunnesson

I think this would be a part of https://github.com/nestproject/Inquiline, but it appears that it isn't in fact accounted for. I opened this: https://github.com/nestproject/Inquiline/issues/13

DrewHood avatar May 03 '16 13:05 DrewHood

Here, throw this is somewhere if you're looking for a quick fix for this.

import Nest

extension RequestType {

 /// Returns the query
public var query:Dictionary<String, String> {
    // Split the string at the ?
    // Then digest the key-value pairs
    let pathArr = self.path.characters.split{$0 == "?"}.map(String.init)
    let queryArr = pathArr[1].characters.split{$0 == "&"}.map(String.init)

    var queries:Dictionary<String, String> = [:]

    for (_, element) in queryArr.enumerate() {
      let keyValue = element.characters.split{$0 == "="}.map(String.init)
      queries[keyValue[0]] = keyValue[1]
    }

    return queries
}

}

You can now use request.query.

DrewHood avatar May 07 '16 13:05 DrewHood

My solution to adding query parameters include:

  1. Adding the following in Nest.swift to RequestType
var query: [String: String] { get }
  1. Adding the following import, property, function, and function call to Request.swift
// Top of file
import Foundation

// Property with path, body, content... etc
public var query = [String: String]()

// Inside the initializer
self.getQueries()

// Request function definition
mutating func getQueries() {
    let splitPath = self.path.components(separatedBy: "?")
    self.path = splitPath[0]

    guard splitPath.count > 1 else {
      return
    }

    let queries = splitPath[1].components(separatedBy: "&")

    for splitQuery in queries {
      let query = splitQuery.components(separatedBy: "=")

      guard query.count == 2 else {
        continue
      }

      self.query[query[0]] = query[1]
    }
  }

This solution removes the query from the path, and is accessible via: request.query as a [String: String]

Azoy avatar Jun 08 '17 16:06 Azoy