File: api.go

package info (click to toggle)
golang-github-akamai-akamaiopen-edgegrid-golang 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,408 kB
  • sloc: sh: 532; makefile: 3
file content (36 lines) | stat: -rw-r--r-- 1,043 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
package client

import (
	"encoding/json"
)

// Resource is the "base" type for all API resources
type Resource struct {
	Complete chan bool `json:"-"`
}

// Init initializes the Complete channel, if it is necessary
// need to create a resource specific Init(), make sure to
// initialize the channel.
func (resource *Resource) Init() {
	resource.Complete = make(chan bool, 1)
}

// PostUnmarshalJSON is a default implementation of the
// PostUnmarshalJSON hook that simply calls Init() and
// sends true to the Complete channel. This is overridden
// in many resources, in particular those that represent
// collections, and have to initialize sub-resources also.
func (resource *Resource) PostUnmarshalJSON() error {
	resource.Init()
	resource.Complete <- true
	return nil
}

// GetJSON returns the raw (indented) JSON (as []bytes)
func (resource *Resource) GetJSON() ([]byte, error) {
	return json.MarshalIndent(resource, "", "    ")
}

// JSONBody is a generic struct for temporary JSON unmarshalling.
type JSONBody map[string]interface{}