Made timeout longer and added optional seconds_until_timeout argument

This commit is contained in:
henceiusegentoo 2023-08-07 13:19:30 +02:00
parent b232fa533c
commit 4ca3608cd9
4 changed files with 18 additions and 6 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "nova-python" name = "nova-python"
version = "0.1.1" version = "0.1.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -62,9 +62,12 @@ client.make_request(
If everything goes to plan, you'll receive a string containing JSON-Data, which you can then use in your project. If everything goes to plan, you'll receive a string containing JSON-Data, which you can then use in your project.
*Happy prompting!* *Happy prompting!*
<br><br>
## FAQ ## ## FAQ ##
**Q:** I get an error when installing the package **Q:** I get an error when installing the package
**A:** Make you sure, that you have <a href="https://rustup.rs/">Cargo</a> installed **A:** Make you sure, that you have <a href="https://rustup.rs/">Cargo</a> installed
**Q**: I keep getting `RuntimeError: error sending request for url (https://api.nova-oss.com/v1/moderations): operation timed out`
**A**: Try passing a higher value than 30 as `seconds_until_timeout` to `make_request`
Made with 🩸 by Leander Made with 🩸 by Leander

View file

@ -16,7 +16,8 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy", "Programming Language :: Python :: Implementation :: PyPy",
] ]
repository = "https://github.com/NovaOSS/nova-python" [project.urls]
"Repo" = "https://github.com/henceiusegentoo/nova-python"
[tool.maturin] [tool.maturin]
features = ["pyo3/extension-module"] features = ["pyo3/extension-module"]

View file

@ -3,6 +3,7 @@ use std::collections::HashMap;
use pyo3::types::PyDict; use pyo3::types::PyDict;
use std::time; use std::time;
// An enum to represent the different models
#[pyclass(module = "nova_python", frozen)] #[pyclass(module = "nova_python", frozen)]
#[derive(PartialEq)] #[derive(PartialEq)]
#[derive(Clone)] #[derive(Clone)]
@ -17,6 +18,7 @@ enum Models {
ModerationStable, ModerationStable,
} }
// An enum to represent the different endpoints
#[pyclass(module = "nova_python", frozen)] #[pyclass(module = "nova_python", frozen)]
#[derive(PartialEq)] #[derive(PartialEq)]
#[derive(Clone)] #[derive(Clone)]
@ -27,6 +29,7 @@ enum Endpoints {
Moderation, Moderation,
} }
// Interface to the Nova API
#[pyclass(module = "nova_python", frozen)] #[pyclass(module = "nova_python", frozen)]
struct NovaClient { struct NovaClient {
#[pyo3(get)] #[pyo3(get)]
@ -50,7 +53,8 @@ impl NovaClient {
}) })
} }
fn make_request(&self, endpoint: Endpoints, model: Models, data: Vec<Py<PyDict>>) -> PyResult<String> { // Used to make a request to the Nova API
fn make_request(&self, endpoint: Endpoints, model: Models, data: Vec<Py<PyDict>>, seconds_until_timeout: Option<String>) -> PyResult<String> {
if !model_is_compatible(&endpoint, &model) { if !model_is_compatible(&endpoint, &model) {
return Err(NovaClient::get_endpoint_not_compatible_error()); return Err(NovaClient::get_endpoint_not_compatible_error());
} }
@ -59,10 +63,14 @@ impl NovaClient {
let request_body = self.get_request_body(&endpoint, &model, data).unwrap(); let request_body = self.get_request_body(&endpoint, &model, data).unwrap();
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
let seconds_until_timeout = match seconds_until_timeout {
Some(seconds_until_timeout) => seconds_until_timeout.parse::<u64>().unwrap(),
None => 30
};
let response: Result<String, reqwest::Error> = rt.block_on(async { let response: Result<String, reqwest::Error> = rt.block_on(async {
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
.timeout(time::Duration::from_secs(10)) .timeout(time::Duration::from_secs(seconds_until_timeout))
.user_agent("Mozilla/5.0") .user_agent("Mozilla/5.0")
.build() .build()
.unwrap(); .unwrap();