Skip to content

Commit

Permalink
Adds properties
Browse files Browse the repository at this point in the history
  • Loading branch information
WCByrne committed Apr 5, 2018
1 parent c07d770 commit f76917d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ The config file defines the requests that should be made when running spark. The
> **cases** `Array`
>
> A list of requests to be made. See **[Cases](#cases)**
>
> **properties** `Dictionary`
>
> Values that can be reused in cases. Keys and values must be strings.
>
> Reference values in any string property of a request using `<key>`.

<a name="cases"></a>
Expand Down
83 changes: 69 additions & 14 deletions spark/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import Foundation

struct Config: Codable {
let service : URL
let cases : [Case]
let headers : [String:String]
let output : String?
let oauth: OAuth?
let properties: [String:String]?
let cases : [Case]

static func load(at path: String) throws -> Config {

Expand Down Expand Up @@ -50,21 +51,77 @@ struct Config: Codable {
}
}

extension String {

func range(with range: NSRange) -> Range<String.Index> {
let start = self.index(self.startIndex, offsetBy: range.location)
let end = self.index(start, offsetBy: range.length)
return Range<String.Index>(uncheckedBounds: (start, end))
}

}

extension Config {

func injectProperties(_ input: String) -> String {

let regex = try! NSRegularExpression(pattern: "<(.+?)>", options: [])
var fullRange = NSRange(location: 0, length: input.count)
var source = input

while let match = regex.firstMatch(in: source, options: [], range: fullRange) {
let oldLength = source.count

let keyRange = match.range(at: 1)
let key = String(source[source.range(with: keyRange)])
guard let replace = self.properties?[key] else {
return ""
}
source.replaceSubrange(source.range(with: match.range(at: 0)), with: replace)
let newLength = source.count

let loc = match.range.location + match.range.length + (newLength - oldLength)
fullRange = NSRange(location: loc,
length: newLength - loc)
}
return source
}

func injectProperties(_ input: JSON) -> JSON {
switch input {
case let .array(value):
return .array(value.map{ return injectProperties($0) })
case let .object(value):
return .object(value.mapValues { return injectProperties($0) })
case let .string(str):
return .string(injectProperties(str))
default:
return input
}
}

func request(for requestCase: Case) -> URLRequest {
var url = self.service.appendingPathComponent(requestCase.path)

var url = self.service.appendingPathComponent(self.injectProperties(requestCase.path))
if let q = requestCase.params {
url = url.addingQuery(q)
let params = q.mapValues { return self.injectProperties($0) }
url = url.addingQuery(params)
}

var request = URLRequest(url: url)
request.allHTTPHeaderFields = requestCase.headers
if let oauth = self.oauth {
var data: Data?
if let body = requestCase.body {
data = try? JSONEncoder().encode(body)
request.allHTTPHeaderFields = requestCase.headers?.mapValues {
return self.injectProperties($0)
}

let bodyData : Data? = {
if let b = requestCase.body {
let injected = self.injectProperties(b)
return try? JSONEncoder().encode(injected)
}
return nil
}()

if let oauth = self.oauth {
var token = oauth.token
if let tID = requestCase.token {
token = self.oauth?.tokens?[tID]
Expand All @@ -73,22 +130,18 @@ extension Config {
}
}
request.oAuthSign(method: requestCase.method,
body: data,
body: bodyData,
consumerCredentials: oauth.consumer.tup,
userCredentials: token?.tup)

}
else {
if let body = requestCase.body, let data = try? JSONEncoder().encode(body) {
request.httpBody = data
}
request.httpBody = bodyData
request.httpMethod = requestCase.method
}

return request
}


}


Expand Down Expand Up @@ -126,6 +179,8 @@ public enum JSON {
case null
}



extension JSON: Decodable {

public init(from decoder: Decoder) throws {
Expand Down
5 changes: 3 additions & 2 deletions spark/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ func createConfig(args: [String:Any]) {
]

let config = Config(service: URL(string: "http://api.myservice.com")!,
cases: cases,
headers: headers,
output: "./SparkResponses",
oauth: auth)
oauth: auth,
properties: [:],
cases: cases)


let encoder = JSONEncoder()
Expand Down

0 comments on commit f76917d

Please sign in to comment.