Created UserService and TrashService
This commit is contained in:
parent
4c873d4d09
commit
0f6b8a93b4
@ -10,13 +10,23 @@ using UnityEngine;
|
||||
public class PocketBaseDataRepository : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] public String userToken;
|
||||
[SerializeField] public Trash[] trashes;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Trash t = TrashService.GetTrash("gdnuxl0wlgurtj3");
|
||||
Debug.Log(t);
|
||||
LoginResponse res = UserService.Login(new LoginRequest("************", "**********"));
|
||||
this.userToken = res.token;
|
||||
|
||||
TrashList list = TrashService.ListTrash(this.userToken);
|
||||
foreach (Trash trash in list.items)
|
||||
{
|
||||
Debug.Log(trash.id);
|
||||
DataList trashData = TrashService.ListTrashData(userToken, trash.id);
|
||||
trash.data = trashData.items;
|
||||
}
|
||||
this.trashes = list.items;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
|
29
models/Data.cs
Normal file
29
models/Data.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace models
|
||||
{
|
||||
[Serializable]
|
||||
public class Data
|
||||
{
|
||||
public String id;
|
||||
public String collectionId;
|
||||
public String collectionName;
|
||||
public DateTime created;
|
||||
public DateTime updated;
|
||||
public int value;
|
||||
public String trash_id;
|
||||
public String unit;
|
||||
public Boolean status;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class DataList
|
||||
{
|
||||
public int page;
|
||||
public int perPage;
|
||||
public int totalPages;
|
||||
public int totalItems;
|
||||
public List<Data> items;
|
||||
}
|
||||
}
|
3
models/Data.cs.meta
Normal file
3
models/Data.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9c778b8c2f64792bbbb0aac4cbe431c
|
||||
timeCreated: 1679392634
|
@ -6,29 +6,36 @@ namespace models
|
||||
[Serializable]
|
||||
public class LoginRequest
|
||||
{
|
||||
public String username;
|
||||
public String identity;
|
||||
public String password;
|
||||
|
||||
public LoginRequest(string identity, string password)
|
||||
{
|
||||
this.identity = identity;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class LoginResponse
|
||||
{
|
||||
public Record record { get; set; }
|
||||
public string token { get; set; }
|
||||
public Record record;
|
||||
public string token;
|
||||
|
||||
[Serializable]
|
||||
public class Record
|
||||
{
|
||||
public string avatar { get; set; }
|
||||
public string collectionId { get; set; }
|
||||
public string collectionName { get; set; }
|
||||
public string created { get; set; }
|
||||
public string email { get; set; }
|
||||
public bool emailVisibility { get; set; }
|
||||
public string id { get; set; }
|
||||
public string name { get; set; }
|
||||
public string updated { get; set; }
|
||||
public string username { get; set; }
|
||||
public bool verified { get; set; }
|
||||
public string avatar;
|
||||
public string collectionId;
|
||||
public string collectionName;
|
||||
public string created;
|
||||
public string email;
|
||||
public bool emailVisibility;
|
||||
public string id;
|
||||
public string name;
|
||||
public string updated;
|
||||
public string username;
|
||||
public bool verified;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace models
|
||||
{
|
||||
[Serializable]
|
||||
public class Trash
|
||||
{
|
||||
public String id;
|
||||
public String collectionId;
|
||||
public String collectionName;
|
||||
public DateTime created;
|
||||
public DateTime updated;
|
||||
public Int16 value;
|
||||
public String trash_id;
|
||||
public String unit;
|
||||
public Boolean status;
|
||||
public String field;
|
||||
public string collectionId;
|
||||
public string collectionName;
|
||||
public string created;
|
||||
public string id;
|
||||
public string owner;
|
||||
public string updated;
|
||||
public List<Data> data;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class TrashList
|
||||
{
|
||||
public int page;
|
||||
public int perPage;
|
||||
public int totalItems;
|
||||
public int totalPages;
|
||||
public Trash[] items;
|
||||
}
|
||||
}
|
@ -9,9 +9,19 @@ namespace services
|
||||
{
|
||||
public class TrashService
|
||||
{
|
||||
public static Trash GetTrash(String id)
|
||||
public static Trash GetTrash(String token, String id)
|
||||
{
|
||||
return Api<Trash>.get("/api/collections/trashs/records/"+id);
|
||||
return Api<Trash>.get("/api/collections/trashs/records/"+id, token);
|
||||
}
|
||||
|
||||
public static TrashList ListTrash(String token)
|
||||
{
|
||||
return Api<TrashList>.get("/api/collections/trashs/records/", token);
|
||||
}
|
||||
|
||||
public static DataList ListTrashData(String token, String trashId)
|
||||
{
|
||||
return Api<DataList>.get("/api/collections/data/records/?trash_id="+trashId, token);
|
||||
}
|
||||
}
|
||||
}
|
15
services/UserService.cs
Normal file
15
services/UserService.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using models;
|
||||
using UnityEngine;
|
||||
using utils;
|
||||
|
||||
namespace services
|
||||
{
|
||||
public class UserService
|
||||
{
|
||||
public static LoginResponse Login(LoginRequest request)
|
||||
{
|
||||
var response = Api<LoginResponse>.post("/api/collections/users/auth-with-password", JsonUtility.ToJson(request));
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
3
services/UserService.cs.meta
Normal file
3
services/UserService.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fe63392645946c48c112658ebbf75c5
|
||||
timeCreated: 1679389336
|
29
utils/Api.cs
29
utils/Api.cs
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using models;
|
||||
using UnityEngine;
|
||||
|
||||
@ -9,16 +10,19 @@ namespace utils
|
||||
public class Api<T>
|
||||
{
|
||||
public static String API_URL = "https://iot.epi.cb85.software";
|
||||
public static int API_TIMEOUT = 10000;
|
||||
|
||||
public static HttpWebRequest createClient(String suffix) {
|
||||
return (HttpWebRequest)WebRequest.Create(API_URL + suffix);
|
||||
}
|
||||
|
||||
public static T get(String suffix)
|
||||
public static T get(String suffix, String token)
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_URL + suffix);
|
||||
request.Accept = "application/json";
|
||||
request.Method = "POST";
|
||||
request.Method = "GET";
|
||||
request.Headers.Add("Authorization", token);
|
||||
request.Timeout=API_TIMEOUT;
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
if (response == null)
|
||||
{
|
||||
@ -27,6 +31,7 @@ namespace utils
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
string jsonResponse = reader.ReadToEnd();
|
||||
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
||||
response.Close();
|
||||
return responseData;
|
||||
}
|
||||
|
||||
@ -35,31 +40,19 @@ namespace utils
|
||||
HttpWebRequest request = createClient(suffix);
|
||||
request.ContentType = "application/json";
|
||||
request.Method = "POST";
|
||||
request.Accept = "application/json";
|
||||
|
||||
request.Timeout=API_TIMEOUT;
|
||||
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
|
||||
{
|
||||
streamWriter.Write(body);
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
}
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
string jsonResponse = reader.ReadToEnd();
|
||||
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
||||
return responseData;
|
||||
}
|
||||
|
||||
public T send(HttpWebRequest request)
|
||||
{
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
if (response == null)
|
||||
{
|
||||
throw new Exception("Response is null");
|
||||
}
|
||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||
string jsonResponse = reader.ReadToEnd();
|
||||
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
||||
response.Close();
|
||||
return responseData;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user