-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
91 lines (74 loc) · 1.91 KB
/
Copy pathrequest.go
File metadata and controls
91 lines (74 loc) · 1.91 KB
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
package main
import (
"encoding/xml"
"errors"
"github.com/franela/goreq"
"io/ioutil"
"time"
)
// REQUEST
type SoapRequest struct {
address string
XMLName xml.Name `xml:"soapenv:Envelope"`
SoapenvNamespace1 string `xml:"xmlns:soapenv,attr"`
SoapenvNamespace2 string `xml:"xmlns:web,attr"`
Header RequestHeader
Body RequestBody
}
type RequestHeader struct {
XMLName xml.Name `xml:"soapenv:Header"`
}
type RequestBody struct {
XMLName xml.Name `xml:"soapenv:Body"`
Content RequestContent
}
type RequestContent interface {
}
type RequestContentGetGeoIp struct {
XMLName xml.Name `xml:"web:GetGeoIP"`
IpAddress string `xml:"web:IPAddress"`
}
func NewSoapRequest() (*SoapRequest, error) {
request := new(SoapRequest)
request.SoapenvNamespace1 = "http://schemas.xmlsoap.org/soap/envelope/"
request.SoapenvNamespace2 = "http://www.webservicex.net/"
return request, nil
}
func (this *SoapRequest) SetRequest(serviceAddress string, contentType string, content string) error {
this.address = serviceAddress
switch contentType {
case "GetGeoIp":
requestContent := new(RequestContentGetGeoIp)
requestContent.IpAddress = content
this.Body.Content = requestContent
break
default:
return errors.New("Unrecognized Request Type: " + contentType)
}
return nil
}
func (this *SoapRequest) Do() ([]byte, error) {
formattedXml, err := xml.Marshal(this)
if err != nil {
return nil, err
}
httpResponse, err := goreq.Request{
Method: "POST",
Uri: this.address,
ContentType: "text/xml;charset=UTF8",
Body: formattedXml,
Timeout: 30 * time.Second,
}.Do()
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode != 200 {
return nil, errors.New("Unable to retrieve status")
}
byteBody, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
return byteBody, nil
}