Skip to content

Commit

Permalink
Add server::Handler::get_indicator
Browse files Browse the repository at this point in the history
The corresponding client API is also added.
  • Loading branch information
msk committed Jan 25, 2025
1 parent ca98a42 commit b8f1cdf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- The following client-side API to send a request to the server:
- `Connection::get_indicator`

### Changed

- `server::Handler` handles requests from the following client-side APIs:
- `Connection::get_allow_list`
- `Connection::get_block_list`
- `Connection::get_indicator`

## [0.9.0] - 2025-01-20

Expand Down
13 changes: 12 additions & 1 deletion src/client/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io;
use std::{collections::HashSet, io};

use serde::{de::DeserializeOwned, Serialize};

Expand Down Expand Up @@ -57,6 +57,17 @@ impl Connection {
.and_then(|res| res.ok_or_else(|| io::Error::from(io::ErrorKind::NotFound)))
}

/// Fetches an indicator from the server.
///
/// # Errors
///
/// Returns an error if the request fails or the response is invalid.
pub async fn get_indicator(&self, name: &str) -> io::Result<HashSet<Vec<String>>> {
let res: Result<HashSet<Vec<String>>, String> =
request(self, server::RequestCode::GetIndicator, name).await?;
res.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}

/// Fetches the list of internal networks from the server.
///
/// # Errors
Expand Down
11 changes: 10 additions & 1 deletion src/server/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Requset handler for the server.
use std::io;
use std::{collections::HashSet, io};

use num_enum::FromPrimitive;
use oinq::request::parse_args;
Expand All @@ -26,6 +26,10 @@ pub trait Handler {
Err("not supported".to_string())
}

async fn get_indicator(&self, _name: &str) -> Result<HashSet<Vec<String>>, String> {
Err("not supported".to_string())
}

async fn get_tidb_patterns(
&self,
_db_names: &[(&str, &str)],
Expand Down Expand Up @@ -77,6 +81,11 @@ where
let result = handler.get_data_source(&data_source_key).await;
oinq::request::send_response(send, &mut buf, result).await?;
}
RequestCode::GetIndicator => {
let name = parse_args::<String>(body)?;
let result = handler.get_indicator(&name).await;
oinq::request::send_response(send, &mut buf, result).await?;
}
RequestCode::GetTidbPatterns => {
let db_names = parse_args::<Vec<(&str, &str)>>(body)?;
let result = handler.get_tidb_patterns(&db_names).await;
Expand Down

0 comments on commit b8f1cdf

Please sign in to comment.