Skip to content

Commit 6bfe790

Browse files
committedSep 30, 2020
added status code check
1 parent 3b17704 commit 6bfe790

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed
 

‎example/client.swift.plush

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class OtoClient {
1717
}
1818
<%= for (method) in service.Methods { %>
1919
<%= 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?) -> ()) {
20-
var request = URLRequest(url: URL(string: "\(self.client.endpoint)/<%= service.Name %>.<%= method.Name %>")!)
20+
let url = "\(self.client.endpoint)/<%= service.Name %>.<%= method.Name %>"
21+
var request = URLRequest(url: URL(string: url)!)
2122
request.httpMethod = "POST"
2223
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
2324
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
@@ -35,6 +36,13 @@ class OtoClient {
3536
completion(nil, err)
3637
return
3738
}
39+
if let httpResponse = response as? HTTPURLResponse {
40+
if (httpResponse.statusCode != 200) {
41+
let err = OtoError("\(url): \(httpResponse.statusCode) status code")
42+
completion(nil, err)
43+
return
44+
}
45+
}
3846
var <%= camelize_down(method.OutputObject.TypeName) %>: <%= method.OutputObject.TypeName %>
3947
do {
4048
<%= camelize_down(method.OutputObject.TypeName) %> = try JSONDecoder().decode(<%= method.OutputObject.TypeName %>.self, from: data!)
@@ -57,3 +65,17 @@ class OtoClient {
5765
<% } %>
5866
}
5967
<% } %>
68+
69+
struct OtoError : LocalizedError
70+
{
71+
var errorDescription: String? { return message }
72+
var failureReason: String? { return message }
73+
var recoverySuggestion: String? { return "There is likely something wrong on the server." }
74+
var helpAnchor: String? { return "" }
75+
76+
private var message : String
77+
78+
init(_ description: String) {
79+
message = description
80+
}
81+
}

‎example/swift/SwiftCLIExample/SwiftCLIExample/client.gen.swift

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class GreeterService {
1919

2020
// Greet prepares a lovely greeting.
2121
func greet(withRequest greetRequest: GreetRequest, completion: @escaping (_ response: GreetResponse?, _ error: Error?) -> ()) {
22-
var request = URLRequest(url: URL(string: "\(self.client.endpoint)/GreeterService.Greet")!)
22+
let url = "\(self.client.endpoint)/GreeterService.Greet"
23+
var request = URLRequest(url: URL(string: url)!)
2324
request.httpMethod = "POST"
2425
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
2526
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
@@ -37,6 +38,13 @@ class GreeterService {
3738
completion(nil, err)
3839
return
3940
}
41+
if let httpResponse = response as? HTTPURLResponse {
42+
if (httpResponse.statusCode != 200) {
43+
let err = OtoError("\(url): \(httpResponse.statusCode) status code")
44+
completion(nil, err)
45+
return
46+
}
47+
}
4048
var greetResponse: GreetResponse
4149
do {
4250
greetResponse = try JSONDecoder().decode(GreetResponse.self, from: data!)
@@ -72,3 +80,17 @@ struct GreetResponse: Encodable, Decodable {
7280

7381
}
7482

83+
84+
struct OtoError : LocalizedError
85+
{
86+
var errorDescription: String? { return message }
87+
var failureReason: String? { return message }
88+
var recoverySuggestion: String? { return "There is likely something wrong on the server." }
89+
var helpAnchor: String? { return "" }
90+
91+
private var message : String
92+
93+
init(_ description: String) {
94+
message = description
95+
}
96+
}

‎otohttp/templates/client.swift.plush

+23-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class OtoClient {
1717
}
1818
<%= for (method) in service.Methods { %>
1919
<%= 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?) -> ()) {
20-
var request = URLRequest(url: URL(string: "\(self.client.endpoint)/<%= service.Name %>.<%= method.Name %>")!)
20+
let url = "\(self.client.endpoint)/<%= service.Name %>.<%= method.Name %>"
21+
var request = URLRequest(url: URL(string: url)!)
2122
request.httpMethod = "POST"
2223
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
2324
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Accept")
@@ -35,6 +36,13 @@ class OtoClient {
3536
completion(nil, err)
3637
return
3738
}
39+
if let httpResponse = response as? HTTPURLResponse {
40+
if (httpResponse.statusCode != 200) {
41+
let err = OtoError("\(url): \(httpResponse.statusCode) status code")
42+
completion(nil, err)
43+
return
44+
}
45+
}
3846
var <%= camelize_down(method.OutputObject.TypeName) %>: <%= method.OutputObject.TypeName %>
3947
do {
4048
<%= camelize_down(method.OutputObject.TypeName) %> = try JSONDecoder().decode(<%= method.OutputObject.TypeName %>.self, from: data!)
@@ -57,3 +65,17 @@ class OtoClient {
5765
<% } %>
5866
}
5967
<% } %>
68+
69+
struct OtoError : LocalizedError
70+
{
71+
var errorDescription: String? { return message }
72+
var failureReason: String? { return message }
73+
var recoverySuggestion: String? { return "There is likely something wrong on the server." }
74+
var helpAnchor: String? { return "" }
75+
76+
private var message : String
77+
78+
init(_ description: String) {
79+
message = description
80+
}
81+
}

0 commit comments

Comments
 (0)
Please sign in to comment.