work
This commit is contained in:
parent
b3a96ed3e3
commit
ca24591d9d
12 changed files with 177 additions and 105 deletions
81
src/api.rs
81
src/api.rs
|
@ -1,76 +1,3 @@
|
|||
/*
|
||||
import 'dart:convert';
|
||||
import 'package:cdb_ui/pages/supply.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
// todo : api errors
|
||||
|
||||
class API {
|
||||
SharedPreferences? pref;
|
||||
static final API _instance = API._internal();
|
||||
|
||||
// cache
|
||||
List<Item>? items;
|
||||
Map<String, Location>? locations;
|
||||
Map<String, FlowInfo>? flowInfos;
|
||||
|
||||
factory API() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
API._internal();
|
||||
|
||||
Future<void> init(Function refresh) async {
|
||||
pref = await SharedPreferences.getInstance();
|
||||
instance = pref!.getString("instance") ?? "";
|
||||
refresh();
|
||||
}
|
||||
|
||||
bool isInit() {
|
||||
if (pref == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return pref!.containsKey("token") && pref!.containsKey("instance");
|
||||
}
|
||||
|
||||
bool isPrefetched() {
|
||||
return items != null && locations != null && flowInfos != null;
|
||||
}
|
||||
|
||||
void save(String instance, String token) {
|
||||
pref!.setString("instance", instance);
|
||||
pref!.setString("token", token);
|
||||
this.instance = instance;
|
||||
}
|
||||
|
||||
String instance = "";
|
||||
|
||||
Future<String> getRequest(String url) async {
|
||||
var resp = await http.get(Uri.parse(url), headers: <String, String>{
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'Token': pref!.getString("token")!
|
||||
});
|
||||
|
||||
return utf8.decode(resp.bodyBytes);
|
||||
}
|
||||
|
||||
Future<String> postRequest(String url, Map<String, dynamic> data) async {
|
||||
var resp = await http.post(Uri.parse(url),
|
||||
headers: <String, String>{
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
'Token': pref!.getString("token")!
|
||||
},
|
||||
body: jsonEncode(data));
|
||||
|
||||
return utf8.decode(resp.bodyBytes);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use dioxus::signals::Readable;
|
||||
|
@ -84,6 +11,14 @@ use reqwest::Client;
|
|||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub fn get_item(item: &str) -> Option<Item> {
|
||||
crate::API
|
||||
.read()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.get_item(item.to_string())
|
||||
}
|
||||
|
||||
pub async fn api_get_auth<T>(path: String) -> Result<T, reqwest::Error>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue