mailcow-admin-aliases/internal/mailcow/api.go

31 lines
610 B
Go
Raw Permalink Normal View History

2024-07-26 10:57:24 +02:00
package mailcow
import (
"encoding/json"
"fmt"
"strings"
)
type MailcowApiResponse struct {
Type string `json:"type"`
Msg []string `json:"msg"`
}
// checkApiResponseOK checks if we got a positive response from the Mailcow API.
//
// Returns an error detailing why the request failed.
func checkApiResponseOK(body []byte) error {
var response []MailcowApiResponse
err := json.Unmarshal(body, &response)
if err != nil {
return fmt.Errorf("failed to unmarshal body: %w", err)
}
if response[0].Type != "success" {
return fmt.Errorf("%v", strings.Join(response[0].Msg, " "))
}
return nil
}