diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 02:02:05 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 02:02:05 +0300 |
commit | 94c96cef29cd83bc60b2eabb8bb89c9fd8c7059b (patch) | |
tree | 676b3e2b6ffb171e102931e5fac01f7a4621c20a /subscriptions.go | |
parent | d13acd7da9bd1c08de33b91120a479df88d21db2 (diff) |
0.1.8 Release
Diffstat (limited to 'subscriptions.go')
-rw-r--r-- | subscriptions.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/subscriptions.go b/subscriptions.go new file mode 100644 index 0000000..d32d985 --- /dev/null +++ b/subscriptions.go @@ -0,0 +1,68 @@ +// Package tamtam implements TamTam Bot API +// Copyright (c) 2019 Alexander Kiryukhin <a.kiryukhin@mail.ru> +package tamtam + +import ( + "encoding/json" + "log" + "net/http" + "net/url" +) + +type subscriptions struct { + client *client +} + +func newSubscriptions(client *client) *subscriptions { + return &subscriptions{client: client} +} +func (a *subscriptions) GetSubscriptions() (*GetSubscriptionsResult, error) { + result := new(GetSubscriptionsResult) + values := url.Values{} + body, err := a.client.request(http.MethodGet, "subscriptions", values, nil) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *subscriptions) Subscribe(subscribeURL string, updateTypes []string) (*SimpleQueryResult, error) { + subscription := &SubscriptionRequestBody{ + Url: subscribeURL, + UpdateTypes: updateTypes, + Version: a.client.version, + } + result := new(SimpleQueryResult) + values := url.Values{} + body, err := a.client.request(http.MethodPost, "subscriptions", values, subscription) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} + +func (a *subscriptions) Unsubscribe(subscriptionURL string) (*SimpleQueryResult, error) { + result := new(SimpleQueryResult) + values := url.Values{} + values.Set("url", subscriptionURL) + body, err := a.client.request(http.MethodDelete, "subscriptions", values, nil) + if err != nil { + return result, err + } + defer func() { + if err := body.Close(); err != nil { + log.Println(err) + } + }() + return result, json.NewDecoder(body).Decode(result) +} |