forked from grpc-ecosystem/grpc-gateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EchoServiceApi.go
145 lines (120 loc) · 4.63 KB
/
EchoServiceApi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package echo
import (
"strings"
"fmt"
"encoding/json"
"errors"
"github.com/dghubble/sling"
)
type EchoServiceApi struct {
basePath string
}
func NewEchoServiceApi() *EchoServiceApi{
return &EchoServiceApi {
basePath: "http://localhost",
}
}
func NewEchoServiceApiWithBasePath(basePath string) *EchoServiceApi{
return &EchoServiceApi {
basePath: basePath,
}
}
/**
* Echo method receives a simple message and returns it.
* The message posted as the id parameter will also be\nreturned.
* @param id
* @return ExamplepbSimpleMessage
*/
//func (a EchoServiceApi) Echo (id string) (ExamplepbSimpleMessage, error) {
func (a EchoServiceApi) Echo (id string) (ExamplepbSimpleMessage, error) {
_sling := sling.New().Post(a.basePath)
// create path and map variables
path := "/v1/example/echo/{id}"
path = strings.Replace(path, "{" + "id" + "}", fmt.Sprintf("%v", id), -1)
_sling = _sling.Path(path)
// accept header
accepts := []string { "application/json" }
for key := range accepts {
_sling = _sling.Set("Accept", accepts[key])
break // only use the first Accept
}
var successPayload = new(ExamplepbSimpleMessage)
// We use this map (below) so that any arbitrary error JSON can be handled.
// FIXME: This is in the absence of this Go generator honoring the non-2xx
// response (error) models, which needs to be implemented at some point.
var failurePayload map[string]interface{}
httpResponse, err := _sling.Receive(successPayload, &failurePayload)
if err == nil {
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
if failurePayload != nil {
// If the failurePayload is present, there likely was some kind of non-2xx status
// returned (and a JSON payload error present)
var str []byte
str, err = json.Marshal(failurePayload)
if err == nil { // For safety, check for an error marshalling... probably superfluous
// This will return the JSON error body as a string
err = errors.New(string(str))
}
} else {
// So, there was no network-type error, and nothing in the failure payload,
// but we should still check the status code
if httpResponse == nil {
// This should never happen...
err = errors.New("No HTTP Response received.")
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
}
}
}
return *successPayload, err
}
/**
* EchoBody method receives a simple message and returns it.
*
* @param body
* @return ExamplepbSimpleMessage
*/
//func (a EchoServiceApi) EchoBody (body ExamplepbSimpleMessage) (ExamplepbSimpleMessage, error) {
func (a EchoServiceApi) EchoBody (body ExamplepbSimpleMessage) (ExamplepbSimpleMessage, error) {
_sling := sling.New().Post(a.basePath)
// create path and map variables
path := "/v1/example/echo_body"
_sling = _sling.Path(path)
// accept header
accepts := []string { "application/json" }
for key := range accepts {
_sling = _sling.Set("Accept", accepts[key])
break // only use the first Accept
}
// body params
_sling = _sling.BodyJSON(body)
var successPayload = new(ExamplepbSimpleMessage)
// We use this map (below) so that any arbitrary error JSON can be handled.
// FIXME: This is in the absence of this Go generator honoring the non-2xx
// response (error) models, which needs to be implemented at some point.
var failurePayload map[string]interface{}
httpResponse, err := _sling.Receive(successPayload, &failurePayload)
if err == nil {
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
if failurePayload != nil {
// If the failurePayload is present, there likely was some kind of non-2xx status
// returned (and a JSON payload error present)
var str []byte
str, err = json.Marshal(failurePayload)
if err == nil { // For safety, check for an error marshalling... probably superfluous
// This will return the JSON error body as a string
err = errors.New(string(str))
}
} else {
// So, there was no network-type error, and nothing in the failure payload,
// but we should still check the status code
if httpResponse == nil {
// This should never happen...
err = errors.New("No HTTP Response received.")
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
}
}
}
return *successPayload, err
}