File: response.go

package info (click to toggle)
golang-github-kong-go-kong 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package kong

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

// Response is a Kong Admin API response. It wraps http.Response.
type Response struct {
	*http.Response
	//other Kong specific fields
}

func newResponse(res *http.Response) *Response {
	return &Response{Response: res}
}

func messageFromBody(b []byte) string {
	s := struct {
		Message string
	}{}

	if err := json.Unmarshal(b, &s); err != nil {
		return fmt.Sprintf("<failed to parse response body: %v>", err)
	}

	return s.Message
}

func hasError(res *http.Response) error {
	if res.StatusCode >= 200 && res.StatusCode <= 399 {
		return nil
	}

	body, _ := ioutil.ReadAll(res.Body) // TODO error in error?
	return &APIError{
		httpCode: res.StatusCode,
		message:  messageFromBody(body),
	}
}