feat: Gist::get_file_route utility method to substitute values in get file routes

master
Aravinth Manivannan 2022-02-18 22:54:22 +05:30
parent bcb3545f8f
commit 3b4d944291
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 22 additions and 1 deletions

View File

@ -16,6 +16,7 @@
*/
//! V1 API Routes
use actix_auth_middleware::{Authentication, GetLoginRoute};
use serde::*;
use super::meta::routes::Meta;
@ -44,17 +45,37 @@ impl Auth {
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct GetFilePath {
pub username: String,
pub gist: String,
pub file: String,
}
/// Authentication routes
pub struct Gist {
/// logout route
pub new: &'static str,
/// get fie route
pub get_file: &'static str,
}
impl Gist {
/// create new instance of Authentication route
pub const fn new() -> Gist {
let new = "/api/v1/gist/new";
Gist { new }
let get_file = "/api/v1/gist/profile/{username}/{gist}/{file}";
Gist { new, get_file }
}
/// get file routes with placeholders replaced with values provided.
/// filename is auto-escaped using [urlencoding::encode]
pub fn get_file_route(&self, components: &GetFilePath) -> String {
self.get_file
.replace("{username}", &components.username)
.replace("{gist}", &components.gist)
.replace("{file}", &urlencoding::encode(&components.file))
}
}