diff options
author | Martin Polden <mpolden@mpolden.no> | 2018-02-17 17:17:35 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2018-02-17 17:17:35 +0100 |
commit | dedfccc3ad2273f1e5187a9ad5e26f68c6198ed9 (patch) | |
tree | d6bd9522c4f3a0c4408821a9b98323f5ece1c58c | |
parent | 3056253b8a4ca63836243edce43ea43c4f8ce83e (diff) |
Simplify
-rw-r--r-- | atb/atb.go | 20 | ||||
-rw-r--r-- | atb/rpc.go | 39 |
2 files changed, 28 insertions, 31 deletions
@@ -1,7 +1,6 @@ package atb import ( - "bytes" "encoding/json" "io/ioutil" "net/http" @@ -78,26 +77,21 @@ func NewFromConfig(name string) (Client, error) { return client, nil } -func (c *Client) post(m method, data interface{}) ([]byte, error) { - req, err := m.NewRequest(data) +func (c *Client) post(r request, data interface{}) ([]byte, error) { + body, err := r.Body(data) if err != nil { return nil, err } - buf := bytes.NewBufferString(req) - resp, err := http.Post(c.URL, "application/soap+xml", buf) + resp, err := http.Post(c.URL, "application/soap+xml", body) if err != nil { return nil, err } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + b, err := r.Decode(resp.Body) if err != nil { return nil, err } - out, err := m.Unmarshal(body) - if err != nil { - return nil, err - } - return out, nil + return b, nil } // BusStops retrieves bus stops from AtBs API. @@ -107,7 +101,7 @@ func (c *Client) BusStops() (BusStops, error) { Password string }{c.Username, c.Password} - res, err := c.post(busStopsList, values) + res, err := c.post(busStops, values) if err != nil { return BusStops{}, err } @@ -127,7 +121,7 @@ func (c *Client) Forecasts(nodeID int) (Forecasts, error) { NodeID int }{c.Username, c.Password, nodeID} - res, err := c.post(realTimeForecast, values) + res, err := c.post(forecast, values) if err != nil { return Forecasts{}, err } @@ -3,53 +3,56 @@ package atb import ( "bytes" "encoding/xml" + "io" "text/template" ) -type method interface { - NewRequest(data interface{}) (string, error) - Unmarshal(b []byte) ([]byte, error) +type request interface { + Body(data interface{}) (io.Reader, error) + Decode(r io.Reader) ([]byte, error) } -func compileTemplate(t *template.Template, data interface{}) (string, error) { +func compileTemplate(t *template.Template, data interface{}) (io.Reader, error) { var b bytes.Buffer if err := t.Execute(&b, data); err != nil { - return "", err + return nil, err } - return b.String(), nil + return &b, nil } -type busStopsListMethod struct { +type busStopsRequest struct { XMLName xml.Name `xml:"Envelope"` Result []byte `xml:"Body>GetBusStopsListResponse>GetBusStopsListResult"` template *template.Template } -type realTimeForecastMethod struct { +type forecastRequest struct { XMLName xml.Name `xml:"Envelope"` Result []byte `xml:"Body>getUserRealTimeForecastByStopResponse>getUserRealTimeForecastByStopResult"` template *template.Template } -func (m *busStopsListMethod) NewRequest(data interface{}) (string, error) { +func (m *busStopsRequest) Body(data interface{}) (io.Reader, error) { return compileTemplate(m.template, data) } -func (m *busStopsListMethod) Unmarshal(body []byte) ([]byte, error) { - var stops busStopsListMethod - if err := xml.Unmarshal(body, &stops); err != nil { +func (m *busStopsRequest) Decode(r io.Reader) ([]byte, error) { + var stops busStopsRequest + dec := xml.NewDecoder(r) + if err := dec.Decode(&stops); err != nil { return nil, err } return stops.Result, nil } -func (m *realTimeForecastMethod) NewRequest(data interface{}) (string, error) { +func (m *forecastRequest) Body(data interface{}) (io.Reader, error) { return compileTemplate(m.template, data) } -func (m *realTimeForecastMethod) Unmarshal(body []byte) ([]byte, error) { - var forecast realTimeForecastMethod - if err := xml.Unmarshal(body, &forecast); err != nil { +func (m *forecastRequest) Decode(r io.Reader) ([]byte, error) { + var forecast forecastRequest + dec := xml.NewDecoder(r) + if err := dec.Decode(&forecast); err != nil { return nil, err } return forecast.Result, nil @@ -60,7 +63,7 @@ func templateMust(src string) *template.Template { } var ( - busStopsList = &busStopsListMethod{ + busStops = &busStopsRequest{ template: templateMust(`<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> @@ -73,7 +76,7 @@ var ( </soap12:Body> </soap12:Envelope>`), } - realTimeForecast = &realTimeForecastMethod{ + forecast = &forecastRequest{ template: templateMust(`<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> |