forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocial.rs
More file actions
57 lines (52 loc) · 1.74 KB
/
Copy pathsocial.rs
File metadata and controls
57 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use echomirror_core::social;
use pyo3::prelude::*;
use pyo3::types::PyAny;
use crate::client::PyEchoMirrorClient;
use crate::errors::to_py_err;
use crate::types::{PyGlobalFeedEntry, PyLeaderboardEntry};
/// Global feed and leaderboard.
///
/// ```python
/// social = SocialClient(client)
/// feed = await social.get_global_feed(limit=20)
/// ```
#[pyclass(name = "SocialClient")]
#[derive(Clone)]
pub struct PySocialClient {
client: PyEchoMirrorClient,
}
#[pymethods]
impl PySocialClient {
#[new]
pub fn new(client: PyEchoMirrorClient) -> Self {
Self { client }
}
/// Get the global mood feed — anonymized entries from across the EchoMirror network.
#[pyo3(signature = (limit=50))]
fn get_global_feed<'py>(&self, py: Python<'py>, limit: u32) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let entries = social::get_global_feed(&inner, limit)
.await
.map_err(to_py_err)?;
Ok(entries
.into_iter()
.map(PyGlobalFeedEntry::from)
.collect::<Vec<_>>())
})
}
/// Get the weekly leaderboard.
#[pyo3(signature = (limit=100))]
fn get_leaderboard<'py>(&self, py: Python<'py>, limit: u32) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let entries = social::get_leaderboard(&inner, limit)
.await
.map_err(to_py_err)?;
Ok(entries
.into_iter()
.map(PyLeaderboardEntry::from)
.collect::<Vec<_>>())
})
}
}