@@ -4,17 +4,17 @@ use std::{
44 sync:: { Arc , LazyLock } ,
55} ;
66
7- use crate :: { Chat , Error , Result , chat, types} ;
87use futures:: FutureExt as _;
98use reqwest:: Method ;
109use secrecy:: { ExposeSecret as _, SecretString } ;
11- use serde:: Serialize ;
1210
13- const BASE_URI : & str = "https://generativelanguage.googleapis.com" ;
11+ use crate :: { Chat , Error , Result , StreamGenerateContent , chat, types} ;
12+
13+ pub ( crate ) const BASE_URI : & str = "https://generativelanguage.googleapis.com" ;
1414
1515pub struct Route < T > {
16- client : Client ,
17- kind : T ,
16+ pub ( crate ) client : Client ,
17+ pub ( crate ) kind : T ,
1818}
1919
2020impl < T > Route < T > {
@@ -38,16 +38,11 @@ impl<T: Request> IntoFuture for Route<T> {
3838 . request ( T :: METHOD , format ! ( "{BASE_URI}/{self}" ) ) ;
3939
4040 if let Some ( body) = self . kind . body ( ) {
41- // Debug print the request body
42- if let Ok ( body_json) = serde_json:: to_string_pretty ( & body) {
43- println ! ( "Request body: {body_json}" ) ;
44- }
4541 request = request. json ( & body) ;
4642 } ;
4743
4844 let response = request. send ( ) . await ?;
4945 let raw_json = response. text ( ) . await ?;
50- println ! ( "Response: {raw_json}" ) ;
5146
5247 match serde_json:: from_str :: < types:: ApiResponse < T :: Model > > ( & raw_json) ? {
5348 types:: ApiResponse :: Ok ( response) => Ok ( response) ,
@@ -58,15 +53,15 @@ impl<T: Request> IntoFuture for Route<T> {
5853 }
5954}
6055
61- impl < T > Deref for Route < T > {
62- type Target = T ;
56+ impl Deref for Route < GenerateContent > {
57+ type Target = GenerateContent ;
6358
6459 fn deref ( & self ) -> & Self :: Target {
6560 & self . kind
6661 }
6762}
6863
69- impl < T > DerefMut for Route < T > {
64+ impl DerefMut for Route < GenerateContent > {
7065 fn deref_mut ( & mut self ) -> & mut Self :: Target {
7166 & mut self . kind
7267 }
@@ -83,7 +78,7 @@ impl<T: Request> std::fmt::Display for Route<T> {
8378/// Covers the 20% of use cases that [Chat] doesn't
8479#[ derive( Clone ) ]
8580pub struct Client {
86- inner : Arc < ClientInner > ,
81+ pub ( crate ) inner : Arc < ClientInner > ,
8782}
8883
8984impl Deref for Client {
@@ -121,14 +116,18 @@ impl Client {
121116 Route :: new ( self , GenerateContent :: new ( model. into ( ) ) )
122117 }
123118
119+ pub fn stream_generate_content ( & self , model : & str ) -> Route < StreamGenerateContent > {
120+ Route :: new ( self , StreamGenerateContent :: new ( model) )
121+ }
122+
124123 pub fn instance ( ) -> Client {
125124 static STATIC_INSTANCE : LazyLock < Client > = LazyLock :: new ( Client :: default) ;
126125 STATIC_INSTANCE . clone ( )
127126 }
128127}
129128
130129pub struct GenerateContent {
131- model : Box < str > ,
130+ pub ( crate ) model : Box < str > ,
132131 pub body : types:: GenerateContent ,
133132}
134133
@@ -186,8 +185,8 @@ impl Request for GenerateContent {
186185 fmt. write_str ( ":generateContent" )
187186 }
188187
189- fn body ( self ) -> Option < Self :: Body > {
190- Some ( self . body )
188+ fn body ( & self ) -> Option < Self :: Body > {
189+ Some ( self . body . clone ( ) )
191190 }
192191}
193192
@@ -241,14 +240,18 @@ impl DerefMut for Formatter<'_, '_> {
241240}
242241
243242impl < ' me , ' buffer > Formatter < ' me , ' buffer > {
244- fn new ( formatter : & ' me mut std:: fmt:: Formatter < ' buffer > ) -> Self {
243+ pub ( crate ) fn new ( formatter : & ' me mut std:: fmt:: Formatter < ' buffer > ) -> Self {
245244 Self {
246245 formatter,
247246 is_first : true ,
248247 }
249248 }
250249
251- fn write_query_param ( & mut self , key : & str , value : & impl std:: fmt:: Display ) -> std:: fmt:: Result {
250+ pub ( crate ) fn write_query_param (
251+ & mut self ,
252+ key : & str ,
253+ value : & impl std:: fmt:: Display ,
254+ ) -> std:: fmt:: Result {
252255 if self . is_first {
253256 self . formatter . write_char ( '?' ) ?;
254257 self . is_first = false ;
@@ -275,7 +278,7 @@ impl<'me, 'buffer> Formatter<'me, 'buffer> {
275278}
276279
277280pub struct ClientInner {
278- reqwest : reqwest:: Client ,
281+ pub ( crate ) reqwest : reqwest:: Client ,
279282 key : SecretString ,
280283}
281284
@@ -299,7 +302,7 @@ pub trait Request: Send + Sized + 'static {
299302
300303 fn format_uri ( & self , fmt : & mut Formatter < ' _ , ' _ > ) -> std:: fmt:: Result ;
301304
302- fn body ( self ) -> Option < Self :: Body > {
305+ fn body ( & self ) -> Option < Self :: Body > {
303306 None
304307 }
305308}
0 commit comments