Documentation ¶
Overview ¶
SDK in Go for the CALLR API. Works with Go 1.16+, using standard packages only.
Note: This package may emit logs when errors occur when communicating with the API. The default logging function is log.Printf from the standard library. You can change the logging function with SetLogFunc.
Usage
package main import ( "context" "fmt" "os" callr "github.com/THECALLR/sdk-go/v2" ) func main() { // use Basic Auth (not recommended) // api := callr.NewWithBasicAuth("login", "password") // or use Api Key Auth (recommended) api := api.NewWithAPIKeyAuth("key") // optional: set a proxy // api.SetProxy("http://proxy:port") // check for destination phone number parameter if len(os.Args) < 2 { fmt.Println("Please supply destination phone number!") os.Exit(1) } // Example to send an SMS result, err := api.Call(context.Background(), "sms.send", "SMS", os.Args[1], "Hello, world", nil) // error management if err != nil { var jsonRpcError *callr.JSONRPCError if errors.As(err, &jsonRpcError) { fmt.Printf("API error: code:%d message:%s data:%v\n", jsonRpcError.Code, jsonRpcError.Message, jsonRpcError.Data) } else { fmt.Println("Transport error: ", err) } os.Exit(1) } fmt.Println(result) }
Index ¶
- func SetLogFunc(fn LogFunc) error
- type API
- func (api *API) Call(ctx context.Context, method string, params ...interface{}) (interface{}, error)
- func (api *API) ResetLoginAs()
- func (api *API) SetLoginAs(targetType LoginAsType, value string) error
- func (api *API) SetLoginAsSubAccountLogin(userLogin string) error
- func (api *API) SetLoginAsSubAccountRef(accountRef string) error
- func (api *API) SetProxy(proxy string) error
- func (api *API) SetURL(url string) error
- func (api *API) SetURLs(urls []string) error
- type JSONRPCError
- type LogFunc
- type LoginAsType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetLogFunc ¶
SetLogFunc can be used to change the default logger (log.Printf). Set to nil to disable package logging.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API represents a connection to the CALLR API.
func NewWithAPIKeyAuth ¶
NewWithAPIKeyAuth returns an API object with an API Key Authentication.
func NewWithBasicAuth ¶
NewWithBasicAuth returns an API object with Basic Authentication (not recommended). Use NewWithAPIKeyAuth auth instead.
func (*API) Call ¶
func (api *API) Call(ctx context.Context, method string, params ...interface{}) (interface{}, error)
Call sends a JSON-RPC 2.0 request to the CALLR API, and returns either a result or an error. The error may be of type *JSONRPCError if the error comes from the API, or a native error otherwise.
func (*API) ResetLoginAs ¶
func (api *API) ResetLoginAs()
ResetLoginAs removes the login-as configuration.
func (*API) SetLoginAs ¶
func (api *API) SetLoginAs(targetType LoginAsType, value string) error
SetLoginAs allows you to connect to the API as a sub account of yours, using different target types.
func (*API) SetLoginAsSubAccountLogin ¶
SetLoginAsSubAccountLogin allows you to connect to the API as a sub-account of yours, using the sub-account user "login" field.
func (*API) SetLoginAsSubAccountRef ¶
SetLoginAsSubAccountRef allows you to connect to the API as a sub-account of yours, using the sub-account "ref" field (sometimes called "hash").
type JSONRPCError ¶
type JSONRPCError struct { Code int64 `json:"code"` Message string `json:"message"` Data interface{} `json:"data"` }
JSONRPCError is a JSON-RPC 2.0 error, returned by the API. It satisfies the native error interface.
func (*JSONRPCError) Error ¶
func (e *JSONRPCError) Error() string
Error implements the error interface. Returns a string with the Code and Message properties.
type LoginAsType ¶
type LoginAsType string
const ( LoginAsAccountID LoginAsType = "account.id" LoginAsAccountRef LoginAsType = "account.hash" LoginAsAccountHash LoginAsType = "account.hash" LoginAsUserID LoginAsType = "user.id" LoginAsUserLogin LoginAsType = "user.login" )