@@ -57,6 +57,16 @@ impl Client {
5757 }
5858 }
5959
60+ pub fn build_request ( & self , request : minreq:: Request ) -> minreq:: Request {
61+ let mut request = request
62+ . with_header ( "Content-Type" , "application/json" )
63+ . with_header ( "Authorization" , format ! ( "Bearer {}" , self . api_key) ) ;
64+ if let Some ( organization) = & self . organization {
65+ request = request. with_header ( "openai-organization" , organization) ;
66+ }
67+ request
68+ }
69+
6070 pub fn post < T : serde:: ser:: Serialize > (
6171 & self ,
6272 path : & str ,
@@ -68,16 +78,8 @@ impl Client {
6878 path = path
6979 ) ;
7080
71- let mut request = minreq:: post ( url)
72- . with_header ( "Content-Type" , "application/json" )
73- . with_header ( "Authorization" , format ! ( "Bearer {}" , self . api_key) ) ;
74-
75- if let Some ( organization) = & self . organization {
76- request = request. with_header ( "openai-organization" , organization) ;
77- }
78-
81+ let request = self . build_request ( minreq:: post ( url) ) ;
7982 let res = request. with_json ( params) . unwrap ( ) . send ( ) ;
80-
8183 match res {
8284 Ok ( res) => {
8385 if ( 200 ..=299 ) . contains ( & res. status_code ) {
@@ -99,16 +101,8 @@ impl Client {
99101 path = path
100102 ) ;
101103
102- let mut request = minreq:: get ( url)
103- . with_header ( "Content-Type" , "application/json" )
104- . with_header ( "Authorization" , format ! ( "Bearer {}" , self . api_key) ) ;
105-
106- if let Some ( organization) = & self . organization {
107- request = request. with_header ( "openai-organization" , organization) ;
108- }
109-
104+ let request = self . build_request ( minreq:: get ( url) ) ;
110105 let res = request. send ( ) ;
111-
112106 match res {
113107 Ok ( res) => {
114108 if ( 200 ..=299 ) . contains ( & res. status_code ) {
@@ -130,16 +124,8 @@ impl Client {
130124 path = path
131125 ) ;
132126
133- let mut request = minreq:: delete ( url)
134- . with_header ( "Content-Type" , "application/json" )
135- . with_header ( "Authorization" , format ! ( "Bearer {}" , self . api_key) ) ;
136-
137- if let Some ( organization) = & self . organization {
138- request = request. with_header ( "openai-organization" , organization) ;
139- }
140-
127+ let request = self . build_request ( minreq:: delete ( url) ) ;
141128 let res = request. send ( ) ;
142-
143129 match res {
144130 Ok ( res) => {
145131 if ( 200 ..=299 ) . contains ( & res. status_code ) {
0 commit comments