Skip to content

Commit 9461014

Browse files
committedSep 29, 2020
more work on swift client
1 parent 2e8295e commit 9461014

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
oto
1616
.DS_Store
1717
dist
18+
example/swift/SwiftCLIExample/SwiftCLIExample.xcodeproj/project.xcworkspace/xcuserdata/matryer.xcuserdatad/UserInterfaceState.xcuserstate

‎example/swift/SwiftCLIExample/SwiftCLIExample.xcodeproj/project.pbxproj

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
/* Begin PBXBuildFile section */
1010
0536E5D225222EB200E82696 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0536E5D125222EB200E82696 /* main.swift */; };
11-
0536E5DA25222F1F00E82696 /* Client.swift.plush in Sources */ = {isa = PBXBuildFile; fileRef = 0536E5D925222F1F00E82696 /* Client.swift.plush */; };
1211
/* End PBXBuildFile section */
1312

1413
/* Begin PBXCopyFilesBuildPhase section */
@@ -26,7 +25,6 @@
2625
/* Begin PBXFileReference section */
2726
0536E5CE25222EB200E82696 /* SwiftCLIExample */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftCLIExample; sourceTree = BUILT_PRODUCTS_DIR; };
2827
0536E5D125222EB200E82696 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
29-
0536E5D925222F1F00E82696 /* Client.swift.plush */ = {isa = PBXFileReference; lastKnownFileType = text; path = Client.swift.plush; sourceTree = "<group>"; };
3028
/* End PBXFileReference section */
3129

3230
/* Begin PBXFrameworksBuildPhase section */
@@ -60,7 +58,6 @@
6058
isa = PBXGroup;
6159
children = (
6260
0536E5D125222EB200E82696 /* main.swift */,
63-
0536E5D925222F1F00E82696 /* Client.swift.plush */,
6461
);
6562
path = SwiftCLIExample;
6663
sourceTree = "<group>";
@@ -123,7 +120,6 @@
123120
buildActionMask = 2147483647;
124121
files = (
125122
0536E5D225222EB200E82696 /* main.swift in Sources */,
126-
0536E5DA25222F1F00E82696 /* Client.swift.plush in Sources */,
127123
);
128124
runOnlyForDeploymentPostprocessing = 0;
129125
};

‎example/swift/SwiftCLIExample/SwiftCLIExample/main.swift

+38-11
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,63 @@ import Foundation
1010
let client = OtoClient(withEndpoint: "http://localhost:8080/oto")
1111
let service = MyService(withClient: client)
1212

13-
let resp = service.doSomething(request: DoSomethingRequest(
13+
service.doSomething(withRequest: DoSomethingRequest(
1414
name: "Mat"
15-
))
16-
print(resp.greeting)
15+
)) { (response, err) -> () in
16+
print("done")
17+
}
1718

1819
class OtoClient {
1920
var endpoint: String
2021
init(withEndpoint url: String) {
2122
self.endpoint = url
2223
}
23-
2424
}
2525

2626
class MyService {
2727
var client: OtoClient
2828
init(withClient client: OtoClient) {
2929
self.client = client
3030
}
31-
func doSomething(request: DoSomethingRequest) -> DoSomethingResponse {
32-
let resp = DoSomethingResponse(
33-
greeting: "Hi \(request.name)"
34-
)
35-
return resp
31+
func doSomething(withRequest doSomethingRequest: DoSomethingRequest, completion: (_ response: DoSomethingResponse?, _ error: Error?) -> ()) {
32+
//var request = URLRequest(url: URL(string: "\(self.client.endpoint)/MyService/MyMethod")!)
33+
// https://jsonplaceholder.typicode.com/todos/1
34+
var request = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com/todos/1")!)
35+
36+
request.httpMethod = "POST"
37+
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
38+
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
39+
var jsonData: Data
40+
do {
41+
jsonData = try JSONEncoder().encode(doSomethingRequest)
42+
} catch let jsonEncodeErr {
43+
print("TODO: handle JSON encode error: \(jsonEncodeErr)")
44+
return
45+
}
46+
request.httpBody = jsonData
47+
let session = URLSession(configuration: URLSessionConfiguration.default)
48+
let task = session.dataTask(with: request) { (data, response, error) in
49+
if let err = error {
50+
print("TODO: handle response error: \(err)")
51+
return
52+
}
53+
var doSomethingResponse: DoSomethingResponse
54+
do {
55+
doSomethingResponse = try JSONDecoder().decode(DoSomethingResponse.self, from: data!)
56+
} catch let err {
57+
print("TODO: handle JSON decode error: \(err)")
58+
return
59+
}
60+
print("\(doSomethingResponse)")
61+
}
62+
task.resume()
3663
}
3764
}
3865

39-
struct DoSomethingRequest {
66+
struct DoSomethingRequest: Encodable {
4067
var name: String = ""
4168
}
4269

43-
struct DoSomethingResponse {
70+
struct DoSomethingResponse: Decodable {
4471
var greeting: String = ""
4572
}

0 commit comments

Comments
 (0)
Please sign in to comment.