-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa92f6d
commit 6ee9177
Showing
16 changed files
with
331 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "binance.png", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// Binance.swift | ||
// CoinQuotor | ||
// | ||
// Created by syaku on 2018/10/04. | ||
// Copyright © 2018年 syaku. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Moya | ||
import RxSwift | ||
|
||
class Binance: Ex { | ||
override var name: String { | ||
get { | ||
return "binance" | ||
} | ||
set {} | ||
} | ||
|
||
let provider = MoyaProvider<BinanceDataTarget>() | ||
|
||
private func request(target: BinanceDataTarget, completed: @escaping (Data?, Error?) -> () ) { | ||
provider.rx.request(target) | ||
.subscribe({ event in | ||
switch event { | ||
case let .success(response): | ||
completed(response.data, nil) | ||
case let .error(error): | ||
completed(nil, error) | ||
} | ||
}) | ||
.disposed(by: disposeBag) | ||
} | ||
|
||
override func getLastPrice(pair: String, completed: @escaping (Double?, Error?) -> ()) { | ||
request(target: .lastPrice(pair: pair)) { (res, err) in | ||
if res != nil { | ||
do { | ||
let symbol = try JSONDecoder().decode(Ticker.self, from: res! ) | ||
completed(Double(symbol.price), nil) | ||
} catch { | ||
completed(nil, APIError.castError("getLastPrice converting data error!")) | ||
} | ||
} else { | ||
completed(nil, err) | ||
} | ||
} | ||
} | ||
|
||
override func getPairs(completed: @escaping ([String]?, Error?) -> ()) { | ||
request(target: .pairs()) { (res, err) in | ||
if res != nil { | ||
do { | ||
let res = try JSONDecoder().decode(Symbols.self, from: res! ) | ||
let pairs = res.symbols.compactMap { symbol -> String? in | ||
return symbol.symbol | ||
} | ||
completed(pairs, nil) | ||
} catch { | ||
completed(nil, APIError.castError("getPairs converting data error!")) | ||
} | ||
} else { | ||
completed(nil, err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// BinanceDataTarget.swift | ||
// CoinQuotor | ||
// | ||
// Created by syaku on 2018/10/04. | ||
// Copyright © 2018年 syaku. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Moya | ||
import RxCocoa | ||
import RxSwift | ||
|
||
enum BinanceDataTarget { | ||
case lastPrice(pair: String) | ||
case pairs() | ||
} | ||
|
||
extension BinanceDataTarget: TargetType { | ||
var sampleData: Data { | ||
return "".data(using: String.Encoding.utf8)! | ||
} | ||
|
||
var baseURL:URL { | ||
return URL.init(string: "https://api.binance.com/api")! | ||
} | ||
|
||
var path: String { | ||
switch self { | ||
case .lastPrice(_): | ||
return "/v3/ticker/price" | ||
case .pairs(): | ||
return "/v1/exchangeInfo" | ||
} | ||
} | ||
|
||
var method: Moya.Method { | ||
return .get | ||
} | ||
|
||
var task: Task { | ||
switch self { | ||
case .lastPrice(let pair): | ||
return .requestParameters(parameters: [ | ||
"symbol": pair | ||
], encoding: URLEncoding.queryString) | ||
case .pairs: | ||
return .requestPlain | ||
} | ||
} | ||
|
||
var headers: [String : String]? { | ||
return ["Content-Type": "application/json"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Symbol.swift | ||
// CoinQuotor | ||
// | ||
// Created by syaku on 2018/10/04. | ||
// Copyright © 2018年 syaku. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Binance { | ||
struct Ticker: Codable { | ||
let symbol: String | ||
let price: String | ||
|
||
enum CodingKeys : String, CodingKey { | ||
case symbol = "symbol" | ||
case price = "price" | ||
} | ||
} | ||
|
||
struct Symbol: Codable { | ||
let symbol: String | ||
|
||
enum CodingKeys : String, CodingKey { | ||
case symbol = "symbol" | ||
} | ||
} | ||
|
||
struct Symbols: Codable { | ||
let symbols: [Symbol] | ||
|
||
enum CodingKeys : String, CodingKey { | ||
case symbols = "symbols" | ||
} | ||
} | ||
} |
Oops, something went wrong.