1
0
mirror of synced 2024-11-29 00:25:39 +03:00
mg-transport-api-client-go/v1/client.go
2018-11-08 17:17:24 +03:00

478 lines
10 KiB
Go

package v1
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/google/go-querystring/query"
)
// New initialize client
func New(url string, token string) *MgClient {
return &MgClient{
URL: url,
Token: token,
httpClient: &http.Client{Timeout: 20 * time.Second},
}
}
// TransportChannels returns channels list
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.TransportChannels{Channels{Active: true}}
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("Status: %v, Channels found: %v", status, len(data))
func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int, error) {
var resp []ChannelListItem
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/channels?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// ActivateTransportChannel implement channel activation
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// request := ActivateRequest{
// Type: "telegram",
// Name: "@my_shopping_bot",
// Settings: ChannelSettings{
// SpamAllowed: false,
// Status: Status{
// Delivered: ChannelFeatureNone,
// Read: ChannelFeatureReceive,
// },
// Text: ChannelSettingsText{
// Creating: ChannelFeatureBoth,
// Editing: ChannelFeatureBoth,
// Quoting: ChannelFeatureReceive,
// Deleting: ChannelFeatureSend,
// },
// Product: Product{
// Creating: ChannelFeatureSend,
// Deleting: ChannelFeatureSend,
// },
// Order: Order{
// Creating: ChannelFeatureBoth,
// Deleting: ChannelFeatureSend,
// },
// },
// }
//
// data, status, err := client.ActivateTransportChannel(request)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.CreatedAt)
func (c *MgClient) ActivateTransportChannel(request Channel) (ActivateResponse, int, error) {
var resp ActivateResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PostRequest("/channels", []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// UpdateTransportChannel implement channel activation
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// request := ActivateRequest{
// ID: 3053450384,
// Type: "telegram",
// Name: "@my_shopping_bot",
// Settings: ChannelSettings{
// SpamAllowed: false,
// Status: Status{
// Delivered: ChannelFeatureNone,
// Read: ChannelFeatureReceive,
// },
// Text: ChannelSettingsText{
// Creating: ChannelFeatureBoth,
// Editing: ChannelFeatureSend,
// Quoting: ChannelFeatureReceive,
// Deleting: ChannelFeatureBoth,
// },
// Product: Product{
// Creating: ChannelFeatureSend,
// Deleting: ChannelFeatureSend,
// },
// Order: Order{
// Creating: ChannelFeatureBoth,
// Deleting: ChannelFeatureSend,
// },
// },
// }
//
// data, status, err := client.UpdateTransportChannel(request)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.UpdatedAt)
func (c *MgClient) UpdateTransportChannel(request Channel) (UpdateResponse, int, error) {
var resp UpdateResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PutRequest(fmt.Sprintf("/channels/%d", request.ID), []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// DeactivateTransportChannel implement channel deactivation
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.DeactivateTransportChannel(3053450384)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.DeactivatedAt)
func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, error) {
var resp DeleteResponse
var buf []byte
data, status, err := c.DeleteRequest(
fmt.Sprintf("/channels/%s", strconv.FormatUint(id, 10)),
buf,
)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// Messages implement send message
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
// msg := SendData{
// SendMessage{
// Message{
// ExternalID: "274628",
// Type: "text",
// Text: "hello!",
// },
// time.Now(),
// },
// User{
// ExternalID: "8",
// Nickname: "@octopus",
// Firstname: "Joe",
// },
// 10,
// }
//
// data, status, err := client.Messages(msg)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) {
var resp MessagesResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PostRequest("/messages", []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// UpdateMessages implement edit message
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
// msg := UpdateData{
// UpdateMessage{
// Message{
// ExternalID: "274628",
// Type: "text",
// Text: "hello hello!",
// },
// MakeTimestamp(),
// },
// 10,
// }
//
// data, status, err := client.UpdateMessages(msg)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) UpdateMessages(request EditMessageRequest) (MessagesResponse, int, error) {
var resp MessagesResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PutRequest("/messages", []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// MarkMessageRead send message read event to MG
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
// msg := MarkMessageReadRequest{
// Message{
// ExternalID: "274628",
// },
// 10,
// }
//
// data, status, err := client.MarkMessageRead(msg)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%v %v\n", status, data)
func (c *MgClient) MarkMessageRead(request MarkMessageReadRequest) (MarkMessageReadResponse, int, error) {
var resp MarkMessageReadResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PostRequest("/messages/read", []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// DeleteMessage implement delete message
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// msg := DeleteData{
// Message{
// ExternalID: "274628",
// },
// 10,
// }
//
// data, status, err := client.DeleteMessage(msg)
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) DeleteMessage(request DeleteData) (MessagesResponse, int, error) {
var resp MessagesResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.DeleteRequest(
"/messages",
[]byte(outgoing),
)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// GetFile implement get file url
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.GetFile("file_ID")
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) GetFile(request string) (FullFileResponse, int, error) {
var resp FullFileResponse
var b []byte
data, status, err := c.GetRequest(fmt.Sprintf("/files/%s", request), b)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// UploadFile upload file
func (c *MgClient) UploadFile(request []byte) (UploadFileResponse, int, error) {
var resp UploadFileResponse
data, status, err := c.PostRequest("/files/upload", request)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// UploadFileByURL upload file by url
func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileResponse, int, error) {
var resp UploadFileResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.PostRequest("/files/upload_by_url", []byte(outgoing))
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
func (c *MgClient) Error(info []byte) error {
var data map[string]interface{}
if err := json.Unmarshal(info, &data); err != nil {
return err
}
values := data["errors"].([]interface{})
return errors.New(values[0].(string))
}
// MakeTimestamp returns current unix timestamp
func MakeTimestamp() int64 {
return time.Now().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
}