2018-08-30 14:55:42 +00:00
|
|
|
package client
|
|
|
|
|
2018-09-25 19:18:30 +00:00
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <td/telegram/td_json_client.h>
|
|
|
|
*/
|
2018-08-30 14:55:42 +00:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2018-10-23 12:38:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
2018-08-30 14:55:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type JsonClient struct {
|
2018-10-23 12:38:10 +00:00
|
|
|
jsonClient unsafe.Pointer
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewJsonClient() *JsonClient {
|
2018-10-23 12:38:10 +00:00
|
|
|
return &JsonClient{
|
|
|
|
jsonClient: C.td_json_client_create(),
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends request to the TDLib client. May be called from any thread.
|
|
|
|
func (jsonClient *JsonClient) Send(req Request) {
|
2018-10-23 12:38:10 +00:00
|
|
|
data, _ := json.Marshal(req)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
query := C.CString(string(data))
|
|
|
|
defer C.free(unsafe.Pointer(query))
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
C.td_json_client_send(jsonClient.jsonClient, query)
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receives incoming updates and request responses from the TDLib client. May be called from any thread, but
|
|
|
|
// shouldn't be called simultaneously from two different threads.
|
|
|
|
// Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute
|
|
|
|
// in the same thread, so it can't be used after that.
|
2018-10-09 03:08:28 +00:00
|
|
|
func (jsonClient *JsonClient) Receive(timeout time.Duration) (*Response, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
result := C.td_json_client_receive(jsonClient.jsonClient, C.double(float64(timeout)/float64(time.Second)))
|
|
|
|
if result == nil {
|
|
|
|
return nil, errors.New("update receiving timeout")
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
data := []byte(C.GoString(result))
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
var resp Response
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
err := json.Unmarshal(data, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
resp.Data = data
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return &resp, nil
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronously executes TDLib request. May be called from any thread.
|
|
|
|
// Only a few requests can be executed synchronously.
|
|
|
|
// Returned pointer will be deallocated by TDLib during next call to td_json_client_receive or td_json_client_execute
|
|
|
|
// in the same thread, so it can't be used after that.
|
|
|
|
func (jsonClient *JsonClient) Execute(req Request) (*Response, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
data, _ := json.Marshal(req)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
query := C.CString(string(data))
|
|
|
|
defer C.free(unsafe.Pointer(query))
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
result := C.td_json_client_execute(jsonClient.jsonClient, query)
|
|
|
|
if result == nil {
|
|
|
|
return nil, errors.New("request can't be parsed")
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
data = []byte(C.GoString(result))
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
var resp Response
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
err := json.Unmarshal(data, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
resp.Data = data
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return &resp, nil
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys the TDLib client instance. After this is called the client instance shouldn't be used anymore.
|
2018-10-24 14:52:22 +00:00
|
|
|
func (jsonClient *JsonClient) Destroy() {
|
2018-10-23 12:38:10 +00:00
|
|
|
C.td_json_client_destroy(jsonClient.jsonClient)
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type meta struct {
|
2018-10-23 12:38:10 +00:00
|
|
|
Type string `json:"@type"`
|
|
|
|
Extra string `json:"@extra"`
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Request struct {
|
2018-10-23 12:38:10 +00:00
|
|
|
meta
|
|
|
|
Data map[string]interface{}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (req Request) MarshalJSON() ([]byte, error) {
|
2018-10-23 12:38:10 +00:00
|
|
|
req.Data["@type"] = req.Type
|
|
|
|
req.Data["@extra"] = req.Extra
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return json.Marshal(req.Data)
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2018-10-23 12:38:10 +00:00
|
|
|
meta
|
|
|
|
Data json.RawMessage
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseError struct {
|
2018-10-23 12:38:10 +00:00
|
|
|
Err *Error
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (responseError ResponseError) Error() string {
|
2018-10-23 12:38:10 +00:00
|
|
|
return fmt.Sprintf("%d %s", responseError.Err.Code, responseError.Err.Message)
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildResponseError(data json.RawMessage) error {
|
2018-10-23 12:38:10 +00:00
|
|
|
respErr, err := UnmarshalError(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return ResponseError{
|
|
|
|
Err: respErr,
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JsonInt64 alias for int64, in order to deal with json big number problem
|
|
|
|
type JsonInt64 int64
|
|
|
|
|
|
|
|
// MarshalJSON marshals to json
|
2019-11-05 20:11:39 +00:00
|
|
|
func (jsonInt64 JsonInt64) MarshalJSON() ([]byte, error) {
|
2019-11-06 17:23:19 +00:00
|
|
|
return []byte(`"` + strconv.FormatInt(int64(jsonInt64), 10) + `"`), nil
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals from json
|
|
|
|
func (jsonInt64 *JsonInt64) UnmarshalJSON(data []byte) error {
|
2018-10-23 12:38:10 +00:00
|
|
|
jsonBigInt, err := strconv.ParseInt(string(data[1:len(data)-1]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
*jsonInt64 = JsonInt64(jsonBigInt)
|
2018-08-30 14:55:42 +00:00
|
|
|
|
2018-10-23 12:38:10 +00:00
|
|
|
return nil
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Type interface {
|
2018-10-23 12:38:10 +00:00
|
|
|
GetType() string
|
|
|
|
GetClass() string
|
2018-08-30 14:55:42 +00:00
|
|
|
}
|