Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from bricklife/request-id-generator
Browse files Browse the repository at this point in the history
rename and add convenience methods
  • Loading branch information
Shinichiro Oba committed Nov 12, 2015
2 parents b0e19ac + 1521a92 commit 63c6b9a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Example/SingleRequestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class StringIdentifierGenerator: RequestIdentifierGenerator {
private var currentIdentifier = 1

public func next() -> RequestIdentifier {
return .StringType("\(self.currentIdentifier++)")
return .StringIdentifier("id\(self.currentIdentifier++)")
}
}

Expand Down
30 changes: 13 additions & 17 deletions JSONRPCKit/JSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ public class JSONRPC {
}

public func addRequest<T: RequestType>(request: T, handler: (Result<T.Response, JSONRPCError>) -> Void) {
let id = self.identifierGenerator.next()
let identifier = self.identifierGenerator.next()

self.requests.append(self.buildJSONFromRequest(request, id: id))
self.requests.append(self.buildJSONFromRequest(request, identifier: identifier))

self.handlers[id] = { (json: [String: AnyObject]) -> Void in
self.handlers[identifier] = { (json: [String: AnyObject]) -> Void in
if let result = json["result"] {
if let response = request.responseFromObject(result) {
handler(.Success(response))
} else {
handler(.Failure(JSONRPCError.InvalidResult(result)))
handler(.Failure(.InvalidResult(result)))
}
} else if let error = json["error"] {
handler(.Failure(JSONRPCError.ErrorRequest(error)))
handler(.Failure(.ErrorRequest(error)))
} else {
handler(.Failure(JSONRPCError.InvalidResponse(json)))
handler(.Failure(.InvalidResponse(json)))
}
}
}
Expand All @@ -50,18 +50,13 @@ public class JSONRPC {
self.requests.append(self.buildJSONFromRequest(request))
}

private func buildJSONFromRequest<T: RequestType>(request: T, id: RequestIdentifier? = nil) -> [String: AnyObject] {
private func buildJSONFromRequest<T: RequestType>(request: T, identifier: RequestIdentifier? = nil) -> [String: AnyObject] {
var json: [String: AnyObject] = request.buildJSON()

json["jsonrpc"] = self.version

if let id = id {
switch id {
case .StringType(let string):
json["id"] = string
case .NumberType(let number):
json["id"] = number
}
if let identifier = identifier {
json["id"] = identifier.value
}

return json
Expand Down Expand Up @@ -90,10 +85,11 @@ public class JSONRPC {
throw JSONRPCError.UnsupportedVersion(version)
}

if let id = json["id"] as? String, handle = self.handlers[.StringType(id)] {
handle(json)
guard let id = json["id"] else {
return
}
if let id = json["id"] as? Int, handle = self.handlers[.NumberType(id)] {

if let identifier = RequestIdentifier(value: id), handle = self.handlers[identifier] {
handle(json)
}
}
Expand Down
2 changes: 1 addition & 1 deletion JSONRPCKit/NumberIdentifierGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class NumberIdentifierGenerator: RequestIdentifierGenerator {
private var currentIdentifier = 1

public func next() -> RequestIdentifier {
return .NumberType(self.currentIdentifier++)
return .NumberIdentifier(self.currentIdentifier++)
}
}
52 changes: 43 additions & 9 deletions JSONRPCKit/RequestIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,61 @@

import Foundation

public enum RequestIdentifier: Hashable {
case NumberType(Int)
case StringType(String)
public enum RequestIdentifier {
case NumberIdentifier(Int)
case StringIdentifier(String)
}

extension RequestIdentifier {

public init(string: String) {
self = .StringIdentifier(string)
}

public init(number: Int) {
self = .NumberIdentifier(number)
}

public init?(value: AnyObject) {
switch value {
case let string as String:
self = .StringIdentifier(string)
case let number as Int:
self = .NumberIdentifier(number)
default:
return nil
}
}

public var value: AnyObject {
switch self {
case NumberIdentifier(let number):
return number
case StringIdentifier(let string):
return string
}
}
}

extension RequestIdentifier: Hashable {

public var hashValue: Int {
switch self {
case NumberType(let number):
case NumberIdentifier(let number):
return number
case StringType(let string):
case StringIdentifier(let string):
return string.hashValue
}
}
}

public func ==(lhs: RequestIdentifier, rhs: RequestIdentifier) -> Bool {
if case let (.NumberType(lnumber), .NumberType(rnumber)) = (lhs, rhs) {
return lnumber == rnumber
if case let (.NumberIdentifier(left), .NumberIdentifier(right)) = (lhs, rhs) {
return left == right
}

if case let (.StringType(lstring), .StringType(rstring)) = (lhs, rhs) {
return lstring == rstring
if case let (.StringIdentifier(left), .StringIdentifier(right)) = (lhs, rhs) {
return left == right
}

return false
Expand Down
1 change: 1 addition & 0 deletions JSONRPCKit/RequestIdentifierGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
import Foundation

public protocol RequestIdentifierGenerator {

func next() -> RequestIdentifier
}

0 comments on commit 63c6b9a

Please sign in to comment.