2025-04-06 04:55:01 +00:00
|
|
|
/*
|
2025-04-07 02:59:20 +00:00
|
|
|
Copyright © 2025 filifa
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
2025-04-06 23:39:04 +00:00
|
|
|
Stats API Documentation
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
Official API for Major League Baseball.
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
API version: 2.0.0
|
2025-04-06 04:55:01 +00:00
|
|
|
*/
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
|
|
|
|
|
2025-04-06 04:55:01 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2025-04-06 23:39:04 +00:00
|
|
|
"log"
|
2025-04-06 04:55:01 +00:00
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2025-04-06 23:39:04 +00:00
|
|
|
"net/http/httputil"
|
2025-04-06 04:55:01 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2025-04-06 23:39:04 +00:00
|
|
|
JsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`)
|
|
|
|
XmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`)
|
|
|
|
queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`)
|
2025-04-07 02:14:30 +00:00
|
|
|
queryDescape = strings.NewReplacer("%5B", "[", "%5D", "]")
|
2025-04-06 04:55:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// APIClient manages communication with the Stats API Documentation API v2.0.0
|
|
|
|
// In most cases there should be only one, shared, APIClient.
|
|
|
|
type APIClient struct {
|
|
|
|
cfg *Configuration
|
|
|
|
common service // Reuse a single struct instead of allocating one for each service on the heap.
|
|
|
|
|
|
|
|
// API Services
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
AnalyticsAPI *AnalyticsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
AttendanceAPI *AttendanceAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
AwardsAPI *AwardsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
BatTrackingAPI *BatTrackingAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
BiomechanicsAPI *BiomechanicsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
BroadcastAPI *BroadcastAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
ConferenceAPI *ConferenceAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
DivisionAPI *DivisionAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
DraftAPI *DraftAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
GameAPI *GameAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
GamePaceAPI *GamePaceAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
HighLowAPI *HighLowAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
HomerunDerbyAPI *HomerunDerbyAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
JobAPI *JobAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
LeagueAPI *LeagueAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
MilestonesAPI *MilestonesAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
MiscAPI *MiscAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
PersonAPI *PersonAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
PredictionsAPI *PredictionsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
ReviewsAPI *ReviewsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
ScheduleAPI *ScheduleAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
SeasonAPI *SeasonAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
SkeletalAPI *SkeletalAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
SportsAPI *SportsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
StandingsAPI *StandingsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
StatsAPI *StatsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
StreaksAPI *StreaksAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
TeamsAPI *TeamsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
TransactionsAPI *TransactionsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
UniformsAPI *UniformsAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
VenuesAPI *VenuesAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
WeatherAPI *WeatherAPIService
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
client *APIClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIClient creates a new API client. Requires a userAgent string describing your application.
|
|
|
|
// optionally a custom http.Client to allow for advanced features such as caching.
|
|
|
|
func NewAPIClient(cfg *Configuration) *APIClient {
|
|
|
|
if cfg.HTTPClient == nil {
|
|
|
|
cfg.HTTPClient = http.DefaultClient
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &APIClient{}
|
|
|
|
c.cfg = cfg
|
|
|
|
c.common.client = c
|
|
|
|
|
|
|
|
// API Services
|
2025-04-06 23:39:04 +00:00
|
|
|
c.AnalyticsAPI = (*AnalyticsAPIService)(&c.common)
|
|
|
|
c.AttendanceAPI = (*AttendanceAPIService)(&c.common)
|
|
|
|
c.AwardsAPI = (*AwardsAPIService)(&c.common)
|
|
|
|
c.BatTrackingAPI = (*BatTrackingAPIService)(&c.common)
|
|
|
|
c.BiomechanicsAPI = (*BiomechanicsAPIService)(&c.common)
|
|
|
|
c.BroadcastAPI = (*BroadcastAPIService)(&c.common)
|
|
|
|
c.ConferenceAPI = (*ConferenceAPIService)(&c.common)
|
|
|
|
c.DivisionAPI = (*DivisionAPIService)(&c.common)
|
|
|
|
c.DraftAPI = (*DraftAPIService)(&c.common)
|
|
|
|
c.GameAPI = (*GameAPIService)(&c.common)
|
|
|
|
c.GamePaceAPI = (*GamePaceAPIService)(&c.common)
|
|
|
|
c.HighLowAPI = (*HighLowAPIService)(&c.common)
|
|
|
|
c.HomerunDerbyAPI = (*HomerunDerbyAPIService)(&c.common)
|
|
|
|
c.JobAPI = (*JobAPIService)(&c.common)
|
|
|
|
c.LeagueAPI = (*LeagueAPIService)(&c.common)
|
|
|
|
c.MilestonesAPI = (*MilestonesAPIService)(&c.common)
|
|
|
|
c.MiscAPI = (*MiscAPIService)(&c.common)
|
|
|
|
c.PersonAPI = (*PersonAPIService)(&c.common)
|
|
|
|
c.PredictionsAPI = (*PredictionsAPIService)(&c.common)
|
|
|
|
c.ReviewsAPI = (*ReviewsAPIService)(&c.common)
|
|
|
|
c.ScheduleAPI = (*ScheduleAPIService)(&c.common)
|
|
|
|
c.SeasonAPI = (*SeasonAPIService)(&c.common)
|
|
|
|
c.SkeletalAPI = (*SkeletalAPIService)(&c.common)
|
|
|
|
c.SportsAPI = (*SportsAPIService)(&c.common)
|
|
|
|
c.StandingsAPI = (*StandingsAPIService)(&c.common)
|
|
|
|
c.StatsAPI = (*StatsAPIService)(&c.common)
|
|
|
|
c.StreaksAPI = (*StreaksAPIService)(&c.common)
|
|
|
|
c.TeamsAPI = (*TeamsAPIService)(&c.common)
|
|
|
|
c.TransactionsAPI = (*TransactionsAPIService)(&c.common)
|
|
|
|
c.UniformsAPI = (*UniformsAPIService)(&c.common)
|
|
|
|
c.VenuesAPI = (*VenuesAPIService)(&c.common)
|
|
|
|
c.WeatherAPI = (*WeatherAPIService)(&c.common)
|
2025-04-06 04:55:01 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func atoi(in string) (int, error) {
|
|
|
|
return strconv.Atoi(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectHeaderContentType select a content type from the available list.
|
|
|
|
func selectHeaderContentType(contentTypes []string) string {
|
|
|
|
if len(contentTypes) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if contains(contentTypes, "application/json") {
|
|
|
|
return "application/json"
|
|
|
|
}
|
|
|
|
return contentTypes[0] // use the first content type specified in 'consumes'
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectHeaderAccept join all accept types and return
|
|
|
|
func selectHeaderAccept(accepts []string) string {
|
|
|
|
if len(accepts) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if contains(accepts, "application/json") {
|
|
|
|
return "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(accepts, ",")
|
|
|
|
}
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
// contains is a case insensitive match, finding needle in a haystack
|
2025-04-06 04:55:01 +00:00
|
|
|
func contains(haystack []string, needle string) bool {
|
|
|
|
for _, a := range haystack {
|
2025-04-06 23:39:04 +00:00
|
|
|
if strings.EqualFold(a, needle) {
|
2025-04-06 04:55:01 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify optional parameters are of the correct type.
|
|
|
|
func typeCheckParameter(obj interface{}, expected string, name string) error {
|
|
|
|
// Make sure there is an object.
|
|
|
|
if obj == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the type is as expected.
|
|
|
|
if reflect.TypeOf(obj).String() != expected {
|
2025-04-06 23:39:04 +00:00
|
|
|
return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-07 02:14:30 +00:00
|
|
|
func parameterValueToString(obj interface{}, key string) string {
|
2025-04-06 23:39:04 +00:00
|
|
|
if reflect.TypeOf(obj).Kind() != reflect.Ptr {
|
|
|
|
if actualObj, ok := obj.(interface{ GetActualInstanceValue() interface{} }); ok {
|
|
|
|
return fmt.Sprintf("%v", actualObj.GetActualInstanceValue())
|
|
|
|
}
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
return fmt.Sprintf("%v", obj)
|
|
|
|
}
|
2025-04-07 02:14:30 +00:00
|
|
|
var param, ok = obj.(MappedNullable)
|
2025-04-06 23:39:04 +00:00
|
|
|
if !ok {
|
|
|
|
return ""
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
2025-04-07 02:14:30 +00:00
|
|
|
dataMap, err := param.ToMap()
|
2025-04-06 23:39:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v", dataMap[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
// parameterAddToHeaderOrQuery adds the provided object to the request header or url query
|
|
|
|
// supporting deep object syntax
|
|
|
|
func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix string, obj interface{}, style string, collectionType string) {
|
|
|
|
var v = reflect.ValueOf(obj)
|
|
|
|
var value = ""
|
|
|
|
if v == reflect.ValueOf(nil) {
|
|
|
|
value = "null"
|
|
|
|
} else {
|
|
|
|
switch v.Kind() {
|
2025-04-07 02:14:30 +00:00
|
|
|
case reflect.Invalid:
|
|
|
|
value = "invalid"
|
2025-04-06 04:55:01 +00:00
|
|
|
|
2025-04-07 02:14:30 +00:00
|
|
|
case reflect.Struct:
|
|
|
|
if t, ok := obj.(MappedNullable); ok {
|
|
|
|
dataMap, err := t.ToMap()
|
|
|
|
if err != nil {
|
2025-04-06 23:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
2025-04-07 02:14:30 +00:00
|
|
|
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, dataMap, style, collectionType)
|
2025-04-06 23:39:04 +00:00
|
|
|
return
|
2025-04-07 02:14:30 +00:00
|
|
|
}
|
|
|
|
if t, ok := obj.(time.Time); ok {
|
|
|
|
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339Nano), style, collectionType)
|
2025-04-06 23:39:04 +00:00
|
|
|
return
|
2025-04-07 02:14:30 +00:00
|
|
|
}
|
|
|
|
value = v.Type().String() + " value"
|
|
|
|
case reflect.Slice:
|
|
|
|
var indValue = reflect.ValueOf(obj)
|
|
|
|
if indValue == reflect.ValueOf(nil) {
|
2025-04-06 23:39:04 +00:00
|
|
|
return
|
2025-04-07 02:14:30 +00:00
|
|
|
}
|
|
|
|
var lenIndValue = indValue.Len()
|
|
|
|
for i := 0; i < lenIndValue; i++ {
|
|
|
|
var arrayValue = indValue.Index(i)
|
|
|
|
var keyPrefixForCollectionType = keyPrefix
|
|
|
|
if style == "deepObject" {
|
|
|
|
keyPrefixForCollectionType = keyPrefix + "[" + strconv.Itoa(i) + "]"
|
|
|
|
}
|
|
|
|
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefixForCollectionType, arrayValue.Interface(), style, collectionType)
|
|
|
|
}
|
|
|
|
return
|
2025-04-06 23:39:04 +00:00
|
|
|
|
2025-04-07 02:14:30 +00:00
|
|
|
case reflect.Map:
|
|
|
|
var indValue = reflect.ValueOf(obj)
|
|
|
|
if indValue == reflect.ValueOf(nil) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
iter := indValue.MapRange()
|
|
|
|
for iter.Next() {
|
|
|
|
k, v := iter.Key(), iter.Value()
|
|
|
|
parameterAddToHeaderOrQuery(headerOrQueryParams, fmt.Sprintf("%s[%s]", keyPrefix, k.String()), v.Interface(), style, collectionType)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
case reflect.Interface:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Ptr:
|
|
|
|
parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, v.Elem().Interface(), style, collectionType)
|
|
|
|
return
|
|
|
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16,
|
|
|
|
reflect.Int32, reflect.Int64:
|
|
|
|
value = strconv.FormatInt(v.Int(), 10)
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16,
|
|
|
|
reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
value = strconv.FormatUint(v.Uint(), 10)
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
value = strconv.FormatFloat(v.Float(), 'g', -1, 32)
|
|
|
|
case reflect.Bool:
|
|
|
|
value = strconv.FormatBool(v.Bool())
|
|
|
|
case reflect.String:
|
|
|
|
value = v.String()
|
|
|
|
default:
|
|
|
|
value = v.Type().String() + " value"
|
2025-04-06 23:39:04 +00:00
|
|
|
}
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
switch valuesMap := headerOrQueryParams.(type) {
|
2025-04-07 02:14:30 +00:00
|
|
|
case url.Values:
|
|
|
|
if collectionType == "csv" && valuesMap.Get(keyPrefix) != "" {
|
|
|
|
valuesMap.Set(keyPrefix, valuesMap.Get(keyPrefix)+","+value)
|
|
|
|
} else {
|
|
|
|
valuesMap.Add(keyPrefix, value)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case map[string]string:
|
|
|
|
valuesMap[keyPrefix] = value
|
|
|
|
break
|
2025-04-06 23:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper for converting interface{} parameters to json strings
|
|
|
|
func parameterToJson(obj interface{}) (string, error) {
|
|
|
|
jsonBuf, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(jsonBuf), err
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// callAPI do the request.
|
|
|
|
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
|
2025-04-06 23:39:04 +00:00
|
|
|
if c.cfg.Debug {
|
|
|
|
dump, err := httputil.DumpRequestOut(request, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Printf("\n%s\n", string(dump))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.cfg.HTTPClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.cfg.Debug {
|
|
|
|
dump, err := httputil.DumpResponse(resp, true)
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
log.Printf("\n%s\n", string(dump))
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow modification of underlying config for alternate implementations and testing
|
|
|
|
// Caution: modifying the configuration while live can cause data races and potentially unwanted behavior
|
|
|
|
func (c *APIClient) GetConfig() *Configuration {
|
|
|
|
return c.cfg
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
type formFile struct {
|
2025-04-07 02:14:30 +00:00
|
|
|
fileBytes []byte
|
|
|
|
fileName string
|
|
|
|
formFileName string
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepareRequest build the request
|
|
|
|
func (c *APIClient) prepareRequest(
|
|
|
|
ctx context.Context,
|
|
|
|
path string, method string,
|
|
|
|
postBody interface{},
|
|
|
|
headerParams map[string]string,
|
|
|
|
queryParams url.Values,
|
|
|
|
formParams url.Values,
|
2025-04-06 23:39:04 +00:00
|
|
|
formFiles []formFile) (localVarRequest *http.Request, err error) {
|
2025-04-06 04:55:01 +00:00
|
|
|
|
|
|
|
var body *bytes.Buffer
|
|
|
|
|
|
|
|
// Detect postBody type and post.
|
|
|
|
if postBody != nil {
|
|
|
|
contentType := headerParams["Content-Type"]
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = detectContentType(postBody)
|
|
|
|
headerParams["Content-Type"] = contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err = setBody(postBody, contentType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add form parameters and file if available.
|
2025-04-06 23:39:04 +00:00
|
|
|
if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(formFiles) > 0) {
|
2025-04-06 04:55:01 +00:00
|
|
|
if body != nil {
|
|
|
|
return nil, errors.New("Cannot specify postBody and multipart form at the same time.")
|
|
|
|
}
|
|
|
|
body = &bytes.Buffer{}
|
|
|
|
w := multipart.NewWriter(body)
|
|
|
|
|
|
|
|
for k, v := range formParams {
|
|
|
|
for _, iv := range v {
|
|
|
|
if strings.HasPrefix(k, "@") { // file
|
|
|
|
err = addFile(w, k[1:], iv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else { // form value
|
|
|
|
w.WriteField(k, iv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-04-06 23:39:04 +00:00
|
|
|
for _, formFile := range formFiles {
|
|
|
|
if len(formFile.fileBytes) > 0 && formFile.fileName != "" {
|
|
|
|
w.Boundary()
|
|
|
|
part, err := w.CreateFormFile(formFile.formFileName, filepath.Base(formFile.fileName))
|
|
|
|
if err != nil {
|
2025-04-07 02:14:30 +00:00
|
|
|
return nil, err
|
2025-04-06 23:39:04 +00:00
|
|
|
}
|
|
|
|
_, err = part.Write(formFile.fileBytes)
|
|
|
|
if err != nil {
|
2025-04-07 02:14:30 +00:00
|
|
|
return nil, err
|
2025-04-06 23:39:04 +00:00
|
|
|
}
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
// Set the Boundary in the Content-Type
|
|
|
|
headerParams["Content-Type"] = w.FormDataContentType()
|
|
|
|
|
2025-04-06 04:55:01 +00:00
|
|
|
// Set Content-Length
|
|
|
|
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
|
|
|
|
if body != nil {
|
|
|
|
return nil, errors.New("Cannot specify postBody and x-www-form-urlencoded form at the same time.")
|
|
|
|
}
|
|
|
|
body = &bytes.Buffer{}
|
|
|
|
body.WriteString(formParams.Encode())
|
|
|
|
// Set Content-Length
|
|
|
|
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup path and query parameters
|
|
|
|
url, err := url.Parse(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
// Override request host, if applicable
|
|
|
|
if c.cfg.Host != "" {
|
|
|
|
url.Host = c.cfg.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override request scheme, if applicable
|
|
|
|
if c.cfg.Scheme != "" {
|
|
|
|
url.Scheme = c.cfg.Scheme
|
|
|
|
}
|
|
|
|
|
2025-04-06 04:55:01 +00:00
|
|
|
// Adding Query Param
|
|
|
|
query := url.Query()
|
|
|
|
for k, v := range queryParams {
|
|
|
|
for _, iv := range v {
|
|
|
|
query.Add(k, iv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the parameters.
|
2025-04-06 23:39:04 +00:00
|
|
|
url.RawQuery = queryParamSplit.ReplaceAllStringFunc(query.Encode(), func(s string) string {
|
|
|
|
pieces := strings.Split(s, "=")
|
|
|
|
pieces[0] = queryDescape.Replace(pieces[0])
|
|
|
|
return strings.Join(pieces, "=")
|
|
|
|
})
|
2025-04-06 04:55:01 +00:00
|
|
|
|
|
|
|
// Generate a new request
|
|
|
|
if body != nil {
|
|
|
|
localVarRequest, err = http.NewRequest(method, url.String(), body)
|
|
|
|
} else {
|
|
|
|
localVarRequest, err = http.NewRequest(method, url.String(), nil)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add header parameters, if any
|
|
|
|
if len(headerParams) > 0 {
|
|
|
|
headers := http.Header{}
|
|
|
|
for h, v := range headerParams {
|
2025-04-06 23:39:04 +00:00
|
|
|
headers[h] = []string{v}
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
localVarRequest.Header = headers
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the user agent to the request.
|
|
|
|
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
|
|
|
|
|
|
|
|
if ctx != nil {
|
|
|
|
// add context to the request
|
|
|
|
localVarRequest = localVarRequest.WithContext(ctx)
|
|
|
|
|
|
|
|
// Walk through any authentication.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for header, value := range c.cfg.DefaultHeader {
|
|
|
|
localVarRequest.Header.Add(header, value)
|
|
|
|
}
|
|
|
|
return localVarRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
|
2025-04-06 23:39:04 +00:00
|
|
|
if len(b) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if s, ok := v.(*string); ok {
|
|
|
|
*s = string(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if f, ok := v.(*os.File); ok {
|
|
|
|
f, err = os.CreateTemp("", "HttpClientFile")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = f.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = f.Seek(0, io.SeekStart)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if f, ok := v.(**os.File); ok {
|
|
|
|
*f, err = os.CreateTemp("", "HttpClientFile")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = (*f).Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = (*f).Seek(0, io.SeekStart)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if XmlCheck.MatchString(contentType) {
|
2025-04-06 04:55:01 +00:00
|
|
|
if err = xml.Unmarshal(b, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2025-04-06 23:39:04 +00:00
|
|
|
}
|
|
|
|
if JsonCheck.MatchString(contentType) {
|
|
|
|
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
|
|
|
|
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
|
|
|
|
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("Unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
|
|
|
|
}
|
|
|
|
} else if err = json.Unmarshal(b, v); err != nil { // simple model
|
2025-04-06 04:55:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("undefined response type")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a file to the multipart request
|
|
|
|
func addFile(w *multipart.Writer, fieldName, path string) error {
|
2025-04-06 23:39:04 +00:00
|
|
|
file, err := os.Open(filepath.Clean(path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = file.Close()
|
2025-04-06 04:55:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
part, err := w.CreateFormFile(fieldName, filepath.Base(path))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set request body from an interface{}
|
|
|
|
func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) {
|
|
|
|
if bodyBuf == nil {
|
|
|
|
bodyBuf = &bytes.Buffer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if reader, ok := body.(io.Reader); ok {
|
|
|
|
_, err = bodyBuf.ReadFrom(reader)
|
2025-04-06 23:39:04 +00:00
|
|
|
} else if fp, ok := body.(*os.File); ok {
|
|
|
|
_, err = bodyBuf.ReadFrom(fp)
|
2025-04-06 04:55:01 +00:00
|
|
|
} else if b, ok := body.([]byte); ok {
|
|
|
|
_, err = bodyBuf.Write(b)
|
|
|
|
} else if s, ok := body.(string); ok {
|
|
|
|
_, err = bodyBuf.WriteString(s)
|
|
|
|
} else if s, ok := body.(*string); ok {
|
|
|
|
_, err = bodyBuf.WriteString(*s)
|
2025-04-06 23:39:04 +00:00
|
|
|
} else if JsonCheck.MatchString(contentType) {
|
2025-04-06 04:55:01 +00:00
|
|
|
err = json.NewEncoder(bodyBuf).Encode(body)
|
2025-04-06 23:39:04 +00:00
|
|
|
} else if XmlCheck.MatchString(contentType) {
|
|
|
|
var bs []byte
|
|
|
|
bs, err = xml.Marshal(body)
|
|
|
|
if err == nil {
|
|
|
|
bodyBuf.Write(bs)
|
|
|
|
}
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bodyBuf.Len() == 0 {
|
2025-04-06 23:39:04 +00:00
|
|
|
err = fmt.Errorf("invalid body type %s\n", contentType)
|
2025-04-06 04:55:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bodyBuf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// detectContentType method is used to figure out `Request.Body` content type for request header
|
|
|
|
func detectContentType(body interface{}) string {
|
|
|
|
contentType := "text/plain; charset=utf-8"
|
|
|
|
kind := reflect.TypeOf(body).Kind()
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case reflect.Struct, reflect.Map, reflect.Ptr:
|
|
|
|
contentType = "application/json; charset=utf-8"
|
|
|
|
case reflect.String:
|
|
|
|
contentType = "text/plain; charset=utf-8"
|
|
|
|
default:
|
|
|
|
if b, ok := body.([]byte); ok {
|
|
|
|
contentType = http.DetectContentType(b)
|
|
|
|
} else if kind == reflect.Slice {
|
|
|
|
contentType = "application/json; charset=utf-8"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ripped from https://github.com/gregjones/httpcache/blob/master/httpcache.go
|
|
|
|
type cacheControl map[string]string
|
|
|
|
|
|
|
|
func parseCacheControl(headers http.Header) cacheControl {
|
|
|
|
cc := cacheControl{}
|
|
|
|
ccHeader := headers.Get("Cache-Control")
|
|
|
|
for _, part := range strings.Split(ccHeader, ",") {
|
|
|
|
part = strings.Trim(part, " ")
|
|
|
|
if part == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.ContainsRune(part, '=') {
|
|
|
|
keyval := strings.Split(part, "=")
|
|
|
|
cc[strings.Trim(keyval[0], " ")] = strings.Trim(keyval[1], ",")
|
|
|
|
} else {
|
|
|
|
cc[part] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheExpires helper function to determine remaining time before repeating a request.
|
|
|
|
func CacheExpires(r *http.Response) time.Time {
|
|
|
|
// Figure out when the cache expires.
|
|
|
|
var expires time.Time
|
|
|
|
now, err := time.Parse(time.RFC1123, r.Header.Get("date"))
|
|
|
|
if err != nil {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
respCacheControl := parseCacheControl(r.Header)
|
|
|
|
|
|
|
|
if maxAge, ok := respCacheControl["max-age"]; ok {
|
|
|
|
lifetime, err := time.ParseDuration(maxAge + "s")
|
|
|
|
if err != nil {
|
|
|
|
expires = now
|
2025-04-06 23:39:04 +00:00
|
|
|
} else {
|
|
|
|
expires = now.Add(lifetime)
|
2025-04-06 04:55:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expiresHeader := r.Header.Get("Expires")
|
|
|
|
if expiresHeader != "" {
|
|
|
|
expires, err = time.Parse(time.RFC1123, expiresHeader)
|
|
|
|
if err != nil {
|
|
|
|
expires = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return expires
|
|
|
|
}
|
|
|
|
|
|
|
|
func strlen(s string) int {
|
|
|
|
return utf8.RuneCountInString(s)
|
|
|
|
}
|
|
|
|
|
2025-04-06 23:39:04 +00:00
|
|
|
// GenericOpenAPIError Provides access to the body, error and model on returned errors.
|
|
|
|
type GenericOpenAPIError struct {
|
2025-04-06 04:55:01 +00:00
|
|
|
body []byte
|
|
|
|
error string
|
|
|
|
model interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns non-empty string if there was an error.
|
2025-04-06 23:39:04 +00:00
|
|
|
func (e GenericOpenAPIError) Error() string {
|
2025-04-06 04:55:01 +00:00
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Body returns the raw bytes of the response
|
2025-04-06 23:39:04 +00:00
|
|
|
func (e GenericOpenAPIError) Body() []byte {
|
2025-04-06 04:55:01 +00:00
|
|
|
return e.body
|
|
|
|
}
|
|
|
|
|
|
|
|
// Model returns the unpacked model of the error
|
2025-04-06 23:39:04 +00:00
|
|
|
func (e GenericOpenAPIError) Model() interface{} {
|
2025-04-06 04:55:01 +00:00
|
|
|
return e.model
|
|
|
|
}
|
2025-04-06 23:39:04 +00:00
|
|
|
|
|
|
|
// format error message using title and detail when model implements rfc7807
|
|
|
|
func formatErrorMessage(status string, v interface{}) string {
|
|
|
|
str := ""
|
|
|
|
metaValue := reflect.ValueOf(v).Elem()
|
|
|
|
|
|
|
|
if metaValue.Kind() == reflect.Struct {
|
|
|
|
field := metaValue.FieldByName("Title")
|
|
|
|
if field != (reflect.Value{}) {
|
|
|
|
str = fmt.Sprintf("%s", field.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
field = metaValue.FieldByName("Detail")
|
|
|
|
if field != (reflect.Value{}) {
|
|
|
|
str = fmt.Sprintf("%s (%s)", str, field.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(fmt.Sprintf("%s %s", status, str))
|
|
|
|
}
|