Skip to content

Commit

Permalink
godep User-Agent updates (#49)
Browse files Browse the repository at this point in the history
Allow specifying User-Agent on the CLI for depsyncer. Change User-Agent for depsyncer. Optionize the godep Client (with options for UA and for HTTP client).
  • Loading branch information
jessepeterson authored Mar 13, 2024
1 parent 6100355 commit 5d6cf26
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/depsyncer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
stdlog "log"
"net/http"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -34,6 +33,7 @@ func main() {
flStorage = flag.String("storage", "file", "storage backend")
flDSN = flag.String("storage-dsn", "", "storage data source name")
flWebhook = flag.String("webhook-url", "", "URL to send requests to")
flUA = flag.String("user-agent", godep.UserAgent, "User-Agent string to use")
)
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [flags] <DEPname1> [DEPname2 [...]]\nFlags:\n", os.Args[0])
Expand Down Expand Up @@ -118,7 +118,7 @@ func main() {
}
}()

client := godep.NewClient(storage, http.DefaultClient)
client := godep.NewClient(storage, godep.WithUserAgent(*flUA))

var wg sync.WaitGroup

Expand Down
50 changes: 35 additions & 15 deletions godep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const (
mediaType = "application/json;charset=UTF8"
userAgent = "nanodep-godep/0"
UserAgent = "nanodep-g-o-dep/0"
)

// HTTPError encapsulates an HTTP response error from the DEP requests.
Expand Down Expand Up @@ -65,27 +65,45 @@ type ClientStorage interface {

// Client represents an Apple DEP API client identified by a single DEP name.
type Client struct {
store ClientStorage
store ClientStorage
client *http.Client // for DEP API authentication and session management
ua string // HTTP User-Agent
}

// Options change the configuration of the godep Client.
type Option func(*Client)

// an HTTP client that handles DEP API authentication and session management
client *http.Client
// WithUserAgent sets the the HTTP User-Agent string to be used on each request.
func WithUserAgent(ua string) Option {
return func(c *Client) {
c.ua = ua
}
}

// NewClient creates new Client and reads authentication and config data
// from store. The provided client is copied and modified by wrapping its
// WithClient configures the HTTP client to be used.
// The provided client is copied and modified by wrapping its
// transport in a new NanoDEP transport (which transparently handles
// authentication and session management). If client is nil then
// authentication and session management). If not set then
// http.DefaultClient is used.
func NewClient(store ClientStorage, client *http.Client) *Client {
if client == nil {
client = http.DefaultClient
func WithClient(client *http.Client) Option {
return func(c *Client) {
c.client = client
}
t := depclient.NewTransport(client.Transport, client, store, nil)
client = depclient.NewClient(client, t)
return &Client{
}

// NewClient creates new Client and reads authentication and config data from store.
func NewClient(store ClientStorage, opts ...Option) *Client {
c := &Client{
store: store,
client: client,
client: http.DefaultClient,
ua: UserAgent,
}
for _, opt := range opts {
opt(c)
}
t := depclient.NewTransport(c.client.Transport, c.client, store, nil)
c.client = depclient.NewClient(c.client, t)
return c
}

// do executes the HTTP request using the client's HTTP client which
Expand All @@ -106,7 +124,9 @@ func (c *Client) do(ctx context.Context, name, method, path string, in interface
if err != nil {
return err
}
req.Header.Set("User-Agent", userAgent)
if c.ua != "" {
req.Header.Set("User-Agent", c.ua)
}
if body != nil {
req.Header.Set("Content-Type", mediaType)
}
Expand Down

0 comments on commit 5d6cf26

Please sign in to comment.