mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Add new endpoints
This commit is contained in:
parent
382d19ee94
commit
08c46e50bb
5 changed files with 423 additions and 0 deletions
48
utils.go
48
utils.go
|
|
@ -2,6 +2,10 @@ package crunchyroll
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func decodeMapToStruct(m interface{}, s interface{}) error {
|
||||
|
|
@ -23,3 +27,47 @@ func regexGroups(parsed [][]string, subexpNames ...string) map[string]string {
|
|||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
func encodeStructToQueryValues(s interface{}) (string, error) {
|
||||
values := make(url.Values)
|
||||
v := reflect.ValueOf(s)
|
||||
|
||||
for i := 0; i < v.Type().NumField(); i++ {
|
||||
|
||||
// don't include parameters with default or without values in the query to avoid corruption of the API response.
|
||||
if isEmptyValue(v.Field(i)) {
|
||||
continue
|
||||
}
|
||||
|
||||
key := v.Type().Field(i).Tag.Get("param")
|
||||
var val string
|
||||
|
||||
if v.Field(i).Kind() == reflect.Slice {
|
||||
var items []string
|
||||
|
||||
for _, i := range v.Field(i).Interface().([]string) {
|
||||
items = append(items, i)
|
||||
}
|
||||
|
||||
val = strings.Join(items, ",")
|
||||
} else {
|
||||
val = fmt.Sprint(v.Field(i).Interface())
|
||||
}
|
||||
|
||||
values.Add(key, val)
|
||||
}
|
||||
|
||||
return values.Encode(), nil
|
||||
}
|
||||
|
||||
func isEmptyValue(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !v.Bool()
|
||||
case reflect.Uint:
|
||||
return v.Uint() == 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue