| 1 | package Google::Cloud::Spanner::V1::SpannerClient; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Google::gRPC::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Google::Cloud::REST::Client; | ||
| 8 | use Google::Auth; | ||
| 9 | use Carp qw(croak); | ||
| 10 | |||
| 11 | use Protobuf; | ||
| 12 | use Google::Api::Common; | ||
| 13 | use Google::Spanner::V1::ResultSet; | ||
| 14 | use Google::Spanner::V1::QueryPlan; | ||
| 15 | use Google::Spanner::V1::Spanner; | ||
| 16 | use Google::Spanner::V1::Mutation; | ||
| 17 | use Google::Spanner::V1::Type; | ||
| 18 | use Google::Spanner::V1::Transaction; | ||
| 19 | use Google::Spanner::V1::ChangeStream; | ||
| 20 | use Google::Spanner::V1::CommitResponse; | ||
| 21 | use Google::Spanner::V1::Location; | ||
| 22 | use Google::Spanner::V1::Keys; | ||
| 23 | |||
| 24 | our $VERSION = '0.03'; | ||
| 25 | |||
| 26 | has credentials => ( is => 'ro', required => 0 ); | ||
| 27 | has transport => ( is => 'rw' ); | ||
| 28 | |||
| 29 | sub BUILD { | ||
| 30 | my ($self) = @_; | ||
| 31 | |||
| 32 | # Resolve credentials: use passed credentials object if it implements get_token, or default to ADC | ||
| 33 | my $auth = $self->credentials; | ||
| 34 | if (!$auth || !eval { $auth->can('get_token') }) { | ||
| 35 | $auth = Google::Auth->default(); | ||
| 36 | } | ||
| 37 | my $token = $auth->get_token(); | ||
| 38 | |||
| 39 | my $target = 'localhost:50051'; | ||
| 40 | my $t = $self->transport || 'grpc'; | ||
| 41 | |||
| 42 | if (ref($t) && eval { $t->can('call') }) { | ||
| 43 | # Already a transport object | ||
| 44 | } elsif (lc($t) eq 'rest') { | ||
| 45 | my $client = Google::Cloud::REST::Client->new( | ||
| 46 | target => $target, | ||
| 47 | auth_token => $token, | ||
| 48 | ); | ||
| 49 | $self->transport($client); | ||
| 50 | } else { | ||
| 51 | # Default high-performance HTTP/2 gRPC client | ||
| 52 | my $client = Google::gRPC::Client->new( | ||
| 53 | target => $target, | ||
| 54 | auth_token => $token, | ||
| 55 | ); | ||
| 56 | $self->transport($client); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub create_session { | ||
| 61 | my ($self, %params) = @_; | ||
| 62 | |||
| 63 | my $request_class = 'Google::Spanner::V1::Spanner::CreateSessionRequest'; | ||
| 64 | my $request = eval { $request_class->new(\%params) } || eval { $request_class->new(%params) } || ($request_class->can('encode') ? $request_class->encode(\%params) : \%params); | ||
| 65 | |||
| 66 | my $response_class = 'Google::Spanner::V1::Spanner::Session'; | ||
| 67 | my $response = $self->transport->call({ | ||
| 68 | service => 'google.spanner.v1.Spanner', | ||
| 69 | method => 'CreateSession', | ||
| 70 | request => $request, | ||
| 71 | response_class => $response_class, | ||
| 72 | }); | ||
| 73 | |||
| 74 | return $response; | ||
| 75 | } | ||
| 76 | 1; # End of Google::Cloud::Spanner::V1::SpannerClient | ||
| 77 | |||
| 78 | __END__ | ||