go.mercari.io/datastore@v1.8.2/option.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package option contains options for Google API clients. 16 17 package datastore 18 19 import ( 20 "net/http" 21 22 "go.mercari.io/datastore/internal" 23 "golang.org/x/oauth2" 24 "google.golang.org/grpc" 25 ) 26 27 // A ClientOption is an option for a Datastore client. 28 type ClientOption interface { 29 Apply(*internal.ClientSettings) 30 } 31 32 // WithProjectID returns a ClientOption that specifies ProjectID to be used in client. 33 func WithProjectID(projectID string) ClientOption { 34 return withProjectID{projectID} 35 } 36 37 type withProjectID struct{ s string } 38 39 func (w withProjectID) Apply(o *internal.ClientSettings) { 40 o.ProjectID = w.s 41 } 42 43 // WithTokenSource returns a ClientOption that specifies an OAuth2 token 44 // source to be used as the basis for authentication. 45 func WithTokenSource(s oauth2.TokenSource) ClientOption { 46 return withTokenSource{s} 47 } 48 49 type withTokenSource struct{ ts oauth2.TokenSource } 50 51 func (w withTokenSource) Apply(o *internal.ClientSettings) { 52 o.TokenSource = w.ts 53 } 54 55 type withCredFile string 56 57 func (w withCredFile) Apply(o *internal.ClientSettings) { 58 o.CredentialsFile = string(w) 59 } 60 61 // WithCredentialsFile returns a ClientOption that authenticates 62 // API calls with the given service account or refresh token JSON 63 // credentials file. 64 func WithCredentialsFile(filename string) ClientOption { 65 return withCredFile(filename) 66 } 67 68 // WithScopes returns a ClientOption that overrides the default OAuth2 scopes 69 // to be used for a service. 70 func WithScopes(scope ...string) ClientOption { 71 return withScopes(scope) 72 } 73 74 type withScopes []string 75 76 func (w withScopes) Apply(o *internal.ClientSettings) { 77 s := make([]string, len(w)) 78 copy(s, w) 79 o.Scopes = s 80 } 81 82 // WithHTTPClient returns a ClientOption that specifies the HTTP client to use 83 // as the basis of communications. This option may only be used with services 84 // that support HTTP as their communication transport. When used, the 85 // WithHTTPClient option takes precedent over all other supplied options. 86 func WithHTTPClient(client *http.Client) ClientOption { 87 return withHTTPClient{client} 88 } 89 90 type withHTTPClient struct{ client *http.Client } 91 92 func (w withHTTPClient) Apply(o *internal.ClientSettings) { 93 o.HTTPClient = w.client 94 } 95 96 // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption 97 // to an underlying gRPC dial. It does not work with WithGRPCConn. 98 func WithGRPCDialOption(opt grpc.DialOption) ClientOption { 99 return withGRPCDialOption{opt} 100 } 101 102 type withGRPCDialOption struct{ opt grpc.DialOption } 103 104 func (w withGRPCDialOption) Apply(o *internal.ClientSettings) { 105 o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt) 106 }