paperclip icon indicating copy to clipboard operation
paperclip copied to clipboard

How to add actix plugin to API operations implemented with trait?

Open Nutomic opened this issue 4 years ago • 1 comments

I am trying to add paperclip to Lemmy, but that isnt working because paperclip seems to expect that API handlers are implemented as simple methods. But our API is handlers are implementations of a Perform trait, like this:

### Generic trait for all API calls
#[async_trait::async_trait(?Send)]
pub trait Perform {
  type Response: Serialize + Send;

  async fn perform(
    &self,
    context: &Data<LemmyContext>,
    websocket_id: Option<ConnectionId>,
  ) -> Result<Self::Response, LemmyError>;
}

### Concrete implementation for a specific API call (ListCategories in this case)
#[async_trait::async_trait(?Send)]
impl Perform for ListCategories {
  type Response = ListCategoriesResponse;

  async fn perform(
    &self,
    context: &Data<LemmyContext>,
    _websocket_id: Option<ConnectionId>,
  ) -> Result<ListCategoriesResponse, LemmyError> {
    ...
  }
}

I tried by adding #[api_v2_operation] to perform() in various different places, but no matter what I do its always throwing lots of errors. Is there any way to make this work?

Nutomic avatar Nov 18 '20 14:11 Nutomic

As of now, paperclip uses unit structs to implement a trait (Apiv2Operation) which contains the auto-generated schema information. You're right that it's designed for function-like handlers, but we can make it work for method-like handlers too. I think it'll only require some minor fixes in the macro implementation, so that we can specify the #[api_v2_operation] on handler struct methods directly, which will then implement that trait for those methods.

Thanks for taking the time and trying it out! I'll keep this in mind. :smile:

wafflespeanut avatar Nov 27 '20 14:11 wafflespeanut