-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): Replace commands methods by RequestBuilder
- Loading branch information
Showing
4 changed files
with
94 additions
and
37 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
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,76 @@ | ||
// | ||
// RequestBuilder.swift | ||
// GomobileIPFS | ||
// | ||
// Created by Guilhem Fanton on 14/01/2020. | ||
// | ||
|
||
import Foundation | ||
import Ipfs | ||
|
||
public enum RequestOption { | ||
case Bool(Bool) | ||
case String(String) | ||
case Bytes(Data) | ||
} | ||
|
||
public enum RequestBody { | ||
case String(String) | ||
case Bytes(Data) | ||
} | ||
|
||
public class RequestBuilder { | ||
private let reqb: IpfsRequestBuilder | ||
|
||
internal init(reqb: IpfsRequestBuilder) { | ||
self.reqb = reqb | ||
} | ||
|
||
public func with(arg: String) -> RequestBuilder { | ||
self.reqb.argument(arg) | ||
return self | ||
} | ||
|
||
public func with(option: String, val: RequestOption) -> RequestBuilder { | ||
switch (val) { | ||
case .Bool(let bool): | ||
self.reqb.boolOptions(option, value: bool) | ||
case .String(let string): | ||
self.reqb.stringOptions(option, value: string) | ||
case .Bytes(let data): | ||
self.reqb.byteOptions(option, value: data) | ||
} | ||
|
||
return self | ||
} | ||
|
||
public func with(body: RequestBody) -> RequestBuilder { | ||
switch (body) { | ||
case .Bytes(let data): | ||
self.reqb.bodyBytes(data) | ||
case .String(let string): | ||
self.reqb.bodyString(string) | ||
} | ||
|
||
return self | ||
} | ||
|
||
public func withHeader(key: String, val: String) -> RequestBuilder { | ||
self.reqb.header(key, value: val) | ||
return self | ||
} | ||
|
||
public func send() throws -> Data { | ||
return try self.reqb.send() | ||
} | ||
|
||
public func sendToDict() throws -> [String: Any] { | ||
let res = try self.reqb.send() | ||
let json = try JSONSerialization.jsonObject(with: res, options: []) | ||
return json as! [String: Any] | ||
} | ||
|
||
public func exec() throws { | ||
try self.reqb.exec() | ||
} | ||
} |