Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom HTTP client for Connection #122

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions azuredevops/client.go
Original file line number Diff line number Diff line change
@@ -53,12 +53,17 @@ var baseUserAgent = "go/" + runtime.Version() + " (" + runtime.GOOS + " " + runt
// NewClient provides an Azure DevOps client
// and copies the TLS config and timeout from the supplied connection
func NewClient(connection *Connection, baseUrl string) *Client {
httpClient := &http.Client{}
if connection.TlsConfig != nil {
httpClient.Transport = &http.Transport{TLSClientConfig: connection.TlsConfig}
}
if connection.Timeout != nil {
httpClient.Timeout = *connection.Timeout
var httpClient *http.Client
if connection.httpClient != nil {
httpClient = connection.httpClient
} else {
httpClient = &http.Client{}
if connection.TlsConfig != nil {
httpClient.Transport = &http.Transport{TLSClientConfig: connection.TlsConfig}
}
if connection.Timeout != nil {
httpClient.Timeout = *connection.Timeout
}
}

return NewClientWithOptions(connection, baseUrl, WithHTTPClient(httpClient))
26 changes: 18 additions & 8 deletions azuredevops/connection.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"context"
"crypto/tls"
"encoding/base64"
"net/http"
"strings"
"sync"
"time"
@@ -16,21 +17,29 @@ import (

// Creates a new Azure DevOps connection instance using a personal access token.
func NewPatConnection(organizationUrl string, personalAccessToken string) *Connection {
authorizationString := CreateBasicAuthHeaderValue("", personalAccessToken)
organizationUrl = normalizeUrl(organizationUrl)
return &Connection{
AuthorizationString: authorizationString,
BaseUrl: organizationUrl,
SuppressFedAuthRedirect: true,
}
return NewConnectionWithOptions(organizationUrl, WithConnectionPersonalAccessToken(personalAccessToken))
}

// NewAnonymousConnection creates a new connection without specifying any authentication. This
// is equivalent to calling NewConnectionWithOptions without specifying any options.
func NewAnonymousConnection(organizationUrl string) *Connection {
return NewConnectionWithOptions(organizationUrl)
}

// NewConnectionWithOptions creates a new connection using the specified options. See WithConnection*() functions.
func NewConnectionWithOptions(organizationUrl string, options ...ConnectionOptionFunc) *Connection {
organizationUrl = normalizeUrl(organizationUrl)
return &Connection{
connection := &Connection{
BaseUrl: organizationUrl,
SuppressFedAuthRedirect: true,
httpClient: &http.Client{},
}

for _, fn := range options {
fn(connection)
}

return connection
}

type Connection struct {
@@ -45,6 +54,7 @@ type Connection struct {
clientCacheLock sync.RWMutex
resourceAreaCache map[uuid.UUID]ResourceAreaInfo
resourceAreaCacheLock sync.RWMutex
httpClient *http.Client
}

func CreateBasicAuthHeaderValue(username, password string) string {
20 changes: 20 additions & 0 deletions azuredevops/connection_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package azuredevops

import "net/http"

type ConnectionOptionFunc func(*Connection)

// WithConnectionHTTPClient sets the HTTP client that will be used by the connection.
func WithConnectionHTTPClient(client *http.Client) ConnectionOptionFunc {
return func(c *Connection) {
c.httpClient = client
}
}

// WithConnectionPersonalAccessToken specifies the PAT to use for authentication with Azure DevOps.
func WithConnectionPersonalAccessToken(personalAccessToken string) ConnectionOptionFunc {
return func(c *Connection) {
authorizationString := CreateBasicAuthHeaderValue("", personalAccessToken)
c.AuthorizationString = authorizationString
}
}