mirror of
https://github.com/NovaOSS/nova-python.git
synced 2024-11-25 14:33:58 +01:00
Made timeout longer and added optional seconds_until_timeout argument
This commit is contained in:
parent
b232fa533c
commit
4ca3608cd9
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "nova-python"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
|
@ -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.
|
||||
*Happy prompting!*
|
||||
|
||||
<br><br>
|
||||
## FAQ ##
|
||||
**Q:** I get an error when installing the package
|
||||
**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
|
|
@ -16,7 +16,8 @@ classifiers = [
|
|||
"Programming Language :: Python :: Implementation :: PyPy",
|
||||
]
|
||||
|
||||
repository = "https://github.com/NovaOSS/nova-python"
|
||||
[project.urls]
|
||||
"Repo" = "https://github.com/henceiusegentoo/nova-python"
|
||||
|
||||
[tool.maturin]
|
||||
features = ["pyo3/extension-module"]
|
14
src/lib.rs
14
src/lib.rs
|
@ -3,6 +3,7 @@ use std::collections::HashMap;
|
|||
use pyo3::types::PyDict;
|
||||
use std::time;
|
||||
|
||||
// An enum to represent the different models
|
||||
#[pyclass(module = "nova_python", frozen)]
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
|
@ -17,6 +18,7 @@ enum Models {
|
|||
ModerationStable,
|
||||
}
|
||||
|
||||
// An enum to represent the different endpoints
|
||||
#[pyclass(module = "nova_python", frozen)]
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone)]
|
||||
|
@ -27,6 +29,7 @@ enum Endpoints {
|
|||
Moderation,
|
||||
}
|
||||
|
||||
// Interface to the Nova API
|
||||
#[pyclass(module = "nova_python", frozen)]
|
||||
struct NovaClient {
|
||||
#[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) {
|
||||
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 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 client = reqwest::Client::builder()
|
||||
.timeout(time::Duration::from_secs(10))
|
||||
.timeout(time::Duration::from_secs(seconds_until_timeout))
|
||||
.user_agent("Mozilla/5.0")
|
||||
.build()
|
||||
.unwrap();
|
||||
|
|
Loading…
Reference in a new issue