Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Add Get/Post methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 5, 2019
1 parent 7d4820a commit 3767ea0
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
67 changes: 58 additions & 9 deletions volkszaehler/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

type Client interface {
Get(endpoint string) (io.Reader, error)
Post(endpoint string, payload string) (io.Reader, error)
QueryPublicEntities() []Entity
QueryEntity(entity string) Entity
QueryData(uuid string, from time.Time, to time.Time, group string, options string, tuples int) []Tuple
QueryPrognosis(uuid string, period string) Prognosis
}
Expand Down Expand Up @@ -75,7 +78,16 @@ func detectAPIEndpoint(url string) string {
return url
}

func (api *client) get(endpoint string) (io.Reader, error) {
func (api *client) debugResponseBody(resp *http.Response) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Print(err)
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body))
log.Print(string(body))
}

func (api *client) Get(endpoint string) (io.Reader, error) {
url := api.url + endpoint

start := time.Now()
Expand All @@ -97,27 +109,47 @@ func (api *client) get(endpoint string) (io.Reader, error) {
duration := time.Since(start)
log.Printf("GET %s (%dms)", url, duration.Nanoseconds()/1e6)

// read body
body, err := ioutil.ReadAll(resp.Body)
if api.debug {
api.debugResponseBody(resp)
}

return resp.Body, nil
}

func (api *client) Post(endpoint string, payload string) (io.Reader, error) {
url := api.url + endpoint

start := time.Now()
req, err := http.NewRequest("POST", url, strings.NewReader(payload))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Content-type", "application/json")
req.Header.Add("Accept", "application/json")

resp, err := api.client.Do(req)
if err != nil {
log.Print(err)
return nil, err
}
duration := time.Since(start)
log.Printf("POST %d %s (%dms)", resp.StatusCode, url, duration.Nanoseconds()/1e6)

if api.debug {
log.Print(string(body))
api.debugResponseBody(resp)
}

return bytes.NewReader(body), nil
return resp.Body, nil
}

// QueryPublicEntities retrieves public entities from middleware
func (api *client) QueryPublicEntities() []Entity {
r, err := api.get("/entity.json")
r, err := api.Get("/entity.json")
if err != nil {
return []Entity{}
}

er := EntityResponse{}
er := EntitiesResponse{}
if err := json.NewDecoder(r).Decode(&er); err != nil {
log.Printf("json decode failed: %v", err)
return []Entity{}
Expand All @@ -126,6 +158,23 @@ func (api *client) QueryPublicEntities() []Entity {
return er.Entities
}

// QueryEntity retrieves entitiy by uuid
func (api *client) QueryEntity(entity string) Entity {
context := fmt.Sprintf("/entity/%s.json", entity)
r, err := api.Get(context)
if err != nil {
return Entity{}
}

er := EntityResponse{}
if err := json.NewDecoder(r).Decode(&er); err != nil {
log.Printf("json decode failed: %v", err)
return Entity{}
}

return er.Entity
}

// QueryData retrieves data for specified timeframe and parameters
func (api *client) QueryData(uuid string, from time.Time, to time.Time,
group string, options string, tuples int,
Expand All @@ -146,7 +195,7 @@ func (api *client) QueryData(uuid string, from time.Time, to time.Time,
url += "&options=" + options
}

reader, err := api.get(url)
reader, err := api.Get(url)
if err != nil {
return []Tuple{}
}
Expand All @@ -164,7 +213,7 @@ func (api *client) QueryData(uuid string, from time.Time, to time.Time,
func (api *client) QueryPrognosis(uuid string, period string) Prognosis {
url := fmt.Sprintf("/prognosis/%s.json?period=%s", uuid, period)

r, err := api.get(url)
r, err := api.Get(url)
if err != nil {
return Prognosis{}
}
Expand Down
31 changes: 29 additions & 2 deletions volkszaehler/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package volkszaehler

import "encoding/json"

// EntityResponse is the middleware response to /entity.json
type EntityResponse struct {
// EntityType represent the entity types enum
type EntityType string

const (
// Channel is the Channel entity type
Channel EntityType = "channel"
// Group is the Aggregator entity type
Group EntityType = "group"
)

// EntitiesResponse is the middleware response to /entity.json
type EntitiesResponse struct {
Version string `json:"version"`
Entities []Entity `json:"entities"`
}

// EntityResponse is the middleware response to /entity/uuid.json
type EntityResponse struct {
Version string `json:"version"`
Entity Entity `json:"entity"`
}

// Entity is a single middleware entity
type Entity struct {
UUID string `json:"uuid"`
Expand Down Expand Up @@ -44,6 +60,17 @@ type Prognosis struct {
Fator float32 `json:"factor"`
}

type Exception struct {
Type string
Message string
Code int
}

type ErrorResponse struct {
Version string `json:"version"`
Exception Exception `json:"exception"`
}

// UnmarshalJSON converts volkszaehler tuple into Tuple struct
func (t *Tuple) UnmarshalJSON(b []byte) error {
var a []*json.RawMessage
Expand Down

0 comments on commit 3767ea0

Please sign in to comment.