Deserialize paste json properly

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-12-11 19:45:48 +05:30
parent e7a18f4b03
commit 01d902d311
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5
3 changed files with 43 additions and 10 deletions

18
Cargo.lock generated
View file

@ -7,8 +7,10 @@ name = "actix-backend"
version = "0.1.0"
dependencies = [
"actix-web",
"base64 0.20.0",
"reqwest",
"serde",
"serde_json",
"tera",
]
@ -40,7 +42,7 @@ dependencies = [
"actix-service",
"actix-utils",
"ahash",
"base64",
"base64 0.13.1",
"bitflags",
"brotli",
"bytes",
@ -253,6 +255,12 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ea22880d78093b0cbe17c89f64a7d457941e65759157ec6cb31a31d652b05e5"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -1189,7 +1197,7 @@ version = "0.11.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c"
dependencies = [
"base64",
"base64 0.13.1",
"bytes",
"encoding_rs",
"futures-core",
@ -1264,7 +1272,7 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
dependencies = [
"base64",
"base64 0.13.1",
]
[[package]]
@ -1332,9 +1340,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.87"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45"
checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
dependencies = [
"itoa",
"ryu",

View file

@ -10,3 +10,5 @@ actix-web = "4"
tera = "1.17.1"
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }
serde_json = "1.0.89"
base64 = "0.20.0"

View file

@ -1,6 +1,6 @@
use actix_web::{get, App, HttpResponse, HttpServer, Responder, web::{Data, Query}};
use tera::{Tera, Context};
use serde::Deserialize;
use serde::{Serialize, Deserialize};
use reqwest::{Client, header::HeaderMap};
struct Template {
@ -12,6 +12,20 @@ struct Paste {
paste_id: Option<String>,
}
#[derive(Serialize, Deserialize)]
struct ResponseJSON {
file_name: String,
file_path: String,
size: u64,
encoding: String,
content_sha256: String,
r#ref: String,
blob_id: String,
commit_id: String,
last_commit_id: String,
execute_filemode: bool,
content: String,
}
#[get("/")]
async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Responder {
@ -20,14 +34,23 @@ async fn paste_render(data: Data<Template>, paste: Query<Paste>) -> impl Respond
let client = Client::new();
let mut headers = HeaderMap::new();
headers.insert("PRIVATE-TOKEN", "".parse().unwrap());
let resp: String;
let paste_content: String;
if paste.paste_id.is_some() {
resp = client.get(format!("https://git.baalajimaestro.me/api/v4/projects/12/repository/files/paste%2F{}.md?ref=master", paste.paste_id.as_ref().unwrap())).headers(headers).send().await.unwrap().text().await.unwrap();
let response = client.get(format!("https://git.baalajimaestro.me/api/v4/projects/12/repository/files/paste%2F{}.md?ref=master", paste.paste_id.as_ref().unwrap())).headers(headers).send().await.unwrap();
let response_code = response.status().as_str().to_string();
let response_json = response.text().await.unwrap();
if response_code != "200" {
paste_content = "Paste ID Invalid".to_string();
}
else {
resp = "".to_string();
let resp_struct: ResponseJSON = serde_json::from_str(&response_json).unwrap();
paste_content = String::from_utf8(base64::decode(resp_struct.content).unwrap()).unwrap();
}
context.insert("paste_content", &resp);
}
else {
paste_content = "Provide a Paste ID".to_string();
}
context.insert("paste_content", &paste_content);
let rendered = my_data.paste.render("paste.html", &context);
HttpResponse::Ok().body(rendered.unwrap())
}