Skip to content

Commit

Permalink
fix resource and client caching and ensure connection passed by ref. (#8
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tedchamb authored Aug 1, 2019
1 parent 001c69b commit b743f2a
Show file tree
Hide file tree
Showing 59 changed files with 1,226 additions and 1,216 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
ctx := context.Background()
// Create a client to interact with the Core area
coreClient, err := core.NewClient(ctx, *connection)
coreClient, err := core.NewClient(ctx, connection)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions azureDevOps/accounts/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Client struct {
Client azureDevOps.Client
}

func NewClient(ctx context.Context, connection azureDevOps.Connection) (*Client, error) {
func NewClient(ctx context.Context, connection *azureDevOps.Connection) (*Client, error) {
client, err := connection.GetClientByResourceAreaId(ctx, ResourceAreaId)
if err != nil {
return nil, err
Expand All @@ -33,7 +33,7 @@ func NewClient(ctx context.Context, connection azureDevOps.Connection) (*Client,
}

// Get a list of accounts for a specific owner or a specific member.
func (client Client) GetAccounts(ctx context.Context, args GetAccountsArgs) (*[]Account, error) {
func (client *Client) GetAccounts(ctx context.Context, args GetAccountsArgs) (*[]Account, error) {
queryParams := url.Values{}
if args.OwnerId != nil {
queryParams.Add("ownerId", (*args.OwnerId).String())
Expand Down
6 changes: 3 additions & 3 deletions azureDevOps/audit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Client struct {
Client azureDevOps.Client
}

func NewClient(ctx context.Context, connection azureDevOps.Connection) (*Client, error) {
func NewClient(ctx context.Context, connection *azureDevOps.Connection) (*Client, error) {
client, err := connection.GetClientByResourceAreaId(ctx, ResourceAreaId)
if err != nil {
return nil, err
Expand All @@ -36,7 +36,7 @@ func NewClient(ctx context.Context, connection azureDevOps.Connection) (*Client,
}

// [Preview API] Queries audit log entries
func (client Client) QueryLog(ctx context.Context, args QueryLogArgs) (*AuditLogQueryResult, error) {
func (client *Client) QueryLog(ctx context.Context, args QueryLogArgs) (*AuditLogQueryResult, error) {
queryParams := url.Values{}
if args.StartTime != nil {
queryParams.Add("startTime", (*args.StartTime).String())
Expand Down Expand Up @@ -79,7 +79,7 @@ type QueryLogArgs struct {
}

// [Preview API] Downloads audit log entries.
func (client Client) DownloadLog(ctx context.Context, args DownloadLogArgs) (io.ReadCloser, error) {
func (client *Client) DownloadLog(ctx context.Context, args DownloadLogArgs) (io.ReadCloser, error) {
queryParams := url.Values{}
if args.Format == nil {
return nil, &azureDevOps.ArgumentNilError{ArgumentName: "format"}
Expand Down
158 changes: 79 additions & 79 deletions azureDevOps/build/client.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions azureDevOps/cix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ type Client struct {
Client azureDevOps.Client
}

func NewClient(ctx context.Context, connection azureDevOps.Connection) *Client {
func NewClient(ctx context.Context, connection *azureDevOps.Connection) *Client {
client := connection.GetClientByUrl(connection.BaseUrl)
return &Client {
Client: *client,
}
}

// [Preview API] Gets a list of existing configuration files for the given repository.
func (client Client) GetConfigurations(ctx context.Context, args GetConfigurationsArgs) (*[]ConfigurationFile, error) {
func (client *Client) GetConfigurations(ctx context.Context, args GetConfigurationsArgs) (*[]ConfigurationFile, error) {
routeValues := make(map[string]string)
if args.Project == nil || *args.Project == "" {
return nil, &azureDevOps.ArgumentNilOrEmptyError{ArgumentName: "project"}
Expand Down Expand Up @@ -76,7 +76,7 @@ type GetConfigurationsArgs struct {
}

// [Preview API] Creates a new Pipeline connection between the provider installation and the specified project. Returns the PipelineConnection object created.
func (client Client) CreateProjectConnection(ctx context.Context, args CreateProjectConnectionArgs) (*PipelineConnection, error) {
func (client *Client) CreateProjectConnection(ctx context.Context, args CreateProjectConnectionArgs) (*PipelineConnection, error) {
if args.CreateConnectionInputs == nil {
return nil, &azureDevOps.ArgumentNilError{ArgumentName: "createConnectionInputs"}
}
Expand Down Expand Up @@ -109,7 +109,7 @@ type CreateProjectConnectionArgs struct {
}

// [Preview API] Returns a list of build frameworks that best match the given repository based on its contents.
func (client Client) GetDetectedBuildFrameworks(ctx context.Context, args GetDetectedBuildFrameworksArgs) (*[]DetectedBuildFramework, error) {
func (client *Client) GetDetectedBuildFrameworks(ctx context.Context, args GetDetectedBuildFrameworksArgs) (*[]DetectedBuildFramework, error) {
routeValues := make(map[string]string)
if args.Project == nil || *args.Project == "" {
return nil, &azureDevOps.ArgumentNilOrEmptyError{ArgumentName: "project"}
Expand Down Expand Up @@ -160,7 +160,7 @@ type GetDetectedBuildFrameworksArgs struct {
}

// [Preview API] Returns a list of all YAML templates with weighting based on which would best fit the given repository.
func (client Client) GetTemplateRecommendations(ctx context.Context, args GetTemplateRecommendationsArgs) (*[]Template, error) {
func (client *Client) GetTemplateRecommendations(ctx context.Context, args GetTemplateRecommendationsArgs) (*[]Template, error) {
routeValues := make(map[string]string)
if args.Project == nil || *args.Project == "" {
return nil, &azureDevOps.ArgumentNilOrEmptyError{ArgumentName: "project"}
Expand Down Expand Up @@ -206,7 +206,7 @@ type GetTemplateRecommendationsArgs struct {
}

// [Preview API]
func (client Client) CreateResources(ctx context.Context, args CreateResourcesArgs) (*CreatedResources, error) {
func (client *Client) CreateResources(ctx context.Context, args CreateResourcesArgs) (*CreatedResources, error) {
if args.CreationParameters == nil {
return nil, &azureDevOps.ArgumentNilError{ArgumentName: "creationParameters"}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ type CreateResourcesArgs struct {
}

// [Preview API]
func (client Client) RenderTemplate(ctx context.Context, args RenderTemplateArgs) (*Template, error) {
func (client *Client) RenderTemplate(ctx context.Context, args RenderTemplateArgs) (*Template, error) {
if args.TemplateParameters == nil {
return nil, &azureDevOps.ArgumentNilError{ArgumentName: "templateParameters"}
}
Expand Down
50 changes: 28 additions & 22 deletions azureDevOps/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,39 @@ var apiResourceLocationCacheLock = sync.RWMutex{}
// Base user agent string. The UserAgent set on the connection will be appended to this.
var baseUserAgent = "go/" + runtime.Version() + " (" + runtime.GOOS + " " + runtime.GOARCH + ") azure-devops-go-api/0.0.0" // todo: get real version

func NewClient(connection Connection, baseUrl string) *Client {
func NewClient(connection *Connection, baseUrl string) *Client {
client := &http.Client{}
if connection.Timeout != nil {
client.Timeout = *connection.Timeout
}
return &Client {
baseUrl: baseUrl,
connection: connection,
client: *client,
client: client,
authorization: connection.AuthorizationString,
suppressFedAuthRedirect: connection.SuppressFedAuthRedirect,
forceMsaPassThrough: connection.ForceMsaPassThrough,
userAgent: connection.UserAgent,
}
}

type Client struct {
baseUrl string
connection Connection
client http.Client
client *http.Client
authorization string
suppressFedAuthRedirect bool
forceMsaPassThrough bool
userAgent string
}

func (client Client) SendRequest(request *http.Request) (response *http.Response, err error) {
func (client *Client) SendRequest(request *http.Request) (response *http.Response, err error) {
resp, err := client.client.Do(request) // todo: add retry logic
if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
err = client.UnwrapError(resp)
}
return resp, err
}

func (client Client) Send(ctx context.Context,
func (client *Client) Send(ctx context.Context,
httpMethod string,
locationId uuid.UUID,
apiVersion string,
Expand Down Expand Up @@ -110,7 +116,7 @@ func (client Client) Send(ctx context.Context,
return resp, err
}

func (client Client) GenerateUrl(apiResourceLocation *ApiResourceLocation, routeValues map[string]string, queryParameters url.Values) (request string) {
func (client *Client) GenerateUrl(apiResourceLocation *ApiResourceLocation, routeValues map[string]string, queryParameters url.Values) (request string) {
builtUrl := *apiResourceLocation.RouteTemplate
if routeValues == nil {
routeValues = make(map[string]string)
Expand All @@ -124,7 +130,7 @@ func (client Client) GenerateUrl(apiResourceLocation *ApiResourceLocation, route
return builtUrl
}

func (client Client) CreateRequestMessage(ctx context.Context,
func (client *Client) CreateRequestMessage(ctx context.Context,
httpMethod string,
url string,
apiVersion string,
Expand All @@ -141,8 +147,8 @@ func (client Client) CreateRequestMessage(ctx context.Context,
req = req.WithContext(ctx)
}

if client.connection.AuthorizationString != "" {
req.Header.Add(headerKeyAuthorization, client.connection.AuthorizationString)
if client.authorization != "" {
req.Header.Add(headerKeyAuthorization, client.authorization)
}
accept := acceptMediaType
if apiVersion != "" {
Expand All @@ -152,10 +158,10 @@ func (client Client) CreateRequestMessage(ctx context.Context,
if mediaType != "" {
req.Header.Add(headerKeyContentType, mediaType+";charset=utf-8")
}
if client.connection.SuppressFedAuthRedirect {
if client.suppressFedAuthRedirect {
req.Header.Add(headerKeyFedAuthRedirect, "Suppress")
}
if client.connection.ForceMsaPassThrough {
if client.forceMsaPassThrough {
req.Header.Add(headerKeyForceMsaPassThrough, "true")
}

Expand All @@ -166,8 +172,8 @@ func (client Client) CreateRequestMessage(ctx context.Context,
}

userAgent := baseUserAgent
if client.connection.UserAgent != "" {
userAgent += " " + client.connection.UserAgent
if client.userAgent != "" {
userAgent += " " + client.userAgent
}
req.Header.Add(headerUserAgent, userAgent)

Expand All @@ -178,7 +184,7 @@ func (client Client) CreateRequestMessage(ctx context.Context,
return req, err
}

func (client Client) getResourceLocation(ctx context.Context, locationId uuid.UUID) (*ApiResourceLocation, error) {
func (client *Client) getResourceLocation(ctx context.Context, locationId uuid.UUID) (*ApiResourceLocation, error) {
locationsMap, ok := getApiResourceLocationCache(client.baseUrl)
if !ok {
locations, err := client.getResourceLocationsFromServer(ctx)
Expand Down Expand Up @@ -215,7 +221,7 @@ func setApiResourceLocationCache(url string, locationsMap *map[uuid.UUID] ApiRes
apiResourceLocationCache[url] = locationsMap
}

func (client Client) getResourceLocationsFromServer(ctx context.Context,) ([]ApiResourceLocation, error) {
func (client *Client) getResourceLocationsFromServer(ctx context.Context,) ([]ApiResourceLocation, error) {
optionsUri := combineUrl(client.baseUrl, "_apis")
request, err := client.CreateRequestMessage(ctx, http.MethodOptions, optionsUri, "", nil, "", MediaTypeApplicationJson, nil)
if err != nil {
Expand Down Expand Up @@ -334,7 +340,7 @@ func transformRouteTemplate(routeTemplate string, routeValues map[string]string)
return newTemplate
}

func (client Client) UnmarshalBody(response *http.Response, v interface{}) (err error) {
func (client *Client) UnmarshalBody(response *http.Response, v interface{}) (err error) {
if response != nil && response.Body != nil {
var err error
defer func() {
Expand All @@ -352,7 +358,7 @@ func (client Client) UnmarshalBody(response *http.Response, v interface{}) (err
return nil
}

func (client Client) UnmarshalCollectionBody(response *http.Response, v interface{}) (err error) {
func (client *Client) UnmarshalCollectionBody(response *http.Response, v interface{}) (err error) {
if response != nil && response.Body != nil {
var err error
defer func() {
Expand All @@ -371,7 +377,7 @@ func (client Client) UnmarshalCollectionBody(response *http.Response, v interfac
return nil
}

func (client Client) UnmarshalCollectionJson(jsonValue []byte, v interface{}) (err error) {
func (client *Client) UnmarshalCollectionJson(jsonValue []byte, v interface{}) (err error) {
var wrappedResponse VssJsonCollectionWrapper
err = json.Unmarshal(jsonValue, &wrappedResponse)
if err != nil {
Expand All @@ -391,7 +397,7 @@ func trimByteOrderMark(body []byte) []byte {
return bytes.TrimPrefix(body, []byte("\xef\xbb\xbf"))
}

func (client Client) UnwrapError(response *http.Response) (err error) {
func (client *Client) UnwrapError(response *http.Response) (err error) {
if response.ContentLength == 0 {
message := "Request returned status: " + response.Status
return &WrappedError{
Expand Down Expand Up @@ -441,7 +447,7 @@ func (client Client) UnwrapError(response *http.Response) (err error) {
return wrappedError
}

func (client Client) GetResourceAreas(ctx context.Context,) (*[]ResourceAreaInfo, error) {
func (client *Client) GetResourceAreas(ctx context.Context,) (*[]ResourceAreaInfo, error) {
queryParams := url.Values{}
locationId, _ := uuid.Parse("e81700f7-3be2-46de-8624-2eb35882fcaa")
resp, err := client.Send(ctx, http.MethodGet, locationId, "5.1-preview.1", nil, queryParams, nil, "", "application/json", nil)
Expand Down
4 changes: 2 additions & 2 deletions azureDevOps/clientTrace/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ type Client struct {
Client azureDevOps.Client
}

func NewClient(ctx context.Context, connection azureDevOps.Connection) *Client {
func NewClient(ctx context.Context, connection *azureDevOps.Connection) *Client {
client := connection.GetClientByUrl(connection.BaseUrl)
return &Client {
Client: *client,
}
}

// [Preview API]
func (client Client) PublishEvents(ctx context.Context, args PublishEventsArgs) error {
func (client *Client) PublishEvents(ctx context.Context, args PublishEventsArgs) error {
if args.Events == nil {
return &azureDevOps.ArgumentNilError{ArgumentName: "events"}
}
Expand Down
Loading

0 comments on commit b743f2a

Please sign in to comment.