commit | be10e3f9b309a20949bf1722841ced0205026254 | [log] [tgz] |
---|---|---|
author | Seth Hollyman <shollyman@google.com> | Wed Sep 25 17:54:44 2019 +0000 |
committer | Seth Hollyman <shollyman@google.com> | Mon Oct 28 16:54:45 2019 +0000 |
tree | d6a3a74b0d3948afb3fa0fa351c3d0e9ec0613bc | |
parent | 3d82a3b2e4197d731f8b34baa41f0dd62ba5f8d3 [diff] |
google-api-go-generator: generalize media path replacement This change improves media path handling when dealing with API methods that support multipart media upload. By convention, most APIs at Google leverage an /upload/ alternate prefix path to do with multipart requests, as they rely on a common infrastructure component that uses this convention. This removes special path replacement behavior by simply leveraging the supplied media path as expressed in the discovery doc reference: https://developers.google.com/discovery/v1/reference/apis This has the effect of allowing APIs to generate correctly when using hostnames other than www.googleapis.com as the root URL, which previous behavior relied upon. This PR also updates tests, as there were a lot of implicit behaviors that don't reflect proper usage. For example, while under test the BasePath was frequently set to the bare server.URL e.g. http://n.n.n.n:p/ which meant that things like the prior redirect hack was ignored entirely. This change also adds a test for googleapi.ResolveRelative to ensure this path replacement behavior is preserved for future maintainers. Change-Id: Icbc9bccf9f168f6c78d17b7feab6d82c6a4a1199 Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/46011 Reviewed-by: Chris Broadfoot <cbro@google.com> Reviewed-by: kokoro <noreply+kokoro@google.com>
$ go get google.golang.org/api/tasks/v1 $ go get google.golang.org/api/moderator/v1 $ go get google.golang.org/api/urlshortener/v1 ... etc ...
and using:
package main import ( "net/http" "google.golang.org/api/urlshortener/v1" ) func main() { svc, err := urlshortener.New(http.DefaultClient) // ... }
These are auto-generated Go libraries from the Google Discovery Service's JSON description files of the available “new style” Google APIs.
Due to the auto-generated nature of this collection of libraries, complete APIs or specific versions can appear or go away without notice. As a result, you should always locally vendor any API(s) that your code relies upon.
These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.
If you're working with Google Cloud Platform APIs such as Datastore or Pub/Sub, consider using the Cloud Client Libraries for Go instead. These are the new and idiomatic Go libraries targeted specifically at Google Cloud Platform Services.
The generator itself and the code it produces are beta. Some APIs are alpha/beta, and indicated as such in the import path (e.g., “google.golang.org/api/someapi/v1alpha”).
Application Default Credentials provide a simplified way to obtain credentials for authenticating with Google APIs.
The Application Default Credentials authenticate as the application itself, which make them great for working with Google Cloud APIs like Storage or Datastore. They are the recommended form of authentication when building applications that run on Google Compute Engine or Google App Engine.
Default credentials are provided by the golang.org/x/oauth2/google
package. To use them, add the following import:
import "golang.org/x/oauth2/google"
Some credentials types require you to specify scopes, and service entry points may not inject them. If you encounter this situation you may need to specify scopes as follows:
import ( "context" "golang.org/x/oauth2/google" "google.golang.org/api/compute/v1" ) func main() { // Use oauth2.NoContext if there isn't a good context to pass in. ctx := context.Background() client, err := google.DefaultClient(ctx, compute.ComputeScope) if err != nil { //... } computeService, err := compute.New(client) if err != nil { //... } }
If you need a oauth2.TokenSource
, use the DefaultTokenSource
function:
ts, err := google.DefaultTokenSource(ctx, scope1, scope2, ...) if err != nil { //... } client := oauth2.NewClient(ctx, ts)
See also: golang.org/x/oauth2/google package documentation.