Blame vendor/github.com/Azure/go-autorest/README.md

Packit Service 3a6627
# go-autorest
Packit Service 3a6627
Packit Service 3a6627
[![GoDoc](https://godoc.org/github.com/Azure/go-autorest/autorest?status.png)](https://godoc.org/github.com/Azure/go-autorest/autorest)
Packit Service 3a6627
[![Build Status](https://dev.azure.com/azure-sdk/public/_apis/build/status/go/Azure.go-autorest?branchName=master)](https://dev.azure.com/azure-sdk/public/_build/latest?definitionId=625&branchName=master)
Packit Service 3a6627
[![Go Report Card](https://goreportcard.com/badge/Azure/go-autorest)](https://goreportcard.com/report/Azure/go-autorest)
Packit Service 3a6627
Packit Service 3a6627
Package go-autorest provides an HTTP request client for use with [Autorest](https://github.com/Azure/autorest.go)-generated API client packages.
Packit Service 3a6627
Packit Service 3a6627
An authentication client tested with Azure Active Directory (AAD) is also
Packit Service 3a6627
provided in this repo in the package
Packit Service 3a6627
`github.com/Azure/go-autorest/autorest/adal`.  Despite its name, this package
Packit Service 3a6627
is maintained only as part of the Azure Go SDK and is not related to other
Packit Service 3a6627
"ADAL" libraries in [github.com/AzureAD](https://github.com/AzureAD).
Packit Service 3a6627
Packit Service 3a6627
## Overview
Packit Service 3a6627
Packit Service 3a6627
Package go-autorest implements an HTTP request pipeline suitable for use across
Packit Service 3a6627
multiple goroutines and provides the shared routines used by packages generated
Packit Service 3a6627
by [Autorest](https://github.com/Azure/autorest.go).
Packit Service 3a6627
Packit Service 3a6627
The package breaks sending and responding to HTTP requests into three phases: Preparing, Sending,
Packit Service 3a6627
and Responding. A typical pattern is:
Packit Service 3a6627
Packit Service 3a6627
```go
Packit Service 3a6627
  req, err := Prepare(&http.Request{},
Packit Service 3a6627
    token.WithAuthorization())
Packit Service 3a6627
Packit Service 3a6627
  resp, err := Send(req,
Packit Service 3a6627
    WithLogging(logger),
Packit Service 3a6627
    DoErrorIfStatusCode(http.StatusInternalServerError),
Packit Service 3a6627
    DoCloseIfError(),
Packit Service 3a6627
    DoRetryForAttempts(5, time.Second))
Packit Service 3a6627
Packit Service 3a6627
  err = Respond(resp,
Packit Service 3a6627
		ByDiscardingBody(),
Packit Service 3a6627
    ByClosing())
Packit Service 3a6627
```
Packit Service 3a6627
Packit Service 3a6627
Each phase relies on decorators to modify and / or manage processing. Decorators may first modify
Packit Service 3a6627
and then pass the data along, pass the data first and then modify the result, or wrap themselves
Packit Service 3a6627
around passing the data (such as a logger might do). Decorators run in the order provided. For
Packit Service 3a6627
example, the following:
Packit Service 3a6627
Packit Service 3a6627
```go
Packit Service 3a6627
  req, err := Prepare(&http.Request{},
Packit Service 3a6627
    WithBaseURL("https://microsoft.com/"),
Packit Service 3a6627
    WithPath("a"),
Packit Service 3a6627
    WithPath("b"),
Packit Service 3a6627
    WithPath("c"))
Packit Service 3a6627
```
Packit Service 3a6627
Packit Service 3a6627
will set the URL to:
Packit Service 3a6627
Packit Service 3a6627
```
Packit Service 3a6627
  https://microsoft.com/a/b/c
Packit Service 3a6627
```
Packit Service 3a6627
Packit Service 3a6627
Preparers and Responders may be shared and re-used (assuming the underlying decorators support
Packit Service 3a6627
sharing and re-use). Performant use is obtained by creating one or more Preparers and Responders
Packit Service 3a6627
shared among multiple go-routines, and a single Sender shared among multiple sending go-routines,
Packit Service 3a6627
all bound together by means of input / output channels.
Packit Service 3a6627
Packit Service 3a6627
Decorators hold their passed state within a closure (such as the path components in the example
Packit Service 3a6627
above). Be careful to share Preparers and Responders only in a context where such held state
Packit Service 3a6627
applies. For example, it may not make sense to share a Preparer that applies a query string from a
Packit Service 3a6627
fixed set of values. Similarly, sharing a Responder that reads the response body into a passed
Packit Service 3a6627
struct (e.g., `ByUnmarshallingJson`) is likely incorrect.
Packit Service 3a6627
Packit Service 3a6627
Errors raised by autorest objects and methods will conform to the `autorest.Error` interface.
Packit Service 3a6627
Packit Service 3a6627
See the included examples for more detail. For details on the suggested use of this package by
Packit Service 3a6627
generated clients, see the Client described below.
Packit Service 3a6627
Packit Service 3a6627
## Helpers
Packit Service 3a6627
Packit Service 3a6627
### Handling Swagger Dates
Packit Service 3a6627
Packit Service 3a6627
The Swagger specification (https://swagger.io) that drives AutoRest
Packit Service 3a6627
(https://github.com/Azure/autorest/) precisely defines two date forms: date and date-time. The
Packit Service 3a6627
github.com/Azure/go-autorest/autorest/date package provides time.Time derivations to ensure correct
Packit Service 3a6627
parsing and formatting.
Packit Service 3a6627
Packit Service 3a6627
### Handling Empty Values
Packit Service 3a6627
Packit Service 3a6627
In JSON, missing values have different semantics than empty values. This is especially true for
Packit Service 3a6627
services using the HTTP PATCH verb. The JSON submitted with a PATCH request generally contains
Packit Service 3a6627
only those values to modify. Missing values are to be left unchanged. Developers, then, require a
Packit Service 3a6627
means to both specify an empty value and to leave the value out of the submitted JSON.
Packit Service 3a6627
Packit Service 3a6627
The Go JSON package (`encoding/json`) supports the `omitempty` tag. When specified, it omits
Packit Service 3a6627
empty values from the rendered JSON. Since Go defines default values for all base types (such as ""
Packit Service 3a6627
for string and 0 for int) and provides no means to mark a value as actually empty, the JSON package
Packit Service 3a6627
treats default values as meaning empty, omitting them from the rendered JSON. This means that, using
Packit Service 3a6627
the Go base types encoded through the default JSON package, it is not possible to create JSON to
Packit Service 3a6627
clear a value at the server.
Packit Service 3a6627
Packit Service 3a6627
The workaround within the Go community is to use pointers to base types in lieu of base types within
Packit Service 3a6627
structures that map to JSON. For example, instead of a value of type `string`, the workaround uses
Packit Service 3a6627
`*string`. While this enables distinguishing empty values from those to be unchanged, creating
Packit Service 3a6627
pointers to a base type (notably constant, in-line values) requires additional variables. This, for
Packit Service 3a6627
example,
Packit Service 3a6627
Packit Service 3a6627
```go
Packit Service 3a6627
  s := struct {
Packit Service 3a6627
    S *string
Packit Service 3a6627
  }{ S: &"foo" }
Packit Service 3a6627
```
Packit Service 3a6627
fails, while, this
Packit Service 3a6627
Packit Service 3a6627
```go
Packit Service 3a6627
  v := "foo"
Packit Service 3a6627
  s := struct {
Packit Service 3a6627
    S *string
Packit Service 3a6627
  }{ S: &v }
Packit Service 3a6627
```
Packit Service 3a6627
succeeds.
Packit Service 3a6627
Packit Service 3a6627
To ease using pointers, the subpackage `to` contains helpers that convert to and from pointers for
Packit Service 3a6627
Go base types which have Swagger analogs. It also provides a helper that converts between
Packit Service 3a6627
`map[string]string` and `map[string]*string`, enabling the JSON to specify that the value
Packit Service 3a6627
associated with a key should be cleared. With the helpers, the previous example becomes
Packit Service 3a6627
Packit Service 3a6627
```go
Packit Service 3a6627
  s := struct {
Packit Service 3a6627
    S *string
Packit Service 3a6627
  }{ S: to.StringPtr("foo") }
Packit Service 3a6627
```
Packit Service 3a6627
Packit Service 3a6627
## Install
Packit Service 3a6627
Packit Service 3a6627
```bash
Packit Service 3a6627
go get github.com/Azure/go-autorest/autorest
Packit Service 3a6627
go get github.com/Azure/go-autorest/autorest/azure
Packit Service 3a6627
go get github.com/Azure/go-autorest/autorest/date
Packit Service 3a6627
go get github.com/Azure/go-autorest/autorest/to
Packit Service 3a6627
```
Packit Service 3a6627
Packit Service 3a6627
### Using with Go Modules
Packit Service 3a6627
In [v12.0.1](https://github.com/Azure/go-autorest/pull/386), this repository introduced the following modules.
Packit Service 3a6627
Packit Service 3a6627
- autorest/adal
Packit Service 3a6627
- autorest/azure/auth
Packit Service 3a6627
- autorest/azure/cli
Packit Service 3a6627
- autorest/date
Packit Service 3a6627
- autorest/mocks
Packit Service 3a6627
- autorest/to
Packit Service 3a6627
- autorest/validation
Packit Service 3a6627
- autorest
Packit Service 3a6627
- logger
Packit Service 3a6627
- tracing
Packit Service 3a6627
Packit Service 3a6627
Tagging cumulative SDK releases as a whole (e.g. `v12.3.0`) is still enabled to support consumers of this repo that have not yet migrated to modules.
Packit Service 3a6627
Packit Service 3a6627
## License
Packit Service 3a6627
Packit Service 3a6627
See LICENSE file.
Packit Service 3a6627
Packit Service 3a6627
-----
Packit Service 3a6627
Packit Service 3a6627
This project has adopted the [Microsoft Open Source Code of
Packit Service 3a6627
Conduct](https://opensource.microsoft.com/codeofconduct/). For more information
Packit Service 3a6627
see the [Code of Conduct
Packit Service 3a6627
FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
Packit Service 3a6627
[opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional
Packit Service 3a6627
questions or comments.