Use ruma-api-macros for the media endpoints.

This commit is contained in:
Jimmy Cuadra 2017-07-01 15:39:45 -07:00
parent fb90929aa1
commit e0bb81ad67
3 changed files with 82 additions and 155 deletions

View File

@ -19,11 +19,13 @@ ruma-signatures = "0.4.0"
serde = "1.0.2"
serde_derive = "1.0.2"
serde_json = "1.0.1"
serde_urlencoded = "0.5.1"
url = "1.5.1"
[dependencies.ruma-api]
git = "https://github.com/ruma/ruma-api"
rev = "211cf5e3531cd1862136129d5f73caaac7b4eb43"
rev = "4893be93f86543f9a9e3c5d7205afba769b84aa6"
[dependencies.ruma-api-macros]
git = "https://github.com/ruma/ruma-api-macros"
rev = "f5a935384e36134daf86a13190318a5c50cf2c17"
rev = "84562c426044eef6b1c2f0e964e85ff9d100aa27"

View File

@ -16,6 +16,8 @@ extern crate ruma_signatures;
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
extern crate serde_urlencoded;
extern crate url;
/// Endpoints for the r0.x.x versions of the client API specification.
pub mod r0 {
@ -26,7 +28,7 @@ pub mod r0 {
pub mod context;
pub mod directory;
pub mod filter;
// pub mod media;
pub mod media;
// pub mod membership;
// pub mod presence;
// pub mod profile;

View File

@ -2,117 +2,73 @@
/// [GET /_matrix/media/r0/download/{serverName}/{mediaId}](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-media-r0-download-servername-mediaid)
pub mod get_content {
/// Details about this API endpoint.
#[derive(Clone, Copy, Debug)]
pub struct Endpoint;
use hyper::header::{ContentDisposition, ContentType};
use ruma_api_macros::ruma_api;
/// This API endpoint's path parameters.
#[derive(Clone, Debug)]
pub struct PathParams {
/// The media ID from the mxc:// URI (the path component).
pub media_id: String,
/// The server name from the mxc:// URI (the authoritory component).
pub server_name: String,
}
impl ::Endpoint for Endpoint {
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = ();
type Response = (); // TODO: How should a file be represented as a response?
// must include HTTP headers Content-Type and Content-Disposition (filename)
fn method() -> ::Method {
::Method::Get
ruma_api! {
metadata {
description: "Retrieve content from the media store.",
method: Method::Get,
name: "get_media_content",
path: "/_matrix/media/r0/download/:server_name/:media_id",
rate_limited: false,
requires_authentication: false,
}
fn request_path(params: Self::PathParams) -> String {
format!(
"/_matrix/media/r0/download/{}/{}",
params.server_name,
params.media_id
)
request {
/// The media ID from the mxc:// URI (the path component).
#[ruma_api(path)]
pub media_id: String,
/// The server name from the mxc:// URI (the authoritory component).
#[ruma_api(path)]
pub server_name: String,
}
fn router_path() -> &'static str {
"/_matrix/media/r0/download/:server_name/:media_id"
}
fn name() -> &'static str {
"get_media_content"
}
fn description() -> &'static str {
"Retrieve content from the media store."
}
fn requires_authentication() -> bool {
false
}
fn rate_limited() -> bool {
false
response {
/// The content that was previously uploaded.
#[ruma_api(body)]
pub file: Vec<u8>,
/// The content type of the file that was previously uploaded.
#[ruma_api(header)]
pub content_type: ContentType,
/// The name of the file that was previously uploaded, if set.
#[ruma_api(header)]
pub content_disposition: ContentDisposition,
}
}
}
/// [POST /_matrix/media/r0/upload](https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-media-r0-upload)
pub mod create_content {
/// Details about this API endpoint.
#[derive(Clone, Copy, Debug)]
pub struct Endpoint;
use hyper::header::ContentType;
use ruma_api_macros::ruma_api;
/// This API endpoint's response.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
/// The MXC URI for the uploaded content.
pub content_uri: String,
}
impl ::Endpoint for Endpoint {
type BodyParams = (); // TODO: How should a file be represented as the request body?
type PathParams = ();
type QueryParams = ();
type Response = Response;
fn method() -> ::Method {
::Method::Post
ruma_api! {
metadata {
description: "Upload content to the media store.",
method: Method::Post,
name: "create_media_content",
path: "/_matrix/media/r0/upload",
rate_limited: false,
requires_authentication: false,
}
fn request_path(_params: Self::PathParams) -> String {
Self::router_path().to_string()
request {
/// The content type of the file being uploaded.
#[ruma_api(header)]
pub content_type: ContentType,
}
fn router_path() -> &'static str {
"/_matrix/media/r0/upload"
}
fn name() -> &'static str {
"create_media_content"
}
fn description() -> &'static str {
"Upload content to the media store."
}
fn requires_authentication() -> bool {
// TODO: How comes this does not require authentication?
false
}
fn rate_limited() -> bool {
false
response {
/// The MXC URI for the uploaded content.
pub content_uri: String,
}
}
}
/// [GET /_matrix/media/r0/thumbnail/{serverName}/{mediaId}](https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-media-r0-thumbnail-servername-mediaid)
pub mod get_content_thumbnail {
use std::fmt::{Display, Error as FmtError, Formatter};
/// Details about this API endpoint.
#[derive(Clone, Copy, Debug)]
pub struct Endpoint;
use ruma_api_macros::ruma_api;
/// The desired resizing method.
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
@ -125,73 +81,40 @@ pub mod get_content_thumbnail {
Scale,
}
/// This API endpoint's path parameters.
#[derive(Clone, Debug)]
pub struct PathParams {
/// The media ID from the mxc:// URI (the path component).
pub media_id: String,
/// The server name from the mxc:// URI (the authoritory component).
pub server_name: String,
}
/// This API endpoint's query string parameters.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct QueryParams {
/// The *desired* height of the thumbnail. The actual thumbnail may not match the size
/// specified.
pub height: Option<u64>,
/// The desired resizing method.
pub method: Option<Method>,
/// The *desired* width of the thumbnail. The actual thumbnail may not match the size
/// specified.
pub width: Option<u64>,
}
impl ::Endpoint for Endpoint {
type BodyParams = ();
type PathParams = PathParams;
type QueryParams = QueryParams;
type Response = (); // TODO: How should a file be represented as a response?
fn method() -> ::Method {
::Method::Post
ruma_api! {
metadata {
description: "Get a thumbnail of content from the media store.",
method: Method::Get,
name: "get_content_thumbnail",
path: "/_matrix/media/r0/thumbnail/:server_name/:media_id",
rate_limited: false,
requires_authentication: false,
}
fn request_path(params: Self::PathParams) -> String {
format!(
"/_matrix/media/r0/thumbnail/{}/{}",
params.server_name,
params.media_id
)
request {
/// The media ID from the mxc:// URI (the path component).
#[ruma_api(path)]
pub media_id: String,
/// The server name from the mxc:// URI (the authoritory component).
#[ruma_api(path)]
pub server_name: String,
/// The *desired* height of the thumbnail. The actual thumbnail may not match the size
/// specified.
#[ruma_api(query)]
pub height: Option<u64>,
/// The desired resizing method.
#[ruma_api(query)]
pub method: Option<Method>,
/// The *desired* width of the thumbnail. The actual thumbnail may not match the size
/// specified.
#[ruma_api(query)]
pub width: Option<u64>,
}
fn router_path() -> &'static str {
"/_matrix/media/r0/thumbnail/:server_name/:media_id"
}
fn name() -> &'static str {
"get_content_thumbnail"
}
fn description() -> &'static str {
"Get a thumbnail of content from the media store."
}
fn requires_authentication() -> bool {
false
}
fn rate_limited() -> bool {
false
}
}
impl Display for Method {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
match *self {
Method::Crop => write!(f, "crop"),
Method::Scale => write!(f, "scale"),
}
response {
/// A thumbnail of the requested content.
#[ruma_api(body)]
pub file: Vec<u8>,
}
}
}