File: products.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 (125 lines) | stat: -rw-r--r-- 2,757 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package papi

import (
	"encoding/json"
	"fmt"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
	edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
	"github.com/patrickmn/go-cache"
)

// Products represents a collection of products
type Products struct {
	client.Resource
	AccountID  string `json:"accountId"`
	ContractID string `json:"contractId"`
	Products   struct {
		Items []*Product `json:"items"`
	} `json:"products"`
}

// NewProducts creates a new Products
func NewProducts() *Products {
	products := &Products{}
	products.Init()

	return products
}

// PostUnmarshalJSON is called after JSON unmarshaling into EdgeHostnames
//
// See: jsonhooks-v1/jsonhooks.Unmarshal()
func (products *Products) PostUnmarshalJSON() error {
	products.Init()

	for key, product := range products.Products.Items {
		products.Products.Items[key].parent = products
		if err := product.PostUnmarshalJSON(); err != nil {
			return err
		}
	}

	return nil
}

// GetProducts populates Products with product data
//
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listproducts
// Endpoint: GET /papi/v1/products/{?contractId}
func (products *Products) GetProducts(contract *Contract, correlationid string) error {
	cacheproducts, found := Profilecache.Get("products")
	if found {
		json.Unmarshal(cacheproducts.([]byte), products)
		return nil
	} else {
		req, err := client.NewRequest(
			Config,
			"GET",
			fmt.Sprintf(
				"/papi/v1/products?contractId=%s",
				contract.ContractID,
			),
			nil,
		)
		if err != nil {
			return err
		}

		edge.PrintHttpRequestCorrelation(req, true, correlationid)

		res, err := client.Do(Config, req)
		if err != nil {
			return err
		}

		edge.PrintHttpResponseCorrelation(res, true, correlationid)

		if client.IsError(res) {
			return client.NewAPIError(res)
		}

		if err = client.BodyJSON(res, products); err != nil {
			return err
		}

		byt, _ := json.Marshal(products)
		Profilecache.Set("products", byt, cache.DefaultExpiration)
		return nil
	}

}

// FindProduct finds a specific product by ID
func (products *Products) FindProduct(id string) (*Product, error) {
	var product *Product
	var productFound bool
	for _, product = range products.Products.Items {
		if product.ProductID == id {
			productFound = true
			break
		}
	}

	if !productFound {
		return nil, fmt.Errorf("Unable to find product: \"%s\"", id)
	}

	return product, nil
}

// Product represents a product resource
type Product struct {
	client.Resource
	parent      *Products
	ProductName string `json:"productName"`
	ProductID   string `json:"productId"`
}

// NewProduct creates a new Product
func NewProduct(parent *Products) *Product {
	product := &Product{parent: parent}
	product.Init()

	return product
}