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 #1 from bricklife/request-id-generator
Browse files Browse the repository at this point in the history
add Request ID Generator
  • Loading branch information
Shinichiro Oba committed Nov 11, 2015
2 parents 484c7fb + 96f294e commit 1a8147b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
12 changes: 11 additions & 1 deletion Example/SingleRequestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
import UIKit
import JSONRPCKit

public class StringIdentifierGenerator: RequestIdentifierGenerator {

private var currentIdentifier = 1

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


class SingleRequestViewController: UIViewController {

@IBOutlet weak var firstTextField: UITextField!
@IBOutlet weak var secondTextField: UITextField!
@IBOutlet weak var subtractAnswerLabel: UILabel!

func subtract(first: Int, _ second: Int) {
let jsonrpc = JSONRPC()
let jsonrpc = JSONRPC(identifierGenerator: StringIdentifierGenerator())

let subtractRequest = Subtract(
userName: MathServiceAPI.userName,
Expand Down
12 changes: 12 additions & 0 deletions JSONRPCKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
CB36EB221BF24F1E003A4BCA /* AuthRequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */; };
CB36EB231BF25F45003A4BCA /* JSONRPCKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CBB0C34E1BF0B1D6006A7D41 /* JSONRPCKit.framework */; };
CB36EB241BF25F45003A4BCA /* JSONRPCKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = CBB0C34E1BF0B1D6006A7D41 /* JSONRPCKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
CB36EB281BF39028003A4BCA /* RequestIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB36EB271BF39028003A4BCA /* RequestIdentifier.swift */; };
CB82B9E21BF0C5CE00756CB1 /* JSONRPCError.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */; };
CBB0C3521BF0B1D6006A7D41 /* JSONRPCKit.h in Headers */ = {isa = PBXBuildFile; fileRef = CBB0C3511BF0B1D6006A7D41 /* JSONRPCKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB0C3591BF0B2D3006A7D41 /* RequestType.swift */; };
CBB0C35C1BF0B6FD006A7D41 /* JSONRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */; };
CBB4161A1BF3541F00B4DB0E /* BatchRequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB416181BF3534A00B4DB0E /* BatchRequestViewController.swift */; };
CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */; };
CBB4161E1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -70,13 +73,16 @@
CB36EB181BF243C7003A4BCA /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Result.framework; path = Carthage/Build/iOS/Result.framework; sourceTree = "<group>"; };
CB36EB1F1BF24497003A4BCA /* MathService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MathService.swift; sourceTree = "<group>"; };
CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthRequestType.swift; sourceTree = "<group>"; };
CB36EB271BF39028003A4BCA /* RequestIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestIdentifier.swift; sourceTree = "<group>"; };
CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONRPCError.swift; sourceTree = "<group>"; };
CBB0C34E1BF0B1D6006A7D41 /* JSONRPCKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSONRPCKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CBB0C3511BF0B1D6006A7D41 /* JSONRPCKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSONRPCKit.h; sourceTree = "<group>"; };
CBB0C3531BF0B1D7006A7D41 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
CBB0C3591BF0B2D3006A7D41 /* RequestType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestType.swift; sourceTree = "<group>"; };
CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONRPC.swift; sourceTree = "<group>"; };
CBB416181BF3534A00B4DB0E /* BatchRequestViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BatchRequestViewController.swift; sourceTree = "<group>"; };
CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestIdentifierGenerator.swift; sourceTree = "<group>"; };
CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NumberIdentifierGenerator.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -154,6 +160,9 @@
CB36EB211BF24F1E003A4BCA /* AuthRequestType.swift */,
CBB0C35B1BF0B6FD006A7D41 /* JSONRPC.swift */,
CB82B9E11BF0C5CE00756CB1 /* JSONRPCError.swift */,
CB36EB271BF39028003A4BCA /* RequestIdentifier.swift */,
CBB4161B1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift */,
CBB4161D1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift */,
CBB0C3531BF0B1D7006A7D41 /* Info.plist */,
);
path = JSONRPCKit;
Expand Down Expand Up @@ -286,6 +295,9 @@
buildActionMask = 2147483647;
files = (
CB82B9E21BF0C5CE00756CB1 /* JSONRPCError.swift in Sources */,
CBB4161E1BF370EA00B4DB0E /* NumberIdentifierGenerator.swift in Sources */,
CB36EB281BF39028003A4BCA /* RequestIdentifier.swift in Sources */,
CBB4161C1BF3599D00B4DB0E /* RequestIdentifierGenerator.swift in Sources */,
CB36EB221BF24F1E003A4BCA /* AuthRequestType.swift in Sources */,
CBB0C35A1BF0B2D3006A7D41 /* RequestType.swift in Sources */,
CBB0C35C1BF0B6FD006A7D41 /* JSONRPC.swift in Sources */,
Expand Down
29 changes: 22 additions & 7 deletions JSONRPCKit/JSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ public class JSONRPC {

public let version = "2.0"

private let identifierGenerator: RequestIdentifierGenerator

private var requests: [[String: AnyObject]] = []
private var handlers: [String: ([String: AnyObject]) -> Void] = [:]

public init() {
private var handlers: [RequestIdentifier: ([String: AnyObject]) -> Void] = [:]

public init(identifierGenerator: RequestIdentifierGenerator) {
self.identifierGenerator = identifierGenerator
}

public convenience init() {
self.init(identifierGenerator: NumberIdentifierGenerator())
}

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

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

Expand All @@ -43,13 +50,18 @@ public class JSONRPC {
self.requests.append(self.buildJSONFromRequest(request))
}

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

json["jsonrpc"] = self.version

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

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

if let id = json["id"] as? String, handle = self.handlers[id] {
if let id = json["id"] as? String, handle = self.handlers[.StringType(id)] {
handle(json)
}
if let id = json["id"] as? Int, handle = self.handlers[.NumberType(id)] {
handle(json)
}
}
Expand Down
18 changes: 18 additions & 0 deletions JSONRPCKit/NumberIdentifierGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// NumberIdentifierGenerator.swift
// JSONRPCKit
//
// Created by Shinichiro Oba on 2015/11/11.
// Copyright © 2015年 Shinichiro Oba. All rights reserved.
//

import Foundation

public class NumberIdentifierGenerator: RequestIdentifierGenerator {

private var currentIdentifier = 1

public func next() -> RequestIdentifier {
return .NumberType(self.currentIdentifier++)
}
}
35 changes: 35 additions & 0 deletions JSONRPCKit/RequestIdentifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// RequestIdentifier.swift
// JSONRPCKit
//
// Created by Shinichiro Oba on 11/12/15.
// Copyright © 2015 Shinichiro Oba. All rights reserved.
//

import Foundation

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

public var hashValue: Int {
switch self {
case NumberType(let number):
return number
case StringType(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 (.StringType(lstring), .StringType(rstring)) = (lhs, rhs) {
return lstring == rstring
}

return false
}
13 changes: 13 additions & 0 deletions JSONRPCKit/RequestIdentifierGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// RequestIdentifierGenerator.swift
// JSONRPCKit
//
// Created by Shinichiro Oba on 2015/11/11.
// Copyright © 2015年 Shinichiro Oba. All rights reserved.
//

import Foundation

public protocol RequestIdentifierGenerator {
func next() -> RequestIdentifier
}

0 comments on commit 1a8147b

Please sign in to comment.