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

Query parameters #7

Open
hsjunnesson opened this issue Apr 22, 2016 · 3 comments
Open

Query parameters #7

hsjunnesson opened this issue Apr 22, 2016 · 3 comments

Comments

@hsjunnesson
Copy link

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?

@DrewHood
Copy link

DrewHood commented May 3, 2016

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: nestproject/Inquiline#13

@DrewHood
Copy link

DrewHood commented May 7, 2016

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.

@Azoy
Copy link

Azoy commented Jun 8, 2017

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants