Skip to content

Commit

Permalink
Merge pull request #91 from akamai/dns_maint
Browse files Browse the repository at this point in the history
place lock around http request creation. add addtl http logging
  • Loading branch information
edglynes committed May 27, 2020
2 parents 708e81b + 9ff8a43 commit 301cd2a
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 1 deletion.
7 changes: 7 additions & 0 deletions client-v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
)

var (
Expand All @@ -23,6 +24,8 @@ var (
UserAgent = "Akamai-Open-Edgegrid-golang/" + libraryVersion + " golang/" + strings.TrimPrefix(runtime.Version(), "go")
// Client is the *http.Client to use
Client = http.DefaultClient

reqLock sync.Mutex
)

// NewRequest creates an HTTP request that can be sent to Akamai APIs. A relative URL can be provided in path, which will be resolved to the
Expand All @@ -33,6 +36,9 @@ func NewRequest(config edgegrid.Config, method, path string, body io.Reader) (*h
err error
)

reqLock.Lock()
defer reqLock.Unlock()

if strings.HasPrefix(config.Host, "https://") {
baseURL, err = url.Parse(config.Host)
} else {
Expand Down Expand Up @@ -70,6 +76,7 @@ func NewRequest(config edgegrid.Config, method, path string, body io.Reader) (*h
func NewJSONRequest(config edgegrid.Config, method, path string, body interface{}) (*http.Request, error) {
var req *http.Request
var err error

if body != nil {
jsonBody, err := jsonhooks.Marshal(body)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions configdns-v2/authorities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dnsv2

import (
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
)

type AuthorityResponse struct {
Expand Down Expand Up @@ -29,11 +30,15 @@ func GetAuthorities(contractId string) (*AuthorityResponse, error) {
return nil, err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) && res.StatusCode != 404 {
return nil, client.NewAPIError(res)
} else if res.StatusCode == 404 {
Expand Down
35 changes: 35 additions & 0 deletions papi-v1/activations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"

)

// Activations is a collection of property activations
Expand Down Expand Up @@ -49,12 +51,16 @@ func (activations *Activations) GetActivations(property *Property) error {
return err
}

edge.PrintHttpRequest(req, true)

res, err := client.Do(Config, req)

if err != nil {
return err
}

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -169,11 +175,15 @@ func (activation *Activation) GetActivation(property *Property) (time.Duration,
return 0, err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return 0, client.NewAPIError(res)
}
Expand Down Expand Up @@ -236,8 +246,16 @@ func (activation *Activation) Save(property *Property, acknowledgeWarnings bool)
return err
}

edge.PrintHttpRequest(req, true)

res, err := client.Do(Config, req)

if err != nil {
return err
}

edge.PrintHttpResponse(res, true)

if client.IsError(res) && (!acknowledgeWarnings || (acknowledgeWarnings && res.StatusCode != 400)) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -294,8 +312,17 @@ func (activation *Activation) Save(property *Property, acknowledgeWarnings bool)
return err
}

edge.PrintHttpRequest(req, true)

res, err = client.Do(Config, req)

if err != nil {
return err
}


edge.PrintHttpResponse(res, true)

activations := NewActivations()
if err := client.BodyJSON(res, activations); err != nil {
return err
Expand Down Expand Up @@ -395,8 +422,16 @@ func (activation *Activation) Cancel(property *Property) error {
return err
}

edge.PrintHttpRequest(req, true)

res, err := client.Do(Config, req)

if err != nil {
return err
}

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down
9 changes: 9 additions & 0 deletions papi-v1/available.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"

edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
"github.com/xeipuuv/gojsonschema"
)
Expand Down Expand Up @@ -52,11 +53,15 @@ func (availableCriteria *AvailableCriteria) GetAvailableCriteria(property *Prope
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -125,11 +130,15 @@ func (availableBehaviors *AvailableBehaviors) GetAvailableBehaviors(property *Pr
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down
13 changes: 12 additions & 1 deletion papi-v1/client_settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package papi

import "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
import (
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
)

// ClientSettings represents the PAPI client settings resource
type ClientSettings struct {
Expand All @@ -26,11 +29,15 @@ func (clientSettings *ClientSettings) GetClientSettings() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand All @@ -57,11 +64,15 @@ func (clientSettings *ClientSettings) Save() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down
9 changes: 9 additions & 0 deletions papi-v1/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
"github.com/patrickmn/go-cache"
)
Expand Down Expand Up @@ -64,11 +65,15 @@ func (contracts *Contracts) GetContracts() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -155,11 +160,15 @@ func (contract *Contract) GetProducts() (*Products, error) {
return nil, err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return nil, client.NewAPIError(res)
}
Expand Down
21 changes: 21 additions & 0 deletions papi-v1/cpcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
"github.com/patrickmn/go-cache"
)
Expand Down Expand Up @@ -97,11 +98,15 @@ func (cpcodes *CpCodes) GetCpCodes() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -195,8 +200,16 @@ func (cpcode *CpCode) GetCpCode() error {
return err
}

edge.PrintHttpRequest(req, true)

res, err := client.Do(Config, req)

if err != nil {
return err
}

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -252,11 +265,15 @@ func (cpcode *CpCode) Save() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand All @@ -276,11 +293,15 @@ func (cpcode *CpCode) Save() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down
13 changes: 13 additions & 0 deletions papi-v1/custombehaviors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

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

Expand Down Expand Up @@ -56,11 +57,15 @@ func (behaviors *CustomBehaviors) GetCustomBehaviors() error {
return err
}

edge.PrintHttpRequest(req, true)

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

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down Expand Up @@ -119,8 +124,16 @@ func (behavior *CustomBehavior) GetCustomBehavior() error {
return err
}

edge.PrintHttpRequest(req, true)

res, err := client.Do(Config, req)

if err != nil {
return err
}

edge.PrintHttpResponse(res, true)

if client.IsError(res) {
return client.NewAPIError(res)
}
Expand Down
Loading

0 comments on commit 301cd2a

Please sign in to comment.