-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a623c73
commit 621a00d
Showing
5 changed files
with
204 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use reqwest::{Client, Url}; | ||
use serde::{Serialize, Deserialize}; | ||
use tracing::error; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct AutoCompleteResponse { | ||
pub results: Vec<AutoCompletion> | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct AutoCompletion { | ||
pub term: String, | ||
pub preview: String | ||
} | ||
|
||
pub(crate) async fn fetch_autocomplete(query: &str) -> AutoCompleteResponse { | ||
let client = Client::new(); | ||
let url = Url::parse_with_params("https://api.urbandictionary.com/v0/autocomplete-extra", &[("term", query)]); | ||
match url { | ||
Ok(url) => { | ||
let data = client.get(url.clone()).header("User-Agent", "Moogan/0.2.47").send().await.unwrap().text().await.unwrap(); | ||
error!("Data: {}", data); | ||
error!("URL: {}", &url.as_str()); | ||
|
||
match client.get(url).header("User-Agent", "Moogan/0.2.47").send().await { | ||
Ok(response) => { | ||
match response.json::<AutoCompleteResponse>().await { | ||
Ok(data) => { | ||
data | ||
} | ||
Err(ex) => { | ||
error!("Failed to process autocomplete: {}", ex); | ||
AutoCompleteResponse { | ||
results: vec![] | ||
} | ||
} | ||
} | ||
} | ||
Err(ex) => { | ||
error!("Failed to get autocomplete: {}", ex); | ||
AutoCompleteResponse { | ||
results: vec![] | ||
} | ||
} | ||
} | ||
} | ||
Err(ex) => { | ||
// Silently fail | ||
error!("Failed to parse autocomplete URL: {}", ex); | ||
AutoCompleteResponse { | ||
results: vec![] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod ask; | ||
mod openai; | ||
mod openai_models; | ||
mod dictionary; | ||
|
||
use crate::{CowContext, Error}; | ||
use ask::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters