File tree Expand file tree Collapse file tree 7 files changed +19
-12
lines changed
Expand file tree Collapse file tree 7 files changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ use std::collections::HashMap;
55
66/// Fetch and return the account information for the given API key.
77pub async fn run ( api_key : & str ) -> Result < Value , CliError > {
8- let client = make_client ( api_key) ?;
8+ let client = make_client ( Some ( api_key) ) ?;
99 let result = client. account ( HashMap :: new ( ) ) . await . map_err ( network_err) ?;
1010 check_api_error ( result)
1111}
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ use serde_json::Value;
44
55/// Retrieve a previously cached search result from the SerpApi archive by its ID.
66pub async fn run ( id : & str , api_key : & str ) -> Result < Value , CliError > {
7- let client = make_client ( api_key) ?;
7+ let client = make_client ( Some ( api_key) ) ?;
88 let result = client. search_archive ( id) . await . map_err ( network_err) ?;
99 check_api_error ( result)
1010}
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ use serde_json::Value;
77pub async fn run ( params : Vec < Param > ) -> Result < Value , CliError > {
88 let params_map = params:: params_to_hashmap ( params) ;
99 // Locations endpoint is public – no API key needed.
10- let client = make_client ( "" ) ?;
10+ let client = make_client ( None ) ?;
1111 let result = client. location ( params_map) . await . map_err ( network_err) ?;
1212 check_api_error ( result)
1313}
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ pub async fn run() -> Result<(), CliError> {
3434 } ) ;
3535 }
3636
37- let client = make_client ( api_key) ?;
37+ let client = make_client ( Some ( api_key) ) ?;
3838 let result = client. account ( HashMap :: new ( ) ) . await . map_err ( network_err) ?;
3939
4040 let result = check_api_error ( result) ?;
Original file line number Diff line number Diff line change @@ -20,9 +20,12 @@ pub(crate) fn network_err(e: Box<dyn std::error::Error>) -> CliError {
2020 }
2121}
2222
23- /// Build a `serpapi::Client` authenticated with the given API key.
24- pub fn make_client ( api_key : & str ) -> Result < Client , CliError > {
25- let params = HashMap :: from ( [ ( API_KEY_PARAM . to_string ( ) , api_key. to_string ( ) ) ] ) ;
23+ /// Build a `serpapi::Client`, optionally authenticated with an API key.
24+ pub fn make_client ( api_key : Option < & str > ) -> Result < Client , CliError > {
25+ let mut params = HashMap :: new ( ) ;
26+ if let Some ( key) = api_key {
27+ params. insert ( API_KEY_PARAM . to_string ( ) , key. to_string ( ) ) ;
28+ }
2629 Client :: new ( params) . map_err ( |e : Box < dyn std:: error:: Error > | CliError :: NetworkError {
2730 message : e. to_string ( ) ,
2831 } )
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ use url::Url;
99/// Execute a SerpApi search, optionally accumulating all pages into a single result.
1010pub async fn run (
1111 params : Vec < Param > ,
12- api_key : & str ,
12+ api_key : Option < & str > ,
1313 fields : Option < & str > ,
1414 all_pages : bool ,
1515 max_pages : Option < usize > ,
@@ -29,7 +29,9 @@ pub async fn run(
2929 }
3030
3131 // Ensure api_key is in the initial params map so it survives page transitions.
32- params_map. insert ( API_KEY_PARAM . to_string ( ) , api_key. to_string ( ) ) ;
32+ if let Some ( key) = api_key {
33+ params_map. insert ( API_KEY_PARAM . to_string ( ) , key. to_string ( ) ) ;
34+ }
3335
3436 let client = make_client ( api_key) ?;
3537 let mut current_params = params_map;
@@ -88,7 +90,9 @@ pub async fn run(
8890 break ;
8991 }
9092 let mut next_params = parse_next_params ( & url) ?;
91- next_params. insert ( API_KEY_PARAM . to_string ( ) , api_key. to_string ( ) ) ;
93+ if let Some ( key) = api_key {
94+ next_params. insert ( API_KEY_PARAM . to_string ( ) , key. to_string ( ) ) ;
95+ }
9296 let canonical = canonical_params_key ( & next_params) ;
9397 if !seen. insert ( canonical) {
9498 break ;
Original file line number Diff line number Diff line change @@ -70,15 +70,15 @@ async fn main() {
7070 all_pages,
7171 max_pages,
7272 } => {
73- let api_key = resolve_api_key ( ) . unwrap_or_else ( |e| die ( e ) ) ;
73+ let api_key = config :: resolve_api_key ( cli . api_key . as_deref ( ) ) . ok ( ) ;
7474 let parsed_params = params
7575 . iter ( )
7676 . map ( |s| s. parse :: < params:: Param > ( ) )
7777 . collect :: < Result < Vec < _ > , _ > > ( )
7878 . unwrap_or_else ( |e| die ( e) ) ;
7979 commands:: search:: run (
8080 parsed_params,
81- & api_key,
81+ api_key. as_deref ( ) ,
8282 cli. fields . as_deref ( ) ,
8383 all_pages,
8484 max_pages,
You can’t perform that action at this time.
0 commit comments