github.com/bububa/oceanengine/marketing-api@v0.0.0-20210315120513-0b953137f7a6/model/report/audience/request.go (about)

     1  package audience
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/bububa/oceanengine/marketing-api/enum"
    10  )
    11  
    12  type Request struct {
    13  	AdvertiserID uint64                  `json:"advertiser_id,omitempty"` // 广告主ID
    14  	StartDate    time.Time               `json:"start_date,omitempty"`    // 起始日期,从0时起,格式2020-08-15; 默认15天前,即不指定起止时间获取最近15天数据
    15  	EndDate      time.Time               `json:"end_date,omitempty"`      // 结束日期,至24时止,格式2020-08-29; 默认昨天,即不指定起止时间获取最近15天数据; 起始时间与结束时间之差小于15天,否则报错并提示"max time span is 15 days"
    16  	Page         int                     `json:"page,omitempty"`          // 页码;默认值: 1
    17  	PageSize     int                     `json:"page_size,omitempty"`     // 页面大小,即每页展示的数据量,限制为1-100; 默认值: 20
    18  	IDType       enum.AudienceStatIDType `json:"id_type,omitempty"`       // 查询ID类型
    19  	IDs          []uint64                `json:"ids,omitempty"`           // 查询ID列表,长度1-100; id_type为AUDIENCE_STAT_ID_TYPE_ADVERTISER时,选填;其他类型,必填
    20  	Metrics      []string                `json:"metrics,omitempty"`       // 查询指标列表
    21  }
    22  
    23  func (r Request) Encode() string {
    24  	values := &url.Values{}
    25  	values.Set("advertiser_id", strconv.FormatUint(r.AdvertiserID, 10))
    26  	values.Set("start_date", r.StartDate.Format("2006-01-02"))
    27  	values.Set("end_date", r.EndDate.Format("2006-01-02"))
    28  	values.Set("id_type", string(r.IDType))
    29  	if r.IDs != nil {
    30  		ids, _ := json.Marshal(r.IDs)
    31  		values.Set("ids", string(ids))
    32  	}
    33  	if r.Metrics != nil {
    34  		metrics, _ := json.Marshal(r.Metrics)
    35  		values.Set("metrics", string(metrics))
    36  	}
    37  	if r.Page > 0 {
    38  		values.Set("page", strconv.Itoa(r.Page))
    39  	}
    40  	if r.PageSize > 0 {
    41  		values.Set("page_size", strconv.Itoa(r.PageSize))
    42  	}
    43  	return values.Encode()
    44  }