From 0f6b8a93b49fa9ad4d561eee8aafedb1e97633e0 Mon Sep 17 00:00:00 2001 From: Nicolas SANS Date: Tue, 21 Mar 2023 11:34:57 +0100 Subject: [PATCH] Created UserService and TrashService --- PocketBaseDataRepository.cs | 18 ++++++++++++++---- models/Data.cs | 29 +++++++++++++++++++++++++++++ models/Data.cs.meta | 3 +++ models/LoginDto.cs | 35 +++++++++++++++++++++-------------- models/Trash.cs | 28 ++++++++++++++++++---------- services/TrashService.cs | 14 ++++++++++++-- services/UserService.cs | 15 +++++++++++++++ services/UserService.cs.meta | 3 +++ utils/Api.cs | 29 +++++++++++------------------ 9 files changed, 126 insertions(+), 48 deletions(-) create mode 100644 models/Data.cs create mode 100644 models/Data.cs.meta create mode 100644 services/UserService.cs create mode 100644 services/UserService.cs.meta diff --git a/PocketBaseDataRepository.cs b/PocketBaseDataRepository.cs index 944a5b5..1b7c9d9 100644 --- a/PocketBaseDataRepository.cs +++ b/PocketBaseDataRepository.cs @@ -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() diff --git a/models/Data.cs b/models/Data.cs new file mode 100644 index 0000000..fa094f4 --- /dev/null +++ b/models/Data.cs @@ -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 items; + } +} \ No newline at end of file diff --git a/models/Data.cs.meta b/models/Data.cs.meta new file mode 100644 index 0000000..1a7b2a2 --- /dev/null +++ b/models/Data.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f9c778b8c2f64792bbbb0aac4cbe431c +timeCreated: 1679392634 \ No newline at end of file diff --git a/models/LoginDto.cs b/models/LoginDto.cs index a404263..7c55edd 100644 --- a/models/LoginDto.cs +++ b/models/LoginDto.cs @@ -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; } } } \ No newline at end of file diff --git a/models/Trash.cs b/models/Trash.cs index c181b79..6969f2a 100644 --- a/models/Trash.cs +++ b/models/Trash.cs @@ -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; + } + + [Serializable] + public class TrashList + { + public int page; + public int perPage; + public int totalItems; + public int totalPages; + public Trash[] items; } } \ No newline at end of file diff --git a/services/TrashService.cs b/services/TrashService.cs index d90e8d7..7e7eec4 100644 --- a/services/TrashService.cs +++ b/services/TrashService.cs @@ -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.get("/api/collections/trashs/records/"+id); + return Api.get("/api/collections/trashs/records/"+id, token); + } + + public static TrashList ListTrash(String token) + { + return Api.get("/api/collections/trashs/records/", token); + } + + public static DataList ListTrashData(String token, String trashId) + { + return Api.get("/api/collections/data/records/?trash_id="+trashId, token); } } } \ No newline at end of file diff --git a/services/UserService.cs b/services/UserService.cs new file mode 100644 index 0000000..480a953 --- /dev/null +++ b/services/UserService.cs @@ -0,0 +1,15 @@ +using models; +using UnityEngine; +using utils; + +namespace services +{ + public class UserService + { + public static LoginResponse Login(LoginRequest request) + { + var response = Api.post("/api/collections/users/auth-with-password", JsonUtility.ToJson(request)); + return response; + } + } +} \ No newline at end of file diff --git a/services/UserService.cs.meta b/services/UserService.cs.meta new file mode 100644 index 0000000..04fb540 --- /dev/null +++ b/services/UserService.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2fe63392645946c48c112658ebbf75c5 +timeCreated: 1679389336 \ No newline at end of file diff --git a/utils/Api.cs b/utils/Api.cs index 3b7b0cc..9d7586d 100644 --- a/utils/Api.cs +++ b/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 { 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(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(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(jsonResponse); + response.Close(); return responseData; } } - } \ No newline at end of file