-
Notifications
You must be signed in to change notification settings - Fork 0
/
dweetio.go
87 lines (64 loc) · 1.36 KB
/
dweetio.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
package dweetio
import (
"net/http"
"strings"
"time"
)
//Return the current implementation version
func Version() string {
return "0.1.0"
}
//DweetIO struct
type Dweetio struct {
Key string
}
//Dynamic JSON struct
type JSON struct {
Thing string
Created time.Time
Content interface{}
}
//Base Dweet struct
type Base struct {
This string
By string
The string
}
//Miltiple Dweets struct
type Dweets struct {
Base
With []JSON
}
//Single Dweet struct
type Dweet struct {
Base
With JSON
}
//GET last dweet for a Thing
func (api *Dweetio) GetLastDweetFor(thing string) (dweets *Dweets, err error) {
uri := api.GetUri("/get/latest/dweet/for/", thing)
res, err := http.Get(uri)
err = api.ReadData(res, &dweets)
//If error
api.ReturnError(err)
return dweets, nil
}
//GET all dweet for a Thing
func (api *Dweetio) GetDweetsFor(thing string) (dweets *Dweets, err error) {
uri := api.GetUri("/get/dweets/for/", thing)
res, err := http.Get(uri)
err = api.ReadData(res, &dweets)
//If error
api.ReturnError(err)
return dweets, nil
}
//POST data to a Thing
func (api *Dweetio) DweetFor(thing string, data string) (dweet *Dweet, err error) {
uri := api.GetUri("/dweet/for/", thing)
json := strings.NewReader(data)
res, err := http.Post(uri, "application/json", json)
//If error
api.ReturnError(err)
err = api.ReadData(res, &dweet)
return dweet, nil
}