Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move get_basin_config, reconfigure_basin to AccountService #17

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use s2::{
service_error::{CreateBasinError, CreateStreamError, ServiceError},
types::{
CreateBasinRequest, CreateStreamRequest, DeleteBasinRequest, DeleteStreamRequest,
GetStreamConfigRequest, ListBasinsRequest, ListStreamsRequest,
GetBasinConfigRequest, GetStreamConfigRequest, ListBasinsRequest, ListStreamsRequest,
},
};

Expand Down Expand Up @@ -50,9 +50,8 @@ async fn main() {
Err(err) => exit_with_err(err),
};

let basin_client = client.basin_client(basin).await.unwrap();

match basin_client.get_basin_config().await {
let get_basin_config_req = GetBasinConfigRequest::builder().basin(basin).build();
match client.get_basin_config(get_basin_config_req).await {
Ok(config) => {
println!("Basin config: {config:#?}");
}
Expand All @@ -62,6 +61,7 @@ async fn main() {
let stream = "s2-sdk-example-stream";

let create_stream_req = CreateStreamRequest::builder().stream(stream).build();
let basin_client = client.basin_client(basin).await.unwrap();

match basin_client.create_stream(create_stream_req).await {
Ok(()) => {
Expand Down
2 changes: 1 addition & 1 deletion proto
Submodule proto updated from d9fca0 to d59662
46 changes: 24 additions & 22 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ use crate::{
service::{
account::{
CreateBasinError, CreateBasinServiceRequest, DeleteBasinError,
DeleteBasinServiceRequest, ListBasinsError, ListBasinsServiceRequest,
DeleteBasinServiceRequest, GetBasinConfigError, GetBasinConfigServiceRequest,
ListBasinsError, ListBasinsServiceRequest, ReconfigureBasinError,
ReconfigureBasinServiceRequest,
},
basin::{
CreateStreamError, CreateStreamServiceRequest, DeleteStreamError,
DeleteStreamServiceRequest, GetBasinConfigError, GetBasinConfigServiceRequest,
GetStreamConfigError, GetStreamConfigServiceRequest, ListStreamsError,
ListStreamsServiceRequest, ReconfigureBasinError, ReconfigureBasinServiceRequest,
ReconfigureStreamError, ReconfigureStreamServiceRequest,
DeleteStreamServiceRequest, GetStreamConfigError, GetStreamConfigServiceRequest,
ListStreamsError, ListStreamsServiceRequest, ReconfigureStreamError,
ReconfigureStreamServiceRequest,
},
send_request,
stream::{GetNextSeqNumError, GetNextSeqNumServiceRequest},
Expand Down Expand Up @@ -146,28 +147,15 @@ impl Client {
res => res,
}
}
}

#[derive(Debug, Clone)]
pub struct BasinClient {
inner: ClientInner,
}

impl BasinClient {
pub fn stream_client(&self, stream: impl Into<String>) -> StreamClient {
StreamClient {
inner: self.inner.clone(),
stream: stream.into(),
}
}

pub async fn get_basin_config(
&self,
req: types::GetBasinConfigRequest,
) -> Result<types::GetBasinConfigResponse, ServiceError<GetBasinConfigError>> {
self.inner
.send(
GetBasinConfigServiceRequest::new(self.inner.basin_service_client()),
/* request = */ (),
GetBasinConfigServiceRequest::new(self.inner.account_service_client()),
req,
)
.await
}
Expand All @@ -178,11 +166,25 @@ impl BasinClient {
) -> Result<(), ServiceError<ReconfigureBasinError>> {
self.inner
.send(
ReconfigureBasinServiceRequest::new(self.inner.basin_service_client()),
ReconfigureBasinServiceRequest::new(self.inner.account_service_client()),
req,
)
.await
}
}

#[derive(Debug, Clone)]
pub struct BasinClient {
inner: ClientInner,
}

impl BasinClient {
pub fn stream_client(&self, stream: impl Into<String>) -> StreamClient {
StreamClient {
inner: self.inner.clone(),
stream: stream.into(),
}
}

pub async fn create_stream(
&self,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub use secrecy::SecretString;

pub mod service_error {
pub use crate::service::{
account::{CreateBasinError, DeleteBasinError},
basin::{CreateStreamError, GetBasinConfigError, GetStreamConfigError, ListStreamsError},
account::{CreateBasinError, DeleteBasinError, GetBasinConfigError},
basin::{CreateStreamError, GetStreamConfigError, ListStreamsError},
ServiceError,
};
}
129 changes: 129 additions & 0 deletions src/service/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,132 @@ pub enum DeleteBasinError {
#[error("Not found: {0}")]
NotFound(String),
}

#[derive(Debug, Clone)]
pub struct GetBasinConfigServiceRequest {
client: AccountServiceClient<Channel>,
}

impl GetBasinConfigServiceRequest {
pub fn new(client: AccountServiceClient<Channel>) -> Self {
Self { client }
}
}

impl ServiceRequest for GetBasinConfigServiceRequest {
type Request = types::GetBasinConfigRequest;
type ApiRequest = api::GetBasinConfigRequest;
type Response = types::GetBasinConfigResponse;
type ApiResponse = api::GetBasinConfigResponse;
type Error = GetBasinConfigError;

const IDEMPOTENCY_LEVEL: IdempotencyLevel = IdempotencyLevel::NoSideEffects;

fn prepare_request(
&self,
req: Self::Request,
) -> Result<tonic::Request<Self::ApiRequest>, types::ConvertError> {
let req: api::GetBasinConfigRequest = req.into();
Ok(req.into_request())
}

fn parse_response(
&self,
resp: tonic::Response<Self::ApiResponse>,
) -> Result<Self::Response, types::ConvertError> {
resp.into_inner().try_into()
}

fn parse_status(&self, status: &tonic::Status) -> Option<Self::Error> {
match status.code() {
tonic::Code::NotFound => {
Some(GetBasinConfigError::NotFound(status.message().to_string()))
}
_ => None,
}
}

async fn send(
&mut self,
req: tonic::Request<Self::ApiRequest>,
) -> Result<tonic::Response<Self::ApiResponse>, tonic::Status> {
self.client.get_basin_config(req).await
}

fn should_retry(&self, _err: &super::ServiceError<Self::Error>) -> bool {
false
}
}

#[derive(Debug, thiserror::Error)]
pub enum GetBasinConfigError {
#[error("Not found: {0}")]
NotFound(String),
}

#[derive(Debug, Clone)]
pub struct ReconfigureBasinServiceRequest {
client: AccountServiceClient<Channel>,
}

impl ReconfigureBasinServiceRequest {
pub fn new(client: AccountServiceClient<Channel>) -> Self {
Self { client }
}
}

impl ServiceRequest for ReconfigureBasinServiceRequest {
type Request = types::ReconfigureBasinRequest;
type ApiRequest = api::ReconfigureBasinRequest;
type Response = ();
type ApiResponse = api::ReconfigureBasinResponse;
type Error = ReconfigureBasinError;

const IDEMPOTENCY_LEVEL: IdempotencyLevel = IdempotencyLevel::Idempotent;

fn prepare_request(
&self,
req: Self::Request,
) -> Result<tonic::Request<Self::ApiRequest>, types::ConvertError> {
let req: api::ReconfigureBasinRequest = req.try_into()?;
Ok(req.into_request())
}

fn parse_response(
&self,
_resp: tonic::Response<Self::ApiResponse>,
) -> Result<Self::Response, types::ConvertError> {
Ok(())
}

fn parse_status(&self, status: &tonic::Status) -> Option<Self::Error> {
match status.code() {
tonic::Code::NotFound => Some(ReconfigureBasinError::NotFound(
status.message().to_string(),
)),
tonic::Code::InvalidArgument => Some(ReconfigureBasinError::InvalidArgument(
status.message().to_string(),
)),
_ => None,
}
}

async fn send(
&mut self,
req: tonic::Request<Self::ApiRequest>,
) -> Result<tonic::Response<Self::ApiResponse>, tonic::Status> {
self.client.reconfigure_basin(req).await
}

fn should_retry(&self, _err: &super::ServiceError<Self::Error>) -> bool {
false
}
}

#[derive(Debug, thiserror::Error)]
pub enum ReconfigureBasinError {
#[error("Not found: {0}")]
NotFound(String),
#[error("Invalid argument: {0}")]
InvalidArgument(String),
}
128 changes: 0 additions & 128 deletions src/service/basin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,67 +72,6 @@ pub enum ListStreamsError {
InvalidArgument(String),
}

#[derive(Debug, Clone)]
pub struct GetBasinConfigServiceRequest {
client: BasinServiceClient<Channel>,
}

impl GetBasinConfigServiceRequest {
pub fn new(client: BasinServiceClient<Channel>) -> Self {
Self { client }
}
}

impl ServiceRequest for GetBasinConfigServiceRequest {
type Request = ();
type ApiRequest = api::GetBasinConfigRequest;
type Response = types::GetBasinConfigResponse;
type ApiResponse = api::GetBasinConfigResponse;
type Error = GetBasinConfigError;

const IDEMPOTENCY_LEVEL: IdempotencyLevel = IdempotencyLevel::NoSideEffects;

fn prepare_request(
&self,
_req: Self::Request,
) -> Result<tonic::Request<Self::ApiRequest>, types::ConvertError> {
Ok(api::GetBasinConfigRequest {}.into_request())
}

fn parse_response(
&self,
resp: tonic::Response<Self::ApiResponse>,
) -> Result<Self::Response, types::ConvertError> {
resp.into_inner().try_into()
}

fn parse_status(&self, status: &tonic::Status) -> Option<Self::Error> {
match status.code() {
tonic::Code::NotFound => {
Some(GetBasinConfigError::NotFound(status.message().to_string()))
}
_ => None,
}
}

async fn send(
&mut self,
req: tonic::Request<Self::ApiRequest>,
) -> Result<tonic::Response<Self::ApiResponse>, tonic::Status> {
self.client.get_basin_config(req).await
}

fn should_retry(&self, _err: &super::ServiceError<Self::Error>) -> bool {
false
}
}

#[derive(Debug, thiserror::Error)]
pub enum GetBasinConfigError {
#[error("Not found: {0}")]
NotFound(String),
}

#[derive(Debug, Clone)]
pub struct GetStreamConfigServiceRequest {
client: BasinServiceClient<Channel>,
Expand Down Expand Up @@ -339,73 +278,6 @@ pub enum DeleteStreamError {
InvalidArgument(String),
}

#[derive(Debug, Clone)]
pub struct ReconfigureBasinServiceRequest {
client: BasinServiceClient<Channel>,
}

impl ReconfigureBasinServiceRequest {
pub fn new(client: BasinServiceClient<Channel>) -> Self {
Self { client }
}
}

impl ServiceRequest for ReconfigureBasinServiceRequest {
type Request = types::ReconfigureBasinRequest;
type ApiRequest = api::ReconfigureBasinRequest;
type Response = ();
type ApiResponse = api::ReconfigureBasinResponse;
type Error = ReconfigureBasinError;

const IDEMPOTENCY_LEVEL: IdempotencyLevel = IdempotencyLevel::IdempotencyUnknown;

fn prepare_request(
&self,
req: Self::Request,
) -> Result<tonic::Request<Self::ApiRequest>, types::ConvertError> {
let req: api::ReconfigureBasinRequest = req.try_into()?;
Ok(req.into_request())
}

fn parse_response(
&self,
_resp: tonic::Response<Self::ApiResponse>,
) -> Result<Self::Response, types::ConvertError> {
Ok(())
}

fn parse_status(&self, status: &tonic::Status) -> Option<Self::Error> {
match status.code() {
tonic::Code::NotFound => Some(ReconfigureBasinError::NotFound(
status.message().to_string(),
)),
tonic::Code::InvalidArgument => Some(ReconfigureBasinError::InvalidArgument(
status.message().to_string(),
)),
_ => None,
}
}

async fn send(
&mut self,
req: tonic::Request<Self::ApiRequest>,
) -> Result<tonic::Response<Self::ApiResponse>, tonic::Status> {
self.client.reconfigure_basin(req).await
}

fn should_retry(&self, _err: &super::ServiceError<Self::Error>) -> bool {
false
}
}

#[derive(Debug, thiserror::Error)]
pub enum ReconfigureBasinError {
#[error("Not found: {0}")]
NotFound(String),
#[error("Invalid argument: {0}")]
InvalidArgument(String),
}

#[derive(Debug, Clone)]
pub struct ReconfigureStreamServiceRequest {
client: BasinServiceClient<Channel>,
Expand Down
Loading