tuyacloud

package module
Version: v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2021 License: MIT Imports: 17 Imported by: 0

README

tuyacloud

PkgGoDev Build Status Go Report Card GitHub license Release GitHub issues

This repo is MOVING to https://github.com/ekeynow/tuyacloud

unofficial Tuya Cloud API SDK.

Status

Project is under active development, API may changes in futures.

Feature

  • Go-style API design.
  • Full Feature support.
    • Authorization Management
    • User Management
    • Pair Device Management
    • Device Control
    • Device Timing Management
    • Smart Home Management
    • Scene Automation
    • Data Service
    • Weather Service
    • Device Management
    • OTA
    • Device Group
    • Universal Infrared
    • Smart Door Lock
    • Body Fat Scale
    • Sleep pace
    • IPC

Install

go get github.com/yunjuiot/tuyacloud

Contribute

Use issues for everything

  • For a small change, just send a PR.
  • For bigger changes open an issue for discussion before sending a PR.
  • PR should have:
    • Test case
    • Documentation
    • Example (If it makes sense)
  • You can also contribute by:
    • Reporting issues
    • Suggesting new features or enhancements
    • Improve/fix documentation

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HmacSha256

func HmacSha256(data, key string) string

HmacSha256 for HMAC-SHA-256 sign.

func Timestamp

func Timestamp() string

Timestamp returns timestamp format for randomize.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client for tuya cloud.

Example
package main

import (
	"github.com/go-log/log/log"
	"github.com/yunjuiot/tuyacloud"
	"github.com/yunjuiot/tuyacloud/user"
)

func main() {
	client := tuyacloud.NewClient(
		tuyacloud.APIEndpointUS,
		"1KAD46OrT9HafiKdsXeg",
		"4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC",
		tuyacloud.WithLogger(log.New()),
	)
	req := &user.QueryUserInfoRequest{
		UID: "123456",
	}
	var info user.QueryUserInfoResponse
	err := client.DoAndParse(req, &info)
	if err != nil {
		panic(err)
	}
	// Blah Blah Blah...
}
Output:

func NewClient

func NewClient(endpoint Endpoint, accessID, accessKey string, opts ...Option) (c *Client)

NewClient returns API client.

func (*Client) Do

func (c *Client) Do(r *http.Request) (res *http.Response, err error)

Do send HTTP request.

func (*Client) DoAndParse

func (c *Client) DoAndParse(r Request, resp interface{}) (err error)

DoAndParse = Do + Parse.

func (*Client) Parse

func (c *Client) Parse(res *http.Response, resp interface{}) error

Parse response body.

func (*Client) PlainSign

func (c *Client) PlainSign(timestamp string) string

PlainSign returns sign.

func (*Client) Request

func (c *Client) Request(r Request) (req *http.Request, err error)

Request to TUYA.

func (*Client) Token

func (c *Client) Token() (token string, err error)

Token returns access token.

func (*Client) TokenSign

func (c *Client) TokenSign(token, timestamp string) string

TokenSign returns token sign.

type Endpoint

type Endpoint string

Endpoint for API Call.

const (
	// APIEndpointCN prefer to China.
	APIEndpointCN Endpoint = "https://openapi.tuyacn.com"
	// APIEndpointUS prefer to America.
	APIEndpointUS Endpoint = "https://openapi.tuyaus.com"
	// APIEndpointEU prefer to Europe.
	APIEndpointEU Endpoint = "https://openapi.tuyaeu.com"
	// APIEndpointIN prefer to India.
	APIEndpointIN Endpoint = "https://openapi.tuyain.com"
)

type Error

type Error struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

Error info.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/yunjuiot/tuyacloud"
)

func main() {
	var (
		client *tuyacloud.Client
		r      tuyacloud.Request
		err    error
	)

	m := map[string]interface{}{}
	err = client.DoAndParse(r, &m)
	if err != nil {
		var apiErr tuyacloud.Error
		if errors.As(err, &apiErr) {
			fmt.Println(apiErr.Code, apiErr.Msg)
		}
	}
}
Output:

func (Error) Error

func (e Error) Error() string

type HTTPClient

type HTTPClient interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPClient interface.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore storage.

func (*MemoryStore) Refresh

func (s *MemoryStore) Refresh(c *Client) (err error)

Refresh token

func (*MemoryStore) Token

func (s *MemoryStore) Token() string

Token returns valid token.

type Option

type Option func(o *options)

Option settings.

func WithHTTPClient

func WithHTTPClient(c HTTPClient) Option

WithHTTPClient setup HTTPClient

func WithLogger

func WithLogger(l log.Logger) Option

WithLogger setup log.Logger interface.

func WithMaxRetries added in v0.2.0

func WithMaxRetries(i uint64) Option

WithMaxRetries setup max retries. Disable retries when i == 0.

func WithTokenStore

func WithTokenStore(s TokenStorage) Option

WithTokenStore setup token storage interface.

type Request

type Request interface {
	Method() string
	URL() string
}

Request for API call.

type RequestBody

type RequestBody interface {
	Body() interface{}
}

RequestBody for API.

type Response

type Response struct {
	Success   bool            `json:"success"`
	Code      int             `json:"code"`
	Msg       string          `json:"msg"`
	Timestamp int64           `json:"t"`
	Result    json.RawMessage `json:"result"`
}

Response body

type TokenRequest

type TokenRequest struct {
	// contains filtered or unexported fields
}

TokenRequest manages token refresh method.

func (*TokenRequest) Method

func (t *TokenRequest) Method() string

Method for Request.Method()

func (*TokenRequest) SetRefresh

func (t *TokenRequest) SetRefresh(token string)

SetRefresh refresh token.

func (*TokenRequest) URL added in v0.1.1

func (t *TokenRequest) URL() string

URL for Request.URL()

type TokenResponse

type TokenResponse struct {
	ExpireTime   int    `json:"expire_time"`
	UID          string `json:"uid"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

TokenResponse for token response from tuya server.

type TokenStorage

type TokenStorage interface {
	Token() string
	Refresh(c *Client) error
}

TokenStorage stores token.

Directories

Path Synopsis
Package automation for scene automation.
Package automation for scene automation.
catalog
bodyfat
Package bodyfat for Body Fat Scale
Package bodyfat for Body Fat Scale
infrared
Package infrared for Universal Infrared
Package infrared for Universal Infrared
ipc
Package ipc for IPC
Package ipc for IPC
sleep
Package sleep for Sleep pace
Package sleep for Sleep pace
smartlock
Package smartlock for Smart Door Lock interface.
Package smartlock for Smart Door Lock interface.
Package data for Data Service.
Package data for Data Service.
Package device implements Device Management API interface.
Package device implements Device Management API interface.
Package devicectl for Device Control
Package devicectl for Device Control
Package group for Device Group
Package group for Device Group
Package ota for OTA upgrade.
Package ota for OTA upgrade.
Package pairing for Device Pairing.
Package pairing for Device Pairing.
Package smarthome for Smart Home Management
Package smarthome for Smart Home Management
helpers
Package helpers is a generated GoMock package.
Package helpers is a generated GoMock package.
Package timing for Device Timing Management
Package timing for Device Timing Management
Package user for User Management
Package user for User Management
Package weather for Weather Service.
Package weather for Weather Service.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL