|
6 | 6 | use warnings; |
7 | 7 | use File::Temp qw/ tempfile tempdir /; |
8 | 8 | use File::Which; |
9 | | - |
| 9 | +use feature 'say'; |
10 | 10 |
|
11 | 11 |
|
12 | 12 | ## assumes gapi.json configuration in working directory with scoped project and user authorization |
|
31 | 31 |
|
32 | 32 | } |
33 | 33 |
|
| 34 | +if ( 0 ) |
| 35 | +{ |
| 36 | + my $text_to_speech_request_options = { |
| 37 | + 'input' => { |
| 38 | + 'text' => 'Using the Web-Services-Google-Client Perl module, it is now a simple matter to access all of the Google API Resources in a consistent manner. Nice work Peter!' |
| 39 | + }, |
| 40 | + 'voice' => { |
| 41 | + 'languageCode' => 'en-gb', |
| 42 | + 'name' => 'en-GB-Standard-A', |
| 43 | + 'ssmlGender' => 'FEMALE' |
| 44 | + }, |
| 45 | + 'audioConfig' => { |
| 46 | + 'audioEncoding'=> 'MP3' |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + ## Using this API requires authorised https://www.googleapis.com/auth/cloud-platform scope |
| 51 | + |
| 52 | + if ( 0 ) ## use a full manually constructed non validating standard user agent query builder approach ( includes auto O-Auth token handling ) |
| 53 | + { |
| 54 | + $r = $gapi_client->api_query( |
| 55 | + method => 'POST', |
| 56 | + path => 'https://texttospeech.googleapis.com/v1/text:synthesize', |
| 57 | + options => $text_to_speech_request_options |
| 58 | + ) ; |
34 | 59 |
|
35 | | -my $text_to_speech_request_options = { |
36 | | - 'input' => { |
37 | | - 'text' => 'Using the Web-Services-Google-Client Perl module, it is now a simple matter to access all of the Google API Resources in a consistent manner. Nice work Peter!' |
38 | | - }, |
39 | | - 'voice' => { |
40 | | - 'languageCode' => 'en-gb', |
41 | | - 'name' => 'en-GB-Standard-A', |
42 | | - 'ssmlGender' => 'FEMALE' |
43 | | - }, |
44 | | - 'audioConfig' => { |
45 | | - 'audioEncoding'=> 'MP3' |
46 | | - } |
47 | | - }; |
| 60 | + } |
| 61 | + else ## use the api end-point id and take full advantage of pre-submission validation etc |
| 62 | + { |
| 63 | + $r = $gapi_client->api_query( |
| 64 | + api_endpoint_id => 'texttospeech.text.synthesize', |
| 65 | + # method => 'POST', ## not required as determined from API SPEC |
| 66 | + # path => 'https://texttospeech.googleapis.com/v1/text:synthesize', ## not required as determined from API SPEC |
| 67 | + options => $text_to_speech_request_options |
| 68 | + ) ; |
| 69 | + ## NB - this approach will also autofill any defaults that aren't defined |
| 70 | + ## confirm that the user has the required scope before submitting to Google. |
| 71 | + ## confirms that all required fields are populated |
| 72 | + ## where an error is detected - result is a 418 code ( I'm a teapot ) with the body containing the error descriptions |
48 | 73 |
|
49 | | -## Using this API requires authorised https://www.googleapis.com/auth/cloud-platform scope |
| 74 | + } |
50 | 75 |
|
51 | | -if ( 0 ) ## use a full manually constructed non validating standard user agent query builder approach ( includes auto O-Auth token handling ) |
52 | | -{ |
53 | | - $r = $gapi_client->api_query( |
54 | | - method => 'POST', |
55 | | - path => 'https://texttospeech.googleapis.com/v1/text:synthesize', |
56 | | - options => $text_to_speech_request_options |
57 | | - ) ; |
| 76 | + if ( $r->is_success ) ## $r is a standard Mojo::Message::Response instance |
| 77 | + { |
| 78 | + my $returned_data = $r->json; ## convert from json to native hashref - result is a hashref with a key 'audioContent' containing synthesized audio in base64-encoded MP3 format |
| 79 | + my $decoded_mp3 = decode_base64( $returned_data->{audioContent} ); |
58 | 80 |
|
59 | | -} |
60 | | -else ## use the api end-point id and take full advantage of pre-submission validation etc |
61 | | -{ |
62 | | - $r = $gapi_client->api_query( |
63 | | - api_endpoint_id => 'texttospeech.text.synthesize', |
64 | | - # method => 'POST', ## not required as determined from API SPEC |
65 | | - # path => 'https://texttospeech.googleapis.com/v1/text:synthesize', ## not required as determined from API SPEC |
66 | | - options => $text_to_speech_request_options |
67 | | - ) ; |
68 | | - ## NB - this approach will also autofill any defaults that aren't defined |
69 | | - ## confirm that the user has the required scope before submitting to Google. |
70 | | - ## confirms that all required fields are populated |
71 | | - ## where an error is detected - result is a 418 code ( I'm a teapot ) with the body containing the error descriptions |
| 81 | + my $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.mp3' ); ## should prolly unlink=1 if not planning to use output file in future |
| 82 | + print $tmp $decoded_mp3; |
| 83 | + |
| 84 | + if ( which('ffplay') ) |
| 85 | + { |
| 86 | + print "ffplay -nodisp -autoexit $tmp\n"; |
| 87 | + `ffplay -nodisp -autoexit $tmp`; |
| 88 | + |
| 89 | + } |
| 90 | + close($tmp); |
72 | 91 |
|
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + if ( $r->code eq '418' ) |
| 96 | + { |
| 97 | + print qq{Cool - I'm a teapot - this was caught ebfore sending the request through to Google \n}; |
| 98 | + print $r->body; |
| 99 | + } |
| 100 | + else ## other error - should appear in warnings but can inspect $r for more detail |
| 101 | + { |
| 102 | + print Dumper $r; |
| 103 | + } |
| 104 | + |
| 105 | + } |
73 | 106 | } |
74 | 107 |
|
75 | | -if ( $r->is_success ) ## $r is a standard Mojo::Message::Response instance |
76 | | -{ |
77 | | - my $returned_data = $r->json; ## convert from json to native hashref - result is a hashref with a key 'audioContent' containing synthesized audio in base64-encoded MP3 format |
78 | | - my $decoded_mp3 = decode_base64( $returned_data->{audioContent} ); |
79 | | - |
80 | | - my $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.mp3' ); ## should prolly unlink=1 if not planning to use output file in future |
81 | | - print $tmp $decoded_mp3; |
82 | | - |
83 | | - if ( which('ffplay') ) |
84 | | - { |
85 | | - print "ffplay -nodisp -autoexit $tmp\n"; |
86 | | - `ffplay -nodisp -autoexit $tmp`; |
87 | | - |
88 | | - } |
89 | | - close($tmp); |
90 | 108 |
|
| 109 | +## edge cases to inform improvement to test coverage |
| 110 | + |
| 111 | +my $x = WebService::GoogleAPI::Client->new(); |
| 112 | +say " WebService::GoogleAPI::Client->new() is a " . ref( $x ); |
| 113 | +#exit; |
| 114 | +#my $y = $x->api_query(); |
| 115 | +#say Dumper $y; |
| 116 | +say "WebService::GoogleAPI::Client->new->api_query() is a " . ref( WebService::GoogleAPI::Client->new->api_query() ); # eq 'Mojo::Message::Response'; |
| 117 | + |
| 118 | +say "WebService::GoogleAPI::Client->new->has_scope_to_access_api_endpoint()" . WebService::GoogleAPI::Client->new->has_scope_to_access_api_endpoint(); |
| 119 | +use WebService::GoogleAPI::Client::Discovery; |
| 120 | + |
| 121 | +#say my $x = WebService::GoogleAPI::Client::Discovery->new->list_of_available_google_api_ids(); |
| 122 | +say 'fnarly' if ref ( WebService::GoogleAPI::Client::Discovery->new->discover_all() ) eq 'HASH'; |
| 123 | +#exit; |
| 124 | +if ( WebService::GoogleAPI::Client::Discovery->new->augment_discover_all_with_unlisted_experimental_api() ) |
| 125 | +{ |
| 126 | + #say Dumper [ WebService::GoogleAPI::Client::Discovery->new->augment_discover_all_with_unlisted_experimental_api() ]; |
91 | 127 | } |
92 | | -else |
| 128 | +else |
93 | 129 | { |
94 | | - if ( $r->code eq '418' ) |
95 | | - { |
96 | | - print qq{Cool - I'm a teapot - this was caught ebfore sending the request through to Google \n}; |
97 | | - print $r->body; |
98 | | - } |
99 | | - else ## other error - should appear in warnings but can inspect $r for more detail |
100 | | - { |
101 | | - print Dumper $r; |
102 | | - } |
103 | | - |
| 130 | + #say Dumper WebService::GoogleAPI::Client::Discovery->new->augment_discover_all_with_unlisted_experimental_api(); |
104 | 131 | } |
105 | 132 |
|
| 133 | +say length( WebService::GoogleAPI::Client::Discovery->new->supported_as_text ) > 100 ; |
| 134 | +#say Dumper $x; |
| 135 | + |
| 136 | +say WebService::GoogleAPI::Client::Discovery->new->api_verson_urls; |
| 137 | +exit; |
| 138 | +my $f = WebService::GoogleAPI::Client::AuthStorage->new;#->get_credentials_for_refresh(); |
| 139 | +print Dumper $f; |
| 140 | +my $dd = $f->get_credentials_for_refresh(); |
| 141 | + |
106 | 142 |
|
107 | | -# |
| 143 | +#say WebService::GoogleAPI::Client::Discovery->new->augment_discover_all_with_unlisted_experimental_api(); |
| 144 | +#say join(',', WebService::GoogleAPI::Client->new->list_of_available_google_api_ids() ) . ' as list'; |
| 145 | +#say WebService::GoogleAPI::Client->new->list_of_available_google_api_ids() . ' as scalar'; |
0 commit comments