Skip to content

Commit

Permalink
finished swift client
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Sep 29, 2020
1 parent 0c82632 commit 70db1dc
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ oto
.DS_Store
dist
example/swift/SwiftCLIExample/SwiftCLIExample.xcodeproj/project.xcworkspace/xcuserdata/matryer.xcuserdatad/UserInterfaceState.xcuserstate
example/swift/SwiftCLIExample/SwiftCLIExample.xcodeproj/project.xcworkspace/xcuserdata/matryer.xcuserdatad/UserInterfaceState.xcuserstate
59 changes: 59 additions & 0 deletions example/client.swift.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Code generated by oto; DO NOT EDIT.

import Foundation

class OtoClient {
var endpoint: String
init(withEndpoint url: String) {
self.endpoint = url
}
}

<%= for (service) in def.Services { %>
<%= format_comment_text(service.Comment) %>class <%= service.Name %> {
var client: OtoClient
init(withClient client: OtoClient) {
self.client = client
}
<%= for (method) in service.Methods { %>
<%= format_comment_text(method.Comment) %> func <%= camelize_down(method.Name) %>(withRequest <%= camelize_down(method.InputObject.TypeName) %>: <%= method.InputObject.TypeName %>, completion: @escaping (_ response: <%= method.OutputObject.TypeName %>?, _ error: Error?) -> ()) {
var request = URLRequest(url: URL(string: "\(self.client.endpoint)/<%= service.Name %>.<%= method.Name %>")!)
request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
var jsonData: Data
do {
jsonData = try JSONEncoder().encode(<%= camelize_down(method.InputObject.TypeName) %>)
} catch let err {
completion(nil, err)
return
}
request.httpBody = jsonData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { (data, response, error) in
if let err = error {
completion(nil, err)
return
}
var <%= camelize_down(method.OutputObject.TypeName) %>: <%= method.OutputObject.TypeName %>
do {
<%= camelize_down(method.OutputObject.TypeName) %> = try JSONDecoder().decode(<%= method.OutputObject.TypeName %>.self, from: data!)
} catch let err {
completion(nil, err)
return
}
completion(<%= camelize_down(method.OutputObject.TypeName) %>, nil)
}
task.resume()
}
<% } %>
}
<% } %>

<%= for (object) in def.Objects { %>
<%= format_comment_text(object.Comment) %>struct <%= object.Name %>: Encodable, Decodable {
<%= for (field) in object.Fields { %>
<%= format_comment_text(field.Comment) %> var <%= camelize_down(field.Name) %>: <%= field.Type.SwiftType %>?
<% } %>
}
<% } %>
1 change: 1 addition & 0 deletions example/def/greeter_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package def

// GreeterService is a polite API for greeting people.
type GreeterService interface {
// Greet prepares a lovely greeting.
Greet(GreetRequest) GreetResponse
}

Expand Down
6 changes: 6 additions & 0 deletions example/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ oto -template client.js.plush \
-pkg main \
./def
echo "generated client.gen.js"

oto -template client.swift.plush \
-out ./swift/SwiftCLIExample/SwiftCLIExample/client.gen.swift \
-pkg main \
./def
echo "generated client.gen.swift"
3 changes: 1 addition & 2 deletions example/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0536E5D225222EB200E82696 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0536E5D125222EB200E82696 /* main.swift */; };
0536E5DF2523678400E82696 /* client.gen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0536E5DE2523678400E82696 /* client.gen.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -25,6 +26,7 @@
/* Begin PBXFileReference section */
0536E5CE25222EB200E82696 /* SwiftCLIExample */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftCLIExample; sourceTree = BUILT_PRODUCTS_DIR; };
0536E5D125222EB200E82696 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
0536E5DE2523678400E82696 /* client.gen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = client.gen.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -58,6 +60,7 @@
isa = PBXGroup;
children = (
0536E5D125222EB200E82696 /* main.swift */,
0536E5DE2523678400E82696 /* client.gen.swift */,
);
path = SwiftCLIExample;
sourceTree = "<group>";
Expand Down Expand Up @@ -119,6 +122,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0536E5DF2523678400E82696 /* client.gen.swift in Sources */,
0536E5D225222EB200E82696 /* main.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
74 changes: 74 additions & 0 deletions example/swift/SwiftCLIExample/SwiftCLIExample/client.gen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Code generated by oto; DO NOT EDIT.

import Foundation

class OtoClient {
var endpoint: String
init(withEndpoint url: String) {
self.endpoint = url
}
}


// GreeterService is a polite API for greeting people.
class GreeterService {
var client: OtoClient
init(withClient client: OtoClient) {
self.client = client
}

// Greet prepares a lovely greeting.
func greet(withRequest greetRequest: GreetRequest, completion: @escaping (_ response: GreetResponse?, _ error: Error?) -> ()) {
var request = URLRequest(url: URL(string: "\(self.client.endpoint)/GreeterService.Greet")!)
request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
var jsonData: Data
do {
jsonData = try JSONEncoder().encode(greetRequest)
} catch let err {
completion(nil, err)
return
}
request.httpBody = jsonData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { (data, response, error) in
if let err = error {
completion(nil, err)
return
}
var greetResponse: GreetResponse
do {
greetResponse = try JSONDecoder().decode(GreetResponse.self, from: data!)
} catch let err {
completion(nil, err)
return
}
completion(greetResponse, nil)
}
task.resume()
}

}



// GreetRequest is the request object for GreeterService.Greet.
struct GreetRequest: Encodable, Decodable {

// Name is the person to greet. It is required.
var name: String?

}

// GreetResponse is the response object containing a person's greeting.
struct GreetResponse: Encodable, Decodable {

// Greeting is a nice message welcoming somebody.
var greeting: String?

// Error is string explaining what went wrong. Empty if everything was fine.
var error: String?

}

64 changes: 8 additions & 56 deletions example/swift/SwiftCLIExample/SwiftCLIExample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,17 @@
import Foundation

let client = OtoClient(withEndpoint: "http://localhost:8080/oto")
let service = MyService(withClient: client)
let greeterService = GreeterService(withClient: client)

service.doSomething(withRequest: DoSomethingRequest(
greeterService.greet(withRequest: GreetRequest(
name: "Mat"
)) { (response, err) -> () in
print("done")
}

class OtoClient {
var endpoint: String
init(withEndpoint url: String) {
self.endpoint = url
if let err = err {
print("ERROR: \(err)")
return
}
print(response!.greeting!)
}

class MyService {
var client: OtoClient
init(withClient client: OtoClient) {
self.client = client
}
func doSomething(withRequest doSomethingRequest: DoSomethingRequest, completion: (_ response: DoSomethingResponse?, _ error: Error?) -> ()) {
//var request = URLRequest(url: URL(string: "\(self.client.endpoint)/MyService/MyMethod")!)
// https://jsonplaceholder.typicode.com/todos/1
var request = URLRequest(url: URL(string: "https://jsonplaceholder.typicode.com/todos/1")!)

request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
var jsonData: Data
do {
jsonData = try JSONEncoder().encode(doSomethingRequest)
} catch let jsonEncodeErr {
print("TODO: handle JSON encode error: \(jsonEncodeErr)")
return
}
request.httpBody = jsonData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { (data, response, error) in
if let err = error {
print("TODO: handle response error: \(err)")
return
}
var doSomethingResponse: DoSomethingResponse
do {
doSomethingResponse = try JSONDecoder().decode(DoSomethingResponse.self, from: data!)
} catch let err {
print("TODO: handle JSON decode error: \(err)")
return
}
print("\(doSomethingResponse)")
}
task.resume()
}
}

struct DoSomethingRequest: Encodable {
var name: String = ""
}

struct DoSomethingResponse: Decodable {
var greeting: String = ""
}
print("hi")
sleep(1)
59 changes: 59 additions & 0 deletions otohttp/templates/client.swift.plush
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Code generated by oto; DO NOT EDIT.

import Foundation

class OtoClient {
var endpoint: String
init(withEndpoint url: String) {
self.endpoint = url
}
}

<%= for (service) in def.Services { %>
<%= format_comment_text(service.Comment) %>class <%= service.Name %> {
var client: OtoClient
init(withClient client: OtoClient) {
self.client = client
}
<%= for (method) in service.Methods { %>
<%= format_comment_text(method.Comment) %> func <%= camelize_down(method.Name) %>(withRequest <%= camelize_down(method.InputObject.TypeName) %>: <%= method.InputObject.TypeName %>, completion: @escaping (_ response: <%= method.OutputObject.TypeName %>?, _ error: Error?) -> ()) {
var request = URLRequest(url: URL(string: "\(self.client.endpoint)/<%= service.Name %>.<%= method.Name %>")!)
request.httpMethod = "POST"
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
var jsonData: Data
do {
jsonData = try JSONEncoder().encode(<%= camelize_down(method.InputObject.TypeName) %>)
} catch let err {
completion(nil, err)
return
}
request.httpBody = jsonData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { (data, response, error) in
if let err = error {
completion(nil, err)
return
}
var <%= camelize_down(method.OutputObject.TypeName) %>: <%= method.OutputObject.TypeName %>
do {
<%= camelize_down(method.OutputObject.TypeName) %> = try JSONDecoder().decode(<%= method.OutputObject.TypeName %>.self, from: data!)
} catch let err {
completion(nil, err)
return
}
completion(<%= camelize_down(method.OutputObject.TypeName) %>, nil)
}
task.resume()
}
<% } %>
}
<% } %>

<%= for (object) in def.Objects { %>
<%= format_comment_text(object.Comment) %>struct <%= object.Name %>: Encodable, Decodable {
<%= for (field) in object.Fields { %>
<%= format_comment_text(field.Comment) %> var <%= camelize_down(field.Name) %>: <%= field.Type.SwiftType %>?
<% } %>
}
<% } %>

0 comments on commit 70db1dc

Please sign in to comment.