mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-06-02 11:30:35 +02:00
Simplify API endpoints; add endpoint tests
This commit is contained in:
parent
7ca9afad57
commit
367d024a2d
6 changed files with 113 additions and 75 deletions
util
28
util/util.go
28
util/util.go
|
@ -251,6 +251,11 @@ func BasicAuth(user, pass string) string {
|
|||
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, pass))))
|
||||
}
|
||||
|
||||
// BearerAuth encodes the Authorization header value for a bearer/token auth
|
||||
func BearerAuth(token string) string {
|
||||
return fmt.Sprintf("Bearer %s", token)
|
||||
}
|
||||
|
||||
// MaybeMarshalJSON returns a JSON string of the given object, or "<cannot serialize>" if serialization failed.
|
||||
// This is useful for logging purposes where a failure doesn't matter that much.
|
||||
func MaybeMarshalJSON(v any) string {
|
||||
|
@ -283,3 +288,26 @@ func QuoteCommand(command []string) string {
|
|||
}
|
||||
return strings.Join(quoted, " ")
|
||||
}
|
||||
|
||||
// ReadJSON reads the given io.ReadCloser into a struct
|
||||
func ReadJSON[T any](body io.ReadCloser) (*T, error) {
|
||||
var obj T
|
||||
if err := json.NewDecoder(body).Decode(&obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &obj, nil
|
||||
}
|
||||
|
||||
// ReadJSONWithLimit reads the given io.ReadCloser into a struct, but only until limit is reached
|
||||
func ReadJSONWithLimit[T any](r io.ReadCloser, limit int) (*T, error) {
|
||||
r, err := Peek(r, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer r.Close()
|
||||
var obj T
|
||||
if err := json.NewDecoder(r).Decode(&obj); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &obj, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue