1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-05-09 16:10:42 +00:00

Custom Endpoints

This commit is contained in:
msrd0 2021-01-18 16:56:16 +00:00
parent 002cfb1b4d
commit 5261aa9931
28 changed files with 524 additions and 46 deletions

View file

@ -44,6 +44,56 @@
},
"openapi": "3.0.2",
"paths": {
"/custom": {
"patch": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"required": true
},
"responses": {
"204": {
"description": "No Content"
}
}
}
},
"/custom/read/{from}/with/{id}": {
"get": {
"parameters": [
{
"in": "path",
"name": "from",
"required": true,
"schema": {
"type": "string"
},
"style": "simple"
},
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"format": "int64",
"minimum": 0,
"type": "integer"
},
"style": "simple"
}
],
"responses": {
"204": {
"description": "No Content"
}
}
}
},
"/img/{id}": {
"get": {
"operationId": "getImage",

View file

@ -5,6 +5,7 @@ extern crate gotham_derive;
use chrono::{NaiveDate, NaiveDateTime};
use gotham::{
hyper::Method,
pipeline::{new_pipeline, single::single_pipeline},
router::builder::*,
test::TestServer
@ -81,6 +82,22 @@ fn search_secret(auth: AuthStatus, _query: SecretQuery) -> AuthSuccess<Secrets>
})
}
#[derive(Resource)]
#[resource(custom_read_with, custom_patch)]
struct CustomResource;
#[derive(Deserialize, OpenapiType, StateData, StaticResponseExtender)]
struct ReadWithPath {
from: String,
id: u64
}
#[endpoint(method = "Method::GET", uri = "read/:from/with/:id")]
fn custom_read_with(_path: ReadWithPath) {}
#[endpoint(method = "Method::PATCH", uri = "", body = true)]
fn custom_patch(_body: String) {}
#[test]
fn openapi_specification() {
let info = OpenapiInfo {
@ -97,8 +114,9 @@ fn openapi_specification() {
let server = TestServer::new(build_router(chain, pipelines, |router| {
router.with_openapi(info, |mut router| {
router.resource::<ImageResource>("img");
router.get_openapi("openapi");
router.resource::<SecretResource>("secret");
router.resource::<CustomResource>("custom");
router.get_openapi("openapi");
});
}))
.unwrap();

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[endpoint(method = "I like pizza", uri = "custom_read")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,11 @@
error: unexpected token
--> $DIR/custom_method_invalid_expr.rs:8:21
|
8 | #[endpoint(method = "I like pizza", uri = "custom_read")]
| ^^^^^^^^^^^^^^
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/custom_method_invalid_expr.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[endpoint(method = "String::new()", uri = "custom_read")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,8 @@
error[E0308]: mismatched types
--> $DIR/custom_method_invalid_type.rs:8:21
|
8 | #[endpoint(method = "String::new()", uri = "custom_read")]
| --------------------^^^^^^^^^^^^^^^-----------------------
| | |
| | expected struct `Method`, found struct `std::string::String`
| expected `Method` because of return type

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[endpoint(uri = "custom_read")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,13 @@
error: Missing `method` attribute (e.g. `#[endpoint(method = "gotham_restful::gotham::hyper::Method::GET")]`)
--> $DIR/custom_method_missing.rs:8:1
|
8 | #[endpoint(uri = "custom_read")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/custom_method_missing.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[endpoint(method = "gotham_restful::gotham::hyper::Method::GET")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,13 @@
error: Missing `uri` attribute (e.g. `#[endpoint(uri = "custom_endpoint")]`)
--> $DIR/custom_uri_missing.rs:8:1
|
8 | #[endpoint(method = "gotham_restful::gotham::hyper::Method::GET")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/custom_uri_missing.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[read_all(body = false)]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,11 @@
error: `body` can only be used on custom endpoints
--> $DIR/non_custom_body_attribute.rs:8:12
|
8 | #[read_all(body = false)]
| ^^^^
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/non_custom_body_attribute.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[read_all(method = "gotham_restful::gotham::hyper::Method::GET")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,11 @@
error: `method` can only be used on custom endpoints
--> $DIR/non_custom_method_attribute.rs:8:12
|
8 | #[read_all(method = "gotham_restful::gotham::hyper::Method::GET")]
| ^^^^^^
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/non_custom_method_attribute.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[read_all(params = true)]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,11 @@
error: `params` can only be used on custom endpoints
--> $DIR/non_custom_params_attribute.rs:8:12
|
8 | #[read_all(params = true)]
| ^^^^^^
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/non_custom_params_attribute.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[read_all(uri = "custom_read")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,11 @@
error: `uri` can only be used on custom endpoints
--> $DIR/non_custom_uri_attribute.rs:8:12
|
8 | #[read_all(uri = "custom_read")]
| ^^^
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/non_custom_uri_attribute.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope

View file

@ -0,0 +1,11 @@
#[macro_use]
extern crate gotham_restful;
#[derive(Resource)]
#[resource(read_all)]
struct FooResource;
#[read_all(wants_auth = "yes, please")]
async fn read_all() {}
fn main() {}

View file

@ -0,0 +1,11 @@
error: Expected boolean literal
--> $DIR/wants_auth_non_bool.rs:8:25
|
8 | #[read_all(wants_auth = "yes, please")]
| ^^^^^^^^^^^^^
error[E0412]: cannot find type `read_all___gotham_restful_endpoint` in this scope
--> $DIR/wants_auth_non_bool.rs:5:12
|
5 | #[resource(read_all)]
| ^^^^^^^^ not found in this scope