2020-01-14 17:51:17 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2020-01-14 19:00:54 +00:00
|
|
|
"bytes"
|
2020-01-14 17:51:17 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
http *http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|
|
|
if c == nil {
|
|
|
|
c = http.DefaultClient
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{c, u}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) NewRequest(method string, p string, body io.Reader) (*http.Request, error) {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: c.endpoint.Scheme,
|
|
|
|
User: c.endpoint.User,
|
|
|
|
Host: c.endpoint.Host,
|
|
|
|
Path: path.Join(c.endpoint.Path, p),
|
|
|
|
}
|
|
|
|
return http.NewRequest(method, u.String(), body)
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:00:54 +00:00
|
|
|
func (c *Client) NewXMLRequest(method string, p string, v interface{}) (*http.Request, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(xml.Header)
|
|
|
|
enc := xml.NewEncoder(&buf)
|
|
|
|
if err := enc.Encode(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := enc.Flush(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := c.NewRequest(method, p, &buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:51:17 +00:00
|
|
|
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|
|
|
// TODO: remove this quirk
|
|
|
|
req.SetBasicAuth("simon", "")
|
|
|
|
return c.http.Do(req)
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
func (c *Client) DoMultiStatus(req *http.Request) (*Multistatus, error) {
|
2020-01-14 17:51:17 +00:00
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusMultiStatus {
|
|
|
|
return nil, fmt.Errorf("HTTP multi-status request failed: %v", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: the response can be quite large, support streaming Response elements
|
2020-01-14 20:29:54 +00:00
|
|
|
var ms Multistatus
|
2020-01-14 17:51:17 +00:00
|
|
|
if err := xml.NewDecoder(resp.Body).Decode(&ms); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
return &ms, nil
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|