From f5e8e872b08d2aa9b3c063b0a98c59f842f94ecc Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 27 Jul 2024 21:33:04 +0800 Subject: feat: support patching request url, headers and body (#756) --- src/client/cloudflare.rs | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) (limited to 'src/client/cloudflare.rs') diff --git a/src/client/cloudflare.rs b/src/client/cloudflare.rs index 439abac..3ee0a91 100644 --- a/src/client/cloudflare.rs +++ b/src/client/cloudflare.rs @@ -1,7 +1,7 @@ use super::*; use anyhow::{anyhow, Context, Result}; -use reqwest::{Client as ReqwestClient, RequestBuilder}; +use reqwest::RequestBuilder; use serde::Deserialize; use serde_json::{json, Value}; @@ -14,7 +14,7 @@ pub struct CloudflareConfig { pub api_key: Option, #[serde(default)] pub models: Vec, - pub patch: Option, + pub patch: Option, pub extra: Option, } @@ -27,51 +27,42 @@ impl CloudflareClient { ("api_key", "API Key:", true, PromptKind::String), ]; - fn chat_completions_builder( - &self, - client: &ReqwestClient, - data: ChatCompletionsData, - ) -> Result { + fn prepare_chat_completions(&self, data: ChatCompletionsData) -> Result { let account_id = self.get_account_id()?; let api_key = self.get_api_key()?; - let mut body = build_chat_completions_body(data, &self.model)?; - self.patch_chat_completions_body(&mut body); - let url = format!( "{API_BASE}/accounts/{account_id}/ai/run/{}", self.model.name() ); - debug!("Cloudflare Chat Completions Request: {url} {body}"); + let body = build_chat_completions_body(data, &self.model)?; + + let mut request_data = RequestData::new(url, body); - let builder = client.post(url).bearer_auth(api_key).json(&body); + request_data.bearer_auth(api_key); - Ok(builder) + Ok(request_data) } - fn embeddings_builder( - &self, - client: &ReqwestClient, - data: EmbeddingsData, - ) -> Result { + fn prepare_embeddings(&self, data: EmbeddingsData) -> Result { let account_id = self.get_account_id()?; let api_key = self.get_api_key()?; - let body = json!({ - "text": data.texts, - }); - let url = format!( "{API_BASE}/accounts/{account_id}/ai/run/{}", self.model.name() ); - debug!("Cloudflare Embeddings Request: {url} {body}"); + let body = json!({ + "text": data.texts, + }); + + let mut request_data = RequestData::new(url, body); - let builder = client.post(url).bearer_auth(api_key).json(&body); + request_data.bearer_auth(api_key); - Ok(builder) + Ok(request_data) } } -- cgit v1.2.3