Add associated type for api response to service type

This commit is contained in:
Valentin Brandl 2019-07-26 14:18:36 +02:00
parent 97066d1e06
commit 8e97cbdf3d
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
2 changed files with 22 additions and 8 deletions

View File

@ -11,7 +11,7 @@ mod statics;
use crate::{
data::FilePath,
error::Result,
service::{GitHubApiResponse, Github, Service},
service::{ApiResponse, Bitbucket, Github, Service},
};
use actix_web::{
http::header::{self, CacheControl, CacheDirective, Expires, LOCATION},
@ -67,13 +67,18 @@ fn redirect<T: Service>(
.and_then(move |mut response| match response.status() {
StatusCode::OK => Box::new(
response
.json::<GitHubApiResponse>()
.json::<T::Response>()
.map(move |resp| {
HttpResponse::SeeOther()
.header(
LOCATION,
T::redirect_url(&data.user, &data.repo, &resp.sha, &data.file)
.as_str(),
T::redirect_url(
&data.user,
&data.repo,
resp.commit_ref(),
&data.file,
)
.as_str(),
)
.finish()
})

View File

@ -1,15 +1,22 @@
use crate::data::FilePath;
// use actix_web::Error;
// use awc::Client;
// use futures::Future;
// use std::borrow::Cow;
pub(crate) trait ApiResponse {
fn commit_ref(&self) -> &str;
}
#[derive(Deserialize)]
pub(crate) struct GitHubApiResponse {
pub(crate) sha: String,
}
impl ApiResponse for GitHubApiResponse {
fn commit_ref(&self) -> &str {
&self.sha
}
}
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 api_url(path: &FilePath) -> 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;
impl Service for Github {
type Response = GitHubApiResponse;
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
format!(
"https://raw.githubusercontent.com/{}/{}/{}/{}",