Help us shape the future of Go! Take the 2026 Go Developer Survey

alidayu

package module
v0.0.0-...-7f94a78 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2018 License: MIT Imports: 10 Imported by: 1

README

alidayu

Build Status Go Report Card GoDoc

package alidayu is a Golang SDK for alidayu(阿里大鱼) APIs.

Update: This package is obsolete because alidayu(阿里大鱼) is merged into aliyun(阿里云). Use package message instead.

Documentation
Example
    // Create a new client(创建一个新的Client实例).
    c := &alidayu.Client{AppKey: "", AppSecret: "", UseHTTPS: false}

    // ---------------------------------------
    // Send Verification Code in SMS(发送短信验证码).
    // ---------------------------------------
    // Set parameters(设置API所需的所有参数,包括公共参数和方法相关参数).
    params := map[string]string{}

    // Common parameters will be filled with default values automatically.
    // You may also set them to override default ones.
    // 公共参数会自动使用默认值填充,不需要用户设置.
    // 当然,用户可以使用自己的设置来覆盖默认值.
    //   params["format"] = "json"      // "json" or "xml"
    //   params["v"] = "2.0"            // "2.0" by default
    //   params["sign_method"] = "md5"  // "md5" or "hmac"

    // No need to set "timestamp" and "sign", these parameters will be calculated and filled automatically.
    // 不需要用户设置"timestamp"和"sign",这些参数会被自动计算和设置.

    // Set method specified parameters(设置方法相关特定参数).
    params["method"] = "alibaba.aliqin.fc.sms.num.send"           // Set method to send SMS(API接口名称).
    params["sms_type"] = "normal"                                 // Set SMS type(短信类型).
    params["sms_free_sign_name"] = ""                             // Set SMS signature(短信签名).
    params["sms_param"] = `{"code":"123456", "product":"My App"}` // Set variable for SMS template(短信模板变量).
    params["sms_template_code"] = ""                              // Set SMS template code(短信模板ID).
    params["rec_num"] = ""                                        // Set phone number to send SMS(短信接收号码).

    // Call Exec() to post the request.
    success, result, err := c.Exec(params)
    if err != nil {
            fmt.Fprintf(os.Stderr, "c.Exec() error: %v\nsuccess: %v\nresult: %v\n", err, success, result)
            return
    }

    fmt.Fprintf(os.Stderr, "c.Exec() successfully\nsuccess: %v\nresult: %s\n", success, result)

    // ------------------------------------------
    // Send Verification Code in Single Call(发送文本转语音通知验证码).
    // ------------------------------------------
    // Set Parameters.
    params = map[string]string{}
    params["method"] = "alibaba.aliqin.fc.tts.num.singlecall"     // Set method to make single call(API接口名称).
    params["tts_param"] = `{"code":"123456", "product":"My App"}` // Set variable for TTS template(文本转语音(TTS)模板变量).
    params["called_num"] = ""                                     // Set phone number to make single call(被叫号码).
    params["called_show_num"] = ""                                // Set show number(被叫号显).
    params["tts_code"] = ""                                       // Set TTS code(TTS模板ID).

    // Call Exec() to post the request.
    success, result, err = c.Exec(params)
    if err != nil {
            fmt.Fprintf(os.Stderr, "c.Exec() error: %v\nsuccess: %v\nresult: %v\n", err, success, result)
            return
    }

    fmt.Fprintf(os.Stderr, "c.Exec() successfully\nsuccess: %v\nresult: %s\n", success, result)
License

Documentation

Overview

Package alidayu is Golang SDK for alidayu APIs.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefCommonParams is default common parameters of alidayu APIs
	DefCommonParams = map[string]string{
		"format":      "json",
		"v":           "2.0",
		"sign_method": "md5",
	}
)

Functions

func GetSortedQueryStr

func GetSortedQueryStr(params map[string]string) (sortedQueryStr string)

GetSortedQueryStr sorts the keys of parameters and generate sorted query string which can be used to sign(MD5 or HMAC_MD5).

Types

type Client

type Client struct {
	AppKey    string // App Key
	AppSecret string // App Secret
	UseHTTPS  bool   // Use HTTPS URL or not
	http.Client
}

Client contains app key and secret and provides method to post HTTP request like http.Request.

func (*Client) Exec

func (c *Client) Exec(params map[string]string) (success bool, result string, err error)

Exec executes the REST API and get the response. It's a wrapper of Post().

Params:
  params: map that contains parameters of REST API. See official docs to fill the parameters.
Returns:
  success: If REST API succeeds.
  result: Raw response string of REST API.
Example

Example of Client.Exec()

package main

import (
	"fmt"
	"os"

	"github.com/northbright/alidayu"
)

func main() {
	// Create a new client(创建一个新的Client实例).
	c := &alidayu.Client{AppKey: "", AppSecret: "", UseHTTPS: false}

	// ---------------------------------------
	// Send Verification Code in SMS(发送短信验证码).
	// ---------------------------------------
	// Set parameters(设置API所需的所有参数,包括公共参数和方法相关参数).
	params := map[string]string{}

	// Common parameters will be filled with default values automatically.
	// You may also set them to override default ones.
	// 公共参数会自动使用默认值填充,不需要用户设置.
	// 当然,用户可以使用自己的设置来覆盖默认值.
	//   params["format"] = "json"      // "json" or "xml"
	//   params["v"] = "2.0"            // "2.0" by default
	//   params["sign_method"] = "md5"  // "md5" or "hmac"

	// No need to set "timestamp" and "sign", these parameters will be calculated and filled automatically.
	// 不需要用户设置"timestamp"和"sign",这些参数会被自动计算和设置.

	// Set method specified parameters(设置方法相关特定参数).
	params["method"] = "alibaba.aliqin.fc.sms.num.send"           // Set method to send SMS(API接口名称).
	params["sms_type"] = "normal"                                 // Set SMS type(短信类型).
	params["sms_free_sign_name"] = ""                             // Set SMS signature(短信签名).
	params["sms_param"] = `{"code":"123456", "product":"My App"}` // Set variable for SMS template(短信模板变量).
	params["sms_template_code"] = ""                              // Set SMS template code(短信模板ID).
	params["rec_num"] = ""                                        // Set phone number to send SMS(短信接收号码).

	// Call Exec() to post the request.
	success, result, err := c.Exec(params)
	if err != nil {
		fmt.Fprintf(os.Stderr, "c.Exec() error: %v\nsuccess: %v\nresult: %v\n", err, success, result)
		return
	}

	fmt.Fprintf(os.Stderr, "c.Exec() successfully\nsuccess: %v\nresult: %s\n", success, result)

	// ------------------------------------------
	// Send Verification Code in Single Call(发送文本转语音通知验证码).
	// ------------------------------------------
	// Set Parameters.
	params = map[string]string{}
	params["method"] = "alibaba.aliqin.fc.tts.num.singlecall"     // Set method to make single call(API接口名称).
	params["tts_param"] = `{"code":"123456", "product":"My App"}` // Set variable for TTS template(文本转语音(TTS)模板变量).
	params["called_num"] = ""                                     // Set phone number to make single call(被叫号码).
	params["called_show_num"] = ""                                // Set show number(被叫号显).
	params["tts_code"] = ""                                       // Set TTS code(TTS模板ID).

	// Call Exec() to post the request.
	success, result, err = c.Exec(params)
	if err != nil {
		fmt.Fprintf(os.Stderr, "c.Exec() error: %v\nsuccess: %v\nresult: %v\n", err, success, result)
		return
	}

	fmt.Fprintf(os.Stderr, "c.Exec() successfully\nsuccess: %v\nresult: %s\n", success, result)

}

func (*Client) IsValid

func (c *Client) IsValid() bool

IsValid checks if a client is valid.

func (*Client) MakeRequestBody

func (c *Client) MakeRequestBody(params map[string]string) (body io.Reader, err error)

MakeRequestBody makes the HTTP request body by given parameters for each REST API.

func (*Client) Post

func (c *Client) Post(params map[string]string) (resp *http.Response, err error)

Post does the HTTP post for each REST API.

Params:
  params: map that contains parameters of REST API. See official docs to fill the parameters.
Returns:
  resp: HTTP Response. Do not forget to call resp.Body.Close() after use.
Example

Example of Client.Post()

package main

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/northbright/alidayu"
)

func main() {
	// Create a new client(创建一个新的Client实例).
	c := &alidayu.Client{AppKey: "", AppSecret: "", UseHTTPS: false}

	// ---------------------------------------
	// Send Verification Code in SMS(发送短信验证码).
	// ---------------------------------------
	// Set parameters(设置API所需的所有参数,包括公共参数和方法相关参数).
	params := map[string]string{}

	// Common parameters will be filled with default values automatically.
	// You may also set them to override default ones.
	// 公共参数会自动使用默认值填充,不需要用户设置.
	// 当然,用户可以使用自己的设置来覆盖默认值.
	//   params["format"] = "json"      // "json" or "xml"
	//   params["v"] = "2.0"            // "2.0" by default
	//   params["sign_method"] = "md5"  // "md5" or "hmac"

	// No need to set "timestamp" and "sign", these parameters will be calculated and filled automatically.
	// 不需要用户设置"timestamp"和"sign",这些参数会被自动计算和设置.

	// Set method specified parameters(设置方法相关特定参数).
	params["method"] = "alibaba.aliqin.fc.sms.num.send"           // Set method to send SMS(API接口名称).
	params["sms_type"] = "normal"                                 // Set SMS type(短信类型).
	params["sms_free_sign_name"] = ""                             // Set SMS signature(短信签名).
	params["sms_param"] = `{"code":"123456", "product":"My App"}` // Set variable for SMS template(短信模板变量).
	params["sms_template_code"] = ""                              // Set SMS template code(短信模板ID).
	params["rec_num"] = ""                                        // Set phone number to send SMS(短信接收号码).

	// Call Post() to post the request.
	resp, err := c.Post(params)
	if err != nil {
		fmt.Fprintf(os.Stderr, "c.Post() error: %v\n", err)
		return
	}

	// Read HTTP Response.
	defer resp.Body.Close()
	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ioutil.ReadAll() error:%v\n", err)
		return
	}

	fmt.Fprintf(os.Stderr, "c.Post() successfully\n%v\n", string(data))

	// ------------------------------------------
	// Send Verification Code in Single Call(发送文本转语音通知验证码).
	// ------------------------------------------
	// Set Parameters.
	params = map[string]string{}
	params["method"] = "alibaba.aliqin.fc.tts.num.singlecall"     // Set method to make single call(API接口名称).
	params["tts_param"] = `{"code":"123456", "product":"My App"}` // Set variable for TTS template(文本转语音(TTS)模板变量).
	params["called_num"] = ""                                     // Set phone number to make single call(被叫号码).
	params["called_show_num"] = ""                                // Set show number(被叫号显).
	params["tts_code"] = ""                                       // Set TTS code(TTS模板ID).

	// Call Post() to post the request.
	resp2, err := c.Post(params)
	if err != nil {
		fmt.Fprintf(os.Stderr, "c.Post() error: %v\n", err)
		return
	}

	// Read HTTP Response.
	defer resp2.Body.Close()
	data2, err := ioutil.ReadAll(resp2.Body)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ioutil.ReadAll() error:%v\n", err)
		return
	}

	fmt.Fprintf(os.Stderr, "c.Post() successfully\n%v\n", string(data2))

}

func (*Client) SignHMAC

func (c *Client) SignHMAC(params map[string]string) (signature string)

SignHMAC gets the sorted query string and sign it using HMAC_MD5.

func (*Client) SignMD5

func (c *Client) SignMD5(params map[string]string) (signature string)

SignMD5 gets the sorted query string and sign it using MD5.

func (*Client) UpdateCommonParams

func (c *Client) UpdateCommonParams(params map[string]string)

UpdateCommonParams updates the given parameters by merge default common parameters.

Jump to

Keyboard shortcuts

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