Skip to content
Snippets Groups Projects
Commit aef6b64c authored by HGEpro's avatar HGEpro
Browse files

finished network.js (not fully tested)

parent 1f7b59a4
No related branches found
No related tags found
No related merge requests found
......@@ -18,7 +18,8 @@ tauri-build = { version = "1.0.0-beta.4", features = [] }
tauri = { version = "1.0.0-beta.8", features = ["fs-all", "window-all"] }
rusqlite = "0.26.1"
serde = { version = "1.0", features = ["derive"] }
reqwest = {version = "0.11", features = ["json"]}
serde_json = "1.0"
reqwest = {version = "0.11", features = ["json"] }
[features]
default = [ "custom-protocol" ]
......
......@@ -6,13 +6,14 @@
mod db;
mod models;
mod network;
mod utils;
mod network;
use tauri::{Manager, RunEvent};
use std::sync::atomic::{AtomicBool, Ordering};
use db::*;
use utils::*;
use network::*;
static IS_LOCKED: AtomicBool = AtomicBool::new(false);
......@@ -51,6 +52,7 @@ fn main() {
remove_task,
get_tasks,
get_subjects,
request,
add_subject,
remove_subject,
write_config_file,
......
......@@ -56,7 +56,7 @@
"fs": {
"all": true,
"scope": ["$DOCUMENT/LibreHomework/*"],
"scope": ["$CONFIG/LibreHomework/*"]
}
},
"windows": [
......
......@@ -52,5 +52,6 @@ String.prototype.capitalize = function() {
let net = new ServerAPI()
console.log(net)
net.getDailyMessage().then((d) => console.log(d))
export default svapp;
class ServerAPI {
constructor() {
this.url = "https://librehomework-api.herokuapp.com/";
import { invoke } from "@tauri-apps/api";
let res = fetch(this.url + "dailymessage/").then((r) => {
if (r.status == 200) {
return [true, r.json()];
}
else {
return [false, r.status];
}
}); //should a different function for /dailymessage exist?
class ApiResponse {
constructor(payload) {
this.status = payload[1];
this.data = data[0];
}
}
class ServerAPI {
async getDailyMessage() {
return new ApiResponse(await invoke("request", {"url": "dailymessage"}));
}
async getUsers(page=0) {
let res = await fetch(this.url + "users/" + page);
if (res.status == 200) {
return [true, res.json()];
}
else {
return [false, res.status];
}
return new ApiResponse(await invoke("request", {"url": "users/" + page}));
}
async findUser(username) {
let res = await fetch(this.url + "find/" + username);
if (res.status == 200) {
return [true, res.json()];
}
else {
return [false, res.status];
}
return new ApiResponse(await invoke("request", {"url": "find/" + username}));
}
async login(username, password) {
let res = await fetch(this.url + "login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username,
password: password
})
});
if (res.status == 200) {
return [true, res.json()];
}
else {
return [false, res.status];
}
return new ApiResponse(await invoke("request", {"url": "login", "method": "POST", "body": JSON.stringify({
"username": username,
"password": password
})}));
}
async register(username, password, email, discord, twitter, bio) {
let res = await fetch(this.url + "register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: username,
password: password,
email: email,
discord: discord,
twitter: twitter,
bio: bio
})
});
if (res.status == 200) {
return [true, res.json()];
}
else {
return [false, res.status];
}
async signup(username, password, email=null, discord=null, twitter=null, bio=null) {
return new ApiResponse(await invoke("request", {"url": "signup", "method": "POST", "body": JSON.stringify({
"username": username,
"password": password,
"email": email,
"discord": discord,
"twitter": twitter,
"bio": bio
})}));
}
async deleteAccount(token) {
let res = await fetch(this.url + "remove", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
token: token
})
});
if (res.status == 200) {
return [true, res.json()];
}
else {
return [false, res.status];
}
return new ApiResponse(await invoke("request", {"url": "remove", "method": "POST", "body": JSON.stringify({
"token": token
})}));
}
//password or username cannot be changed, althought I might add a way to change it in the future
async editProfile(token, email, discord, twitter, bio) {
let res = await fetch(this.url + "edit", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
token: token,
email: email,
discord: discord,
twitter: twitter,
bio: bio
})
});
if (res.status == 200) {
return [true, res.json()];
}
else {
return [false, res.status];
}
//server should handle the optional fields but check if at least one param is not null
async editProfile(token, email=null, discord=null, twitter=null, bio=null) {
return new ApiResponse(await invoke("request", {"url": "remove", "method": "POST", "body": JSON.stringify({
"token": token
})}));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment