Skip to content

Commit

Permalink
Facades: Create a request recorder
Browse files Browse the repository at this point in the history
The following creates a request recorder for the charmhub client. It
doesn't really do much atm, but shows that it is possible from the
facade layers.
  • Loading branch information
SimonRichardson committed Jun 18, 2021
1 parent 9fbb2c1 commit e72be30
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
8 changes: 3 additions & 5 deletions apiserver/facades/client/charmhub/charmhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ type Client interface {

// CharmHubAPI API provides the CharmHub API facade for version 1.
type CharmHubAPI struct {
backend Backend
auth facade.Authorizer
client Client
auth facade.Authorizer
client Client
}

// NewFacade creates a new CharmHubAPI facade.
Expand All @@ -60,9 +59,8 @@ func NewFacade(ctx facade.Context) (*CharmHubAPI, error) {
return nil, errors.Trace(err)
}

// TODO (stickupkid): Get the http transport from the facade context
return newCharmHubAPI(m, ctx.Auth(), charmHubClientFactory{
httpTransport: charmhub.DefaultHTTPTransport,
httpTransport: charmhub.RequestRecorderHTTPTransport(ctx.RequestRecorder()),
})
}

Expand Down
31 changes: 30 additions & 1 deletion charmhub/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"sort"
"time"

"github.com/juju/errors"
jujuhttp "github.com/juju/http/v2"
Expand All @@ -37,7 +39,34 @@ type Transport interface {

// DefaultHTTPTransport creates a new HTTPTransport.
func DefaultHTTPTransport(logger Logger) Transport {
return jujuhttp.NewClient(jujuhttp.WithLogger(logger))
return RequestRecorderHTTPTransport(loggingRequestRecorder{
logger: logger,
})(logger)
}

type loggingRequestRecorder struct {
logger Logger
}

// Record an outgoing request which produced an http.Response.
func (r loggingRequestRecorder) Record(method string, url *url.URL, res *http.Response, rtt time.Duration) {

}

// Record an outgoing request which returned back an error.
func (r loggingRequestRecorder) RecordError(method string, url *url.URL, err error) {

}

// RequestRecorderHTTPTransport creates a new HTTPTransport that records the
// requests.
func RequestRecorderHTTPTransport(recorder jujuhttp.RequestRecorder) func(logger Logger) Transport {
return func(logger Logger) Transport {
return jujuhttp.NewClient(
jujuhttp.WithRequestRecorder(recorder),
jujuhttp.WithLogger(logger),
)
}
}

// APIRequester creates a wrapper around the transport to allow for better
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d
github.com/juju/gojsonschema v0.0.0-20150312170016-e1ad140384f2
github.com/juju/gomaasapi/v2 v2.0.0-20210323144809-92beddd020fe
github.com/juju/http/v2 v2.0.0-20210527161802-e8d841c4e076
github.com/juju/http/v2 v2.0.0-20210616081525-ede00d07798a
github.com/juju/idmclient/v2 v2.0.0-20210309081103-6b4a5212f851
github.com/juju/jsonschema v0.0.0-20210422141032-b0ff291abe9c
github.com/juju/jsonschema-gen v0.0.0-20200416014454-d924343d72b2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ github.com/juju/gosigma v0.0.0-20200420012028-063911838a9e h1:JoXBbhRrYNw6EIPGcM
github.com/juju/gosigma v0.0.0-20200420012028-063911838a9e/go.mod h1:OPBu48GcIJ30kNTA1cm+VbZb6GkQ6vthnr5v6NJ49eM=
github.com/juju/http v0.0.0-20201019013536-69ae1d429836 h1:F+KhYWcKHSUf/R7Ovoz+s6VnK1wGFibaCrPXPYijFa4=
github.com/juju/http v0.0.0-20201019013536-69ae1d429836/go.mod h1:lbZ9zbaOw9vMW7XMHGxYTgFadDDfzc4r8Aa7gP8GOYo=
github.com/juju/http/v2 v2.0.0-20210527161802-e8d841c4e076 h1:Y9skx/w7NYVMj5N7t+rV66BYEMItpoSDG+Stb8beRuU=
github.com/juju/http/v2 v2.0.0-20210527161802-e8d841c4e076/go.mod h1:y1PngvubGWeSpDBI4kxdaTc9f3HEThd2A5mv0MKBi74=
github.com/juju/http/v2 v2.0.0-20210616081525-ede00d07798a h1:Moqm99huPhL1lffDs8uNZ6VJ3kAPDxPPu/kbZlMAy98=
github.com/juju/http/v2 v2.0.0-20210616081525-ede00d07798a/go.mod h1:y1PngvubGWeSpDBI4kxdaTc9f3HEThd2A5mv0MKBi74=
github.com/juju/httpprof v0.0.0-20141217160036-14bf14c30767 h1:COsaGcfAONDdIDnGS8yFdxOyReP7zKQEr7jFzCHKDkM=
github.com/juju/httpprof v0.0.0-20141217160036-14bf14c30767/go.mod h1:+MaLYz4PumRkkyHYeXJ2G5g5cIW0sli2bOfpmbaMV/g=
github.com/juju/httprequest v1.0.1/go.mod h1:K+CyYVHU/NcfbMpK7YIVobh4U4Fci3EUB2AqIRtl+xs=
Expand Down

0 comments on commit e72be30

Please sign in to comment.