Skip to content

Commit

Permalink
Merge pull request #72 from akamai/AT-32
Browse files Browse the repository at this point in the history
[AT-32][Change] Add support for cache for contract groups cpcodes for papi
  • Loading branch information
martinstibbe authored Feb 29, 2020
2 parents ed24bf5 + a6309e4 commit 20c0b65
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 73 deletions.
23 changes: 23 additions & 0 deletions edgegrid/profilecache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edgegrid

import (
"time"

"github.com/patrickmn/go-cache"
gocache "github.com/patrickmn/go-cache"
)

// Config struct provides all the necessary fields to
// create authorization header, debug is optional
type (
profileCache gocache.Cache
)

// Init initializes cache
//

// See: InitCache()
func InitCache() (gocache.Cache, error) {
cache := cache.New(5*time.Minute, 10*time.Minute)
return *cache, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/google/go-querystring v1.0.0
github.com/google/uuid v1.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
Expand Down
57 changes: 34 additions & 23 deletions papi-v1/contracts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package papi

import (
"encoding/json"
"fmt"

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

// Contracts represents a collection of property manager contracts
Expand Down Expand Up @@ -45,34 +47,43 @@ func (contracts *Contracts) PostUnmarshalJSON() error {
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listcontracts
// Endpoint: GET /papi/v1/contracts
func (contracts *Contracts) GetContracts() error {
req, err := client.NewRequest(
Config,
"GET",
"/papi/v1/contracts",
nil,
)
if err != nil {
return err
}

res, err := client.Do(Config, req)
if err != nil {
return err
}
cachecontracts, found := Profilecache.Get("contracts")
if found {
json.Unmarshal(cachecontracts.([]byte), contracts)
return nil
} else {

req, err := client.NewRequest(
Config,
"GET",
"/papi/v1/contracts",
nil,
)
if err != nil {
return err
}

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

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

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

return nil
if err != nil {
return err
}
byt, _ := json.Marshal(contracts)
Profilecache.Set("contracts", byt, cache.DefaultExpiration)
return nil
}
}

// FindContract finds a specific contract by ID
Expand Down
67 changes: 38 additions & 29 deletions papi-v1/cpcodes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package papi

import (
"encoding/json"
"fmt"

"strconv"
"strings"
"time"

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

// CpCodes represents a collection of CP Codes
Expand Down Expand Up @@ -70,39 +73,46 @@ func (cpcodes *CpCodes) PostUnmarshalJSON() error {
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listcpcodes
// Endpoint: GET /papi/v1/cpcodes/{?contractId,groupId}
func (cpcodes *CpCodes) GetCpCodes() error {
if cpcodes.Contract == nil {
cpcodes.Contract = NewContract(NewContracts())
cpcodes.Contract.ContractID = cpcodes.Group.ContractIDs[0]
}
cachecpcodes, found := Profilecache.Get("cpcodes")
if found {
json.Unmarshal(cachecpcodes.([]byte), cpcodes)
return nil
} else {
if cpcodes.Contract == nil {
cpcodes.Contract = NewContract(NewContracts())
cpcodes.Contract.ContractID = cpcodes.Group.ContractIDs[0]
}

req, err := client.NewRequest(
Config,
"GET",
fmt.Sprintf(
"/papi/v1/cpcodes?groupId=%s&contractId=%s",
cpcodes.Group.GroupID,
cpcodes.Contract.ContractID,
),
nil,
)
if err != nil {
return err
}
req, err := client.NewRequest(
Config,
"GET",
fmt.Sprintf(
"/papi/v1/cpcodes?groupId=%s&contractId=%s",
cpcodes.Group.GroupID,
cpcodes.Contract.ContractID,
),
nil,
)
if err != nil {
return err
}

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

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

if err = client.BodyJSON(res, cpcodes); err != nil {
return err
if err = client.BodyJSON(res, cpcodes); err != nil {
return err
}
byt, _ := json.Marshal(cpcodes)
Profilecache.Set("cpcodes", byt, cache.DefaultExpiration)
return nil
}

return nil
}

func (cpcodes *CpCodes) FindCpCode(nameOrId string) (*CpCode, error) {
Expand All @@ -111,7 +121,6 @@ func (cpcodes *CpCodes) FindCpCode(nameOrId string) (*CpCode, error) {
if err != nil {
return nil, err
}

if len(cpcodes.CpCodes.Items) == 0 {
return nil, fmt.Errorf("unable to fetch CP codes for group/contract")
}
Expand Down
49 changes: 29 additions & 20 deletions papi-v1/groups.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package papi

import (
"encoding/json"
"fmt"

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

// Groups represents a collection of PAPI groups
Expand Down Expand Up @@ -44,30 +46,37 @@ func (groups *Groups) PostUnmarshalJSON() error {
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listgroups
// Endpoint: GET /papi/v1/groups/
func (groups *Groups) GetGroups() error {
req, err := client.NewRequest(
Config,
"GET",
"/papi/v1/groups",
nil,
)
if err != nil {
return err
}
cachegroups, found := Profilecache.Get("groups")
if found {
json.Unmarshal(cachegroups.([]byte), groups)
return nil
} else {
req, err := client.NewRequest(
Config,
"GET",
"/papi/v1/groups",
nil,
)
if err != nil {
return err
}

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

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

if err = client.BodyJSON(res, groups); err != nil {
return err
if err = client.BodyJSON(res, groups); err != nil {
return err
}
byt, _ := json.Marshal(groups)
Profilecache.Set("groups", byt, cache.DefaultExpiration)
return nil
}

return nil
}

// AddGroup adds a group to a Groups collection
Expand Down
6 changes: 5 additions & 1 deletion papi-v1/service.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package papi

import (
"time"

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

var (
Config edgegrid.Config
Config edgegrid.Config
Profilecache = cache.New(5*time.Minute, 10*time.Minute)
)

// GetGroups retrieves all groups
Expand Down

0 comments on commit 20c0b65

Please sign in to comment.