-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleService.go
98 lines (84 loc) · 2.38 KB
/
SimpleService.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
package main
import (
"bytes"
"io"
"reflectsvc/misc"
)
const SEP = "/* ************************** */"
// SimpleService provides operations on strings.
type SimpleService interface {
Reverse(string) (string, error)
Reflect(request reflectRequest) reflectResponse
Convert(request ConvertRequest) (string, error)
Xml2Json(request xml2JsonRequest) x2jProxyData
Validate(request validateRequest) validateRequest
// Success(string) string
}
// simpleService is a concrete implementation of SimpleService
type simpleService struct{}
/*
func (simpleService) Success(s string) string {
return " "
}
*/
func (simpleService) Validate(v validateRequest) (vr validateRequest) {
return v
}
func (simpleService) Xml2Json(req xml2JsonRequest) (xjProxy x2jProxyData) {
if FlagDebug {
xLog.Printf("enter Xml2Json send request %s", req.MagicInternalGuid)
}
xjProxy.Code = 500
xjProxy.Status = "500 ERROR"
xjProxy.Body = nil
buf := bytes.NewBufferString(req.Json())
rsp, err := x2jProxy(req.Headers, buf)
if nil != err {
xLog.Printf("could not proxy json request to %s\n with data\n%s\n because %s",
FlagDest, req.Json(), err.Error())
if nil != rsp {
xLog.Printf("response: %v", rsp)
xjProxy.Code = rsp.StatusCode
xjProxy.Status = rsp.Status
} else {
xjProxy.Status = "No response from remote server"
return xjProxy
}
}
defer misc.DeferError(rsp.Body.Close)
xjProxy.Body, err = io.ReadAll(rsp.Body)
if nil != err {
xLog.Printf("json request to %s with data\n%s\n"+
"\tcould not read response body because %s",
FlagDest, req.Json(), err.Error())
xjProxy.Status = "failure"
xjProxy.Code = 501
}
xjProxy.Status = rsp.Status
xjProxy.Code = rsp.StatusCode
if FlagDebug || FlagVerbose {
xLog.Printf("\n%s\n%s\n%s", SEP, string(xjProxy.Body), SEP)
}
return xjProxy
}
func (simpleService) Reflect(request reflectRequest) reflectResponse {
if FlagDebug {
xLog.Printf("reflecting request:\n\t/* *** */\n%s\n\t/* *** */\n", string(request.Body))
}
return reflectResponse{Body: request.Body}
}
func (simpleService) Convert(req ConvertRequest) (string, error) {
xLog.Printf("\n%s\n%s\n%s\n%s\n", SEP, req.String(), req.Json(), SEP)
return req.Json(), nil
}
func (simpleService) Reverse(s string) (string, error) {
var r string
if "" == s {
return "", ErrEmpty
}
for _, c := range s {
r = string(c) + r
}
xLog.Printf("reversed a string %s to %s", s, r)
return r, nil
}