Created UserService and TrashService
This commit is contained in:
parent
4c873d4d09
commit
0f6b8a93b4
@ -10,13 +10,23 @@ using UnityEngine;
|
|||||||
public class PocketBaseDataRepository : MonoBehaviour
|
public class PocketBaseDataRepository : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
|
||||||
|
[SerializeField] public String userToken;
|
||||||
|
[SerializeField] public Trash[] trashes;
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
Trash t = TrashService.GetTrash("gdnuxl0wlgurtj3");
|
LoginResponse res = UserService.Login(new LoginRequest("************", "**********"));
|
||||||
Debug.Log(t);
|
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
|
// Update is called once per frame
|
||||||
void Update()
|
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]
|
[Serializable]
|
||||||
public class LoginRequest
|
public class LoginRequest
|
||||||
{
|
{
|
||||||
public String username;
|
public String identity;
|
||||||
public String password;
|
public String password;
|
||||||
|
|
||||||
|
public LoginRequest(string identity, string password)
|
||||||
|
{
|
||||||
|
this.identity = identity;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class LoginResponse
|
public class LoginResponse
|
||||||
{
|
{
|
||||||
public Record record { get; set; }
|
public Record record;
|
||||||
public string token { get; set; }
|
public string token;
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
public class Record
|
public class Record
|
||||||
{
|
{
|
||||||
public string avatar { get; set; }
|
public string avatar;
|
||||||
public string collectionId { get; set; }
|
public string collectionId;
|
||||||
public string collectionName { get; set; }
|
public string collectionName;
|
||||||
public string created { get; set; }
|
public string created;
|
||||||
public string email { get; set; }
|
public string email;
|
||||||
public bool emailVisibility { get; set; }
|
public bool emailVisibility;
|
||||||
public string id { get; set; }
|
public string id;
|
||||||
public string name { get; set; }
|
public string name;
|
||||||
public string updated { get; set; }
|
public string updated;
|
||||||
public string username { get; set; }
|
public string username;
|
||||||
public bool verified { get; set; }
|
public bool verified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,19 +1,27 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace models
|
namespace models
|
||||||
{
|
{
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Trash
|
public class Trash
|
||||||
{
|
{
|
||||||
public String id;
|
public string collectionId;
|
||||||
public String collectionId;
|
public string collectionName;
|
||||||
public String collectionName;
|
public string created;
|
||||||
public DateTime created;
|
public string id;
|
||||||
public DateTime updated;
|
public string owner;
|
||||||
public Int16 value;
|
public string updated;
|
||||||
public String trash_id;
|
public List<Data> data;
|
||||||
public String unit;
|
}
|
||||||
public Boolean status;
|
|
||||||
public String field;
|
[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 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;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
using models;
|
using models;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@ -9,16 +10,19 @@ namespace utils
|
|||||||
public class Api<T>
|
public class Api<T>
|
||||||
{
|
{
|
||||||
public static String API_URL = "https://iot.epi.cb85.software";
|
public static String API_URL = "https://iot.epi.cb85.software";
|
||||||
|
public static int API_TIMEOUT = 10000;
|
||||||
|
|
||||||
public static HttpWebRequest createClient(String suffix) {
|
public static HttpWebRequest createClient(String suffix) {
|
||||||
return (HttpWebRequest)WebRequest.Create(API_URL + 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);
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_URL + suffix);
|
||||||
request.Accept = "application/json";
|
request.Accept = "application/json";
|
||||||
request.Method = "POST";
|
request.Method = "GET";
|
||||||
|
request.Headers.Add("Authorization", token);
|
||||||
|
request.Timeout=API_TIMEOUT;
|
||||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||||
if (response == null)
|
if (response == null)
|
||||||
{
|
{
|
||||||
@ -27,6 +31,7 @@ namespace utils
|
|||||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||||
string jsonResponse = reader.ReadToEnd();
|
string jsonResponse = reader.ReadToEnd();
|
||||||
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
||||||
|
response.Close();
|
||||||
return responseData;
|
return responseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,31 +40,19 @@ namespace utils
|
|||||||
HttpWebRequest request = createClient(suffix);
|
HttpWebRequest request = createClient(suffix);
|
||||||
request.ContentType = "application/json";
|
request.ContentType = "application/json";
|
||||||
request.Method = "POST";
|
request.Method = "POST";
|
||||||
request.Accept = "application/json";
|
request.Timeout=API_TIMEOUT;
|
||||||
|
|
||||||
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
|
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
|
||||||
{
|
{
|
||||||
streamWriter.Write(body);
|
streamWriter.Write(body);
|
||||||
|
streamWriter.Flush();
|
||||||
|
streamWriter.Close();
|
||||||
}
|
}
|
||||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||||
StreamReader reader = new StreamReader(response.GetResponseStream());
|
StreamReader reader = new StreamReader(response.GetResponseStream());
|
||||||
string jsonResponse = reader.ReadToEnd();
|
string jsonResponse = reader.ReadToEnd();
|
||||||
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
T responseData = JsonUtility.FromJson<T>(jsonResponse);
|
||||||
return responseData;
|
response.Close();
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
return responseData;
|
return responseData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user