Add associated type for api response to service type
This commit is contained in:
parent
97066d1e06
commit
8e97cbdf3d
13
src/main.rs
13
src/main.rs
@ -11,7 +11,7 @@ mod statics;
|
|||||||
use crate::{
|
use crate::{
|
||||||
data::FilePath,
|
data::FilePath,
|
||||||
error::Result,
|
error::Result,
|
||||||
service::{GitHubApiResponse, Github, Service},
|
service::{ApiResponse, Bitbucket, Github, Service},
|
||||||
};
|
};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::header::{self, CacheControl, CacheDirective, Expires, LOCATION},
|
http::header::{self, CacheControl, CacheDirective, Expires, LOCATION},
|
||||||
@ -67,13 +67,18 @@ fn redirect<T: Service>(
|
|||||||
.and_then(move |mut response| match response.status() {
|
.and_then(move |mut response| match response.status() {
|
||||||
StatusCode::OK => Box::new(
|
StatusCode::OK => Box::new(
|
||||||
response
|
response
|
||||||
.json::<GitHubApiResponse>()
|
.json::<T::Response>()
|
||||||
.map(move |resp| {
|
.map(move |resp| {
|
||||||
HttpResponse::SeeOther()
|
HttpResponse::SeeOther()
|
||||||
.header(
|
.header(
|
||||||
LOCATION,
|
LOCATION,
|
||||||
T::redirect_url(&data.user, &data.repo, &resp.sha, &data.file)
|
T::redirect_url(
|
||||||
.as_str(),
|
&data.user,
|
||||||
|
&data.repo,
|
||||||
|
resp.commit_ref(),
|
||||||
|
&data.file,
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
)
|
)
|
||||||
.finish()
|
.finish()
|
||||||
})
|
})
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
use crate::data::FilePath;
|
use crate::data::FilePath;
|
||||||
// use actix_web::Error;
|
|
||||||
// use awc::Client;
|
pub(crate) trait ApiResponse {
|
||||||
// use futures::Future;
|
fn commit_ref(&self) -> &str;
|
||||||
// use std::borrow::Cow;
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub(crate) struct GitHubApiResponse {
|
pub(crate) struct GitHubApiResponse {
|
||||||
pub(crate) sha: String,
|
pub(crate) sha: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ApiResponse for GitHubApiResponse {
|
||||||
|
fn commit_ref(&self) -> &str {
|
||||||
|
&self.sha
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) trait Service {
|
pub(crate) trait Service {
|
||||||
|
type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static;
|
||||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
||||||
fn api_url(path: &FilePath) -> String;
|
fn api_url(path: &FilePath) -> String;
|
||||||
fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
|
||||||
@ -18,6 +25,8 @@ pub(crate) trait Service {
|
|||||||
pub(crate) struct Github;
|
pub(crate) struct Github;
|
||||||
|
|
||||||
impl Service for Github {
|
impl Service for Github {
|
||||||
|
type Response = GitHubApiResponse;
|
||||||
|
|
||||||
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
|
||||||
format!(
|
format!(
|
||||||
"https://raw.githubusercontent.com/{}/{}/{}/{}",
|
"https://raw.githubusercontent.com/{}/{}/{}/{}",
|
||||||
|
Loading…
Reference in New Issue
Block a user