- update ip

- update request
master v1.0.84
李光春 1 year ago
parent 895099869e
commit b67842f51b

@ -1,5 +1,5 @@
package go_library
func Version() string {
return "1.0.83"
return "1.0.84"
}

@ -0,0 +1,35 @@
package gddata
import (
"github.com/dtapps/go-library/utils/golog"
"github.com/dtapps/go-library/utils/gorequest"
)
// ClientConfig 实例配置
type ClientConfig struct {
Token string
}
// Client 实例
type Client struct {
requestClient *gorequest.App // 请求服务
config struct {
token string
}
log struct {
status bool // 状态
client *golog.ApiClient // 日志服务
}
}
// NewClient 创建实例化
func NewClient(config *ClientConfig) (*Client, error) {
c := &Client{}
c.config.token = config.Token
c.requestClient = gorequest.NewHttp()
return c, nil
}

@ -0,0 +1,17 @@
package gddata
import "github.com/dtapps/go-library/utils/golog"
func (c *Client) Config(token string) *Client {
c.config.token = token
return c
}
// ConfigApiClientFun 日志配置
func (c *Client) ConfigApiClientFun(apiClientFun golog.ApiClientFun) {
apiClient := apiClientFun()
if apiClient != nil {
c.log.client = apiClient
c.log.status = true
}
}

@ -0,0 +1,9 @@
package gddata
const (
apiUrl = "https://gddata.gd.gov.cn/api/"
)
const (
LogTable = "gddata"
)

@ -0,0 +1,42 @@
package gddata
import (
"context"
"encoding/json"
"fmt"
"github.com/baidubce/bce-sdk-go/http"
"github.com/dtapps/go-library/utils/gorequest"
)
type GdpIndexResponse struct {
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
Type string `json:"type"`
MediaId string `json:"media_id"`
CreatedAt string `json:"created_at"`
}
type GdpIndexResult struct {
Result GdpIndexResponse // 结果
Body []byte // 内容
Http gorequest.Response // 请求
Err error // 错误
}
func newGdpIndexResult(result GdpIndexResponse, body []byte, http gorequest.Response, err error) *GdpIndexResult {
return &GdpIndexResult{Result: result, Body: body, Http: http, Err: err}
}
// GdpIndex 地区生产总值指数1978100接口
// https://gddata.gd.gov.cn/opdata/index?chooseValue=apiForm&id=29000%2F03600017&sourceType
func (c *Client) GdpIndex(ctx context.Context, year string) *GdpIndexResult {
// 参数
params := gorequest.NewParams()
params.Set("year", year) // 统计年份
// 请求
request, err := c.request(ctx, apiUrl+fmt.Sprintf("MjkwMDBfMDM2MDAwMTc=?token=%s", c.GetToken()), params, http.GET)
// 定义
var response GdpIndexResponse
err = json.Unmarshal(request.ResponseBody, &response)
return newGdpIndexResult(response, request.ResponseBody, request, err)
}

@ -0,0 +1,5 @@
package gddata
func (c *Client) GetToken() string {
return c.config.token
}

@ -0,0 +1,46 @@
package gddata
import (
"context"
"github.com/dtapps/go-library"
"github.com/dtapps/go-library/utils/gorequest"
"log"
)
func (c *Client) request(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) {
log.Println(url)
log.Println(params)
log.Println(method)
// 创建请求
client := c.requestClient
// 设置请求地址
client.SetUri(url)
// 设置方式
client.SetMethod(method)
// 设置格式
client.SetContentTypeJson()
// 设置参数
client.SetParams(params)
// 传入SDk版本
client.AfferentSdkUserVersion(go_library.Version())
// 发起请求
request, err := client.Request(ctx)
if err != nil {
return gorequest.Response{}, err
}
// 记录日志
if c.log.status {
go c.log.client.Middleware(ctx, request, go_library.Version())
}
return request, err
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 MiB

After

Width:  |  Height:  |  Size: 68 MiB

Binary file not shown.

@ -1,7 +1,7 @@
package gorequest
const (
userAgentFormat = "%s/%s/%s/%s"
userAgentFormat = "%s/%s/%s"
)
// 定义请求类型

@ -210,9 +210,9 @@ func request(app *App, ctx context.Context) (httpResponse Response, err error) {
// SDK版本
if app.afferentSdkUserVersion == "" {
httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion, app.config.sdkVersion))
httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion))
} else {
httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion, app.config.sdkVersion)+"/"+app.afferentSdkUserVersion)
httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion)+"/"+app.afferentSdkUserVersion)
}
// 请求类型

Loading…
Cancel
Save