123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package invoke
- import (
- "git.sxidc.com/service-supports/dapr_api/utils"
- "github.com/go-resty/resty/v2"
- "net/url"
- "time"
- )
- type API struct {
- client *resty.Client
- baseUrl string
- }
- func NewAPI(baseUrl string, timeout time.Duration) *API {
- return &API{
- client: resty.New().SetTimeout(timeout),
- baseUrl: baseUrl,
- }
- }
- func DestroyAPI(api *API) {
- if api == nil {
- return
- }
- api.baseUrl = ""
- api.client = nil
- }
- func (api *API) PostJSON(methodName string, data []byte, headers map[string]string) ([]byte, error) {
- if headers == nil {
- headers = make(map[string]string)
- }
- headers["Content-Type"] = "application/json"
- return api.Post(methodName, headers, data)
- }
- func (api *API) PutJSON(methodName string, data []byte, headers map[string]string) ([]byte, error) {
- if headers == nil {
- headers = make(map[string]string)
- }
- headers["Content-Type"] = "application/json"
- return api.Put(methodName, headers, data)
- }
- func (api *API) PostWithoutHeaders(methodName string, data []byte) ([]byte, error) {
- return api.Post(methodName, nil, data)
- }
- func (api *API) PutWithoutHeaders(methodName string, data []byte) ([]byte, error) {
- return api.Put(methodName, nil, data)
- }
- func (api *API) DeleteWithoutHeaders(methodName string, queryParams map[string]string) ([]byte, error) {
- return api.Delete(methodName, queryParams, nil)
- }
- func (api *API) GetWithoutHeaders(methodName string, queryParams map[string]string) ([]byte, error) {
- return api.Get(methodName, queryParams, nil)
- }
- func (api *API) Post(methodName string, headers map[string]string, data []byte) ([]byte, error) {
- invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
- if err != nil {
- return nil, err
- }
- resp, err := api.client.R().
- SetHeaders(headers).
- SetBody(data).
- Post(invokeUrl)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, utils.ResponseStatusError(invokeUrl, resp)
- }
- return resp.Body(), nil
- }
- func (api *API) Delete(methodName string, queryParams map[string]string, headers map[string]string) ([]byte, error) {
- invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
- if err != nil {
- return nil, err
- }
- resp, err := api.client.R().
- SetQueryParams(queryParams).
- SetHeaders(headers).
- Delete(invokeUrl)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, utils.ResponseStatusError(invokeUrl, resp)
- }
- return resp.Body(), nil
- }
- func (api *API) Put(methodName string, headers map[string]string, data []byte) ([]byte, error) {
- invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
- if err != nil {
- return nil, err
- }
- resp, err := api.client.R().
- SetHeaders(headers).
- SetBody(data).
- Put(invokeUrl)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, utils.ResponseStatusError(invokeUrl, resp)
- }
- return resp.Body(), nil
- }
- func (api *API) Get(methodName string, queryParams map[string]string, headers map[string]string) ([]byte, error) {
- invokeUrl, err := url.JoinPath(api.baseUrl, methodName)
- if err != nil {
- return nil, err
- }
- resp, err := api.client.R().
- SetQueryParams(queryParams).
- SetHeaders(headers).
- Get(invokeUrl)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, utils.ResponseStatusError(invokeUrl, resp)
- }
- return resp.Body(), nil
- }
|