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

Commit

Permalink
Merge branch 'develop/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ishkawa committed Sep 21, 2016
2 parents 882833e + 1172d62 commit c51493e
Show file tree
Hide file tree
Showing 34 changed files with 1,604 additions and 416 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "Carthage/Checkouts/Alamofire"]
path = Carthage/Checkouts/Alamofire
url = https://github.com/Alamofire/Alamofire.git
[submodule "Carthage/Checkouts/Result"]
path = Carthage/Checkouts/Result
url = https://github.com/antitypical/Result.git
[submodule "Carthage/Checkouts/APIKit"]
path = Carthage/Checkouts/APIKit
url = https://github.com/ishkawa/APIKit.git
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: objective-c
osx_image: xcode7.3

env:
global:
- LC_CTYPE=en_US.UTF-8

script:
- xcodebuild test -workspace JSONRPCKit.xcworkspace -scheme JSONRPCKit-iOS -sdk iphonesimulator | xcpretty -c
- xcodebuild build -workspace JSONRPCKit.xcworkspace -scheme Example -sdk iphonesimulator | xcpretty -c

notifications:
email: false
2 changes: 1 addition & 1 deletion Cartfile.private
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "Alamofire/Alamofire" ~> 3.0
github "ishkawa/APIKit" ~> 2.0
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "Alamofire/Alamofire" "3.4.0"
github "antitypical/Result" "2.0.0"
github "antitypical/Result" "2.1.3"
github "ishkawa/APIKit" "2.0.5"
1 change: 1 addition & 0 deletions Carthage/Checkouts/APIKit
Submodule APIKit added at ee4283
1 change: 0 additions & 1 deletion Carthage/Checkouts/Alamofire
Submodule Alamofire deleted from d2f1f7
12 changes: 8 additions & 4 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
7FD0A33F1D4B5926007C9EAB /* APIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7FD0A33E1D4B5926007C9EAB /* APIKit.framework */; };
7FD0A3431D4B5E80007C9EAB /* UIViewController+Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD0A3421D4B5E80007C9EAB /* UIViewController+Error.swift */; };
CB31EFB11CF6E88100DD13DD /* JSONRPCKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB31EFB01CF6E88100DD13DD /* JSONRPCKit.framework */; };
CB31EFB21CF6E88100DD13DD /* JSONRPCKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = CB31EFB01CF6E88100DD13DD /* JSONRPCKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
CB31EFB41CF6E88800DD13DD /* Result.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB31EFB31CF6E88800DD13DD /* Result.framework */; };
CB31EFB51CF6E88800DD13DD /* Result.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = CB31EFB31CF6E88800DD13DD /* Result.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
CB31EFB71CF6E8B400DD13DD /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB31EFB61CF6E8B400DD13DD /* Alamofire.framework */; };
CB31EFB81CF6E8B400DD13DD /* Alamofire.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = CB31EFB61CF6E8B400DD13DD /* Alamofire.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
CB63E2471CE2309100E22B5C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB63E2461CE2309100E22B5C /* AppDelegate.swift */; };
CB63E24C1CE2309100E22B5C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CB63E24A1CE2309100E22B5C /* Main.storyboard */; };
CB63E24E1CE2309100E22B5C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CB63E24D1CE2309100E22B5C /* Assets.xcassets */; };
Expand All @@ -30,7 +30,6 @@
dstSubfolderSpec = 10;
files = (
CB31EFB21CF6E88100DD13DD /* JSONRPCKit.framework in Embed Frameworks */,
CB31EFB81CF6E8B400DD13DD /* Alamofire.framework in Embed Frameworks */,
CB31EFB51CF6E88800DD13DD /* Result.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
Expand All @@ -39,6 +38,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
7FD0A33E1D4B5926007C9EAB /* APIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = APIKit.framework; path = ../Carthage/Checkouts/APIKit/build/Debug/APIKit.framework; sourceTree = "<group>"; };
7FD0A3421D4B5E80007C9EAB /* UIViewController+Error.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Error.swift"; sourceTree = "<group>"; };
CB31EFB01CF6E88100DD13DD /* JSONRPCKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = JSONRPCKit.framework; path = "../Debug-iphonesimulator/JSONRPCKit.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
CB31EFB31CF6E88800DD13DD /* Result.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Result.framework; path = "../Debug-iphonesimulator/Result.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
CB31EFB61CF6E8B400DD13DD /* Alamofire.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Alamofire.framework; path = "../Debug-iphonesimulator/Alamofire.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -58,8 +59,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7FD0A33F1D4B5926007C9EAB /* APIKit.framework in Frameworks */,
CB31EFB11CF6E88100DD13DD /* JSONRPCKit.framework in Frameworks */,
CB31EFB71CF6E8B400DD13DD /* Alamofire.framework in Frameworks */,
CB31EFB41CF6E88800DD13DD /* Result.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -92,6 +93,7 @@
CB63E2581CE230E300E22B5C /* SingleRequestViewController.swift */,
CB63E25A1CE230E300E22B5C /* BatchRequestViewController.swift */,
CB63E2591CE230E300E22B5C /* MathService.swift */,
7FD0A3421D4B5E80007C9EAB /* UIViewController+Error.swift */,
CB63E2521CE2309100E22B5C /* Info.plist */,
CB63E24D1CE2309100E22B5C /* Assets.xcassets */,
CB63E24F1CE2309100E22B5C /* LaunchScreen.storyboard */,
Expand All @@ -102,6 +104,7 @@
CB63E26E1CE232C700E22B5C /* Frameworks */ = {
isa = PBXGroup;
children = (
7FD0A33E1D4B5926007C9EAB /* APIKit.framework */,
CB31EFB61CF6E8B400DD13DD /* Alamofire.framework */,
CB31EFB01CF6E88100DD13DD /* JSONRPCKit.framework */,
CB31EFB31CF6E88800DD13DD /* Result.framework */,
Expand Down Expand Up @@ -182,6 +185,7 @@
buildActionMask = 2147483647;
files = (
CB63E25C1CE230E300E22B5C /* MathService.swift in Sources */,
7FD0A3431D4B5E80007C9EAB /* UIViewController+Error.swift in Sources */,
CB63E25B1CE230E300E22B5C /* SingleRequestViewController.swift in Sources */,
CB63E25D1CE230E300E22B5C /* BatchRequestViewController.swift in Sources */,
CB63E2471CE2309100E22B5C /* AppDelegate.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone batchElement or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// If your application supports background execution, this method is batchElemented instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
// BatchElemented as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// BatchElemented when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


Expand Down
41 changes: 17 additions & 24 deletions Example/Example/BatchRequestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,43 @@

import UIKit
import JSONRPCKit
import APIKit

class BatchRequestViewController: UIViewController {

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


let batchFactory = BatchFactory()

func subtractAndmultiply(first: Int, _ second: Int) {
let jsonrpc = JSONRPC()

let subtractRequest = Subtract(
minuend: first,
subtrahend: second
)

jsonrpc.addRequest(subtractRequest) { [weak self] result in
switch result {
case .Success(let answer):
self?.subtractAnswerLabel.text = "\(answer)"
case .Failure:
self?.subtractAnswerLabel.text = "?"
}
}


let multiplyRequest = Multiply(
multiplicand: first,
multiplier: second
)

jsonrpc.addRequest(multiplyRequest) { [weak self] result in

let batch = batchFactory.create(subtractRequest, multiplyRequest)
let httpRequest = MathServiceRequest(batch: batch)

Session.sendRequest(httpRequest) { [weak self] result in
switch result {
case .Success(let answer):
self?.multiplyAnswerLabel.text = "\(answer)"
case .Failure:
case .Success(let subtractAnswer, let multiplyAnswer):
self?.subtractAnswerLabel.text = "\(subtractAnswer)"
self?.multiplyAnswerLabel.text = "\(multiplyAnswer)"

case .Failure(let error):
self?.subtractAnswerLabel.text = "?"
self?.multiplyAnswerLabel.text = "?"
self?.showAlertWithError(error)
}
}

MathServiceAPI.request(jsonrpc) { [weak self] error in
let alert = UIAlertController(title: error.localizedDescription, message: error.localizedRecoverySuggestion, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self?.presentViewController(alert, animated: true, completion: nil)
}
}

@IBAction func didPush(sender: AnyObject) {
Expand Down
101 changes: 54 additions & 47 deletions Example/Example/MathService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,43 @@
//

import Foundation
import APIKit
import JSONRPCKit
import Alamofire

// use https://github.com/jenolan/jsonrpcx-php/blob/master/examples/server.php

class MathServiceAPI {

static func request(jsonrpc: JSONRPC, errorHandler: ((error: NSError) -> Void)? = nil) {
guard let requestJSON = try? jsonrpc.buildRequestJSON() else {
return
}

print("request:", requestJSON, separator: "\n")

let URLRequest = NSMutableURLRequest()
URLRequest.URL = NSURL(string: "https://jsonrpckit-demo.appspot.com")!
URLRequest.HTTPMethod = "POST"
URLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLRequest.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(requestJSON, options: [])

Alamofire.request(URLRequest)
.responseJSON { response in
switch response.result {
case .Success(let responseJSON):
print("response:", responseJSON, separator: "\n")

do {
try jsonrpc.parseResponseJSON(responseJSON)
} catch {}

case .Failure(let error):
print("error:", error, separator: "\n")

if let errorHandler = errorHandler {
errorHandler(error: error)
}
}
}
struct MathServiceRequest<Batch: BatchType>: APIKit.RequestType {
let batch: Batch

typealias Response = Batch.Responses

var baseURL: NSURL {
return NSURL(string: "https://jsonrpckit-demo.appspot.com")!
}

var method: HTTPMethod {
return .POST
}

var path: String {
return "/"
}

var parameters: AnyObject? {
return batch.requestObject
}

func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) throws -> Response {
return try batch.responsesFromObject(object)
}
}

struct Subtract: RequestType {
struct CastError<ExpectedType>: ErrorType {
let actualValue: Any
let expectedType: ExpectedType.Type
}

struct Subtract: JSONRPCKit.RequestType {
typealias Response = Int

let minuend: Int
Expand All @@ -57,17 +52,21 @@ struct Subtract: RequestType {
var method: String {
return "subtract"
}
var params: AnyObject? {

var parameters: AnyObject? {
return [minuend, subtrahend]
}

func responseFromObject(object: AnyObject) -> Response? {
return object as? Response
func responseFromResultObject(resultObject: AnyObject) throws -> Response {
if let response = resultObject as? Response {
return response
} else {
throw CastError(actualValue: resultObject, expectedType: Response.self)
}
}
}

struct Multiply: RequestType {
struct Multiply: JSONRPCKit.RequestType {
typealias Response = Int

let multiplicand: Int
Expand All @@ -77,16 +76,20 @@ struct Multiply: RequestType {
return "multiply"
}

var params: AnyObject? {
var parameters: AnyObject? {
return [multiplicand, multiplier]
}

func responseFromObject(object: AnyObject) -> Response? {
return object as? Response
func responseFromResultObject(resultObject: AnyObject) throws -> Response {
if let response = resultObject as? Response {
return response
} else {
throw CastError(actualValue: resultObject, expectedType: Response.self)
}
}
}

struct Divide: RequestType {
struct Divide: JSONRPCKit.RequestType {
typealias Response = Float

let dividend: Int
Expand All @@ -96,11 +99,15 @@ struct Divide: RequestType {
return "divide"
}

var params: AnyObject? {
var parameters: AnyObject? {
return [dividend, divisor]
}

func responseFromObject(object: AnyObject) -> Response? {
return object as? Response
func responseFromResultObject(resultObject: AnyObject) throws -> Response {
if let response = resultObject as? Response {
return response
} else {
throw CastError(actualValue: resultObject, expectedType: Response.self)
}
}
}
Loading

0 comments on commit c51493e

Please sign in to comment.