-
Notifications
You must be signed in to change notification settings - Fork 50
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
Showing
10 changed files
with
213 additions
and
58 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
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 %>? | ||
<% } %> | ||
} | ||
<% } %> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Binary file modified
BIN
-10.8 KB
(69%)
...odeproj/project.xcworkspace/xcuserdata/matryer.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
74 changes: 74 additions & 0 deletions
74
example/swift/SwiftCLIExample/SwiftCLIExample/client.gen.swift
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,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? | ||
|
||
} | ||
|
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,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 %>? | ||
<% } %> | ||
} | ||
<% } %> |