WebAPIManager.cs 5.6 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class WebAPIManager : MonoBehaviour
{
    [SerializeField]
    public string BASE_URL;
    
    public IEnumerator GetRequest(string uri)
    {
        uri = BASE_URL + uri;

        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            string[] pages = uri.Split('/');
            int page = pages.Length - 1;

            switch (webRequest.result)
            {
                case UnityWebRequest.Result.ConnectionError:
                case UnityWebRequest.Result.DataProcessingError:
                    Debug.LogError(pages[page] + ": Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.ProtocolError:
                    Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.Success:
                    Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
                    break;
            }
        }
    }

    public IEnumerator GetRequest(string uri, System.Action<string> callback)
    {
        uri = BASE_URL + uri;

        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            string[] pages = uri.Split('/');
            int page = pages.Length - 1;

            switch (webRequest.result)
            {
                case UnityWebRequest.Result.ConnectionError:
                case UnityWebRequest.Result.DataProcessingError:
                    Debug.LogError(pages[page] + ": Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.ProtocolError:
                    Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
                    break;
                case UnityWebRequest.Result.Success:
                    Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
                    callback?.Invoke(webRequest.downloadHandler.text);
                    break;
            }
        }
    }

    public IEnumerator Upload(string uri, string jsonData)
    {
        uri = BASE_URL + uri;
        using (UnityWebRequest www = UnityWebRequest.Post(uri, jsonData, "application/json"))
        {
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError(www.error);
            }
            else
            {
                Debug.Log("Form upload completed!");
                Debug.Log(www.downloadHandler.text);
            }
        }
    }

    public IEnumerator Upload(string uri, string jsonData, System.Action<string> callback)
    {
        uri = BASE_URL + uri;
        using (UnityWebRequest www = UnityWebRequest.Post(uri, jsonData, "application/json"))
        {
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError(www.error);
                callback?.Invoke(null);
            }
            else
            {                
                Debug.Log(www.downloadHandler.text);
                callback?.Invoke(www.downloadHandler.text);
            }
        }
    }

    public IEnumerator PutRequest(string uri, string jsonData, System.Action<string> callback)
    {
        uri = BASE_URL + uri;
        using (UnityWebRequest webRequest = UnityWebRequest.Put(uri, jsonData))
        {
            // Set the content type to application/json
            webRequest.SetRequestHeader("Content-Type", "application/json");

            yield return webRequest.SendWebRequest();

            if (webRequest.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError("PUT Request Failed: " + webRequest.error); 
                callback?.Invoke(null);
            }
            else
            {
                Debug.Log("PUT Request Successful: " + webRequest.downloadHandler.text);
                callback?.Invoke(webRequest.downloadHandler.text);
            }
        }
    }
}


[Serializable]
public class PersonalProfileData
{
    public string firstName;
    public string lastName;
    public int age;
    public string nationality;
    public string email;
}

[Serializable]
public class Profile
{
    public string personalProfileID;
    public string header;
    public string mInstanceID;
    public string humanID;
    public PersonalProfileData personalProfileData;
    public string descrMetadata;
}

[Serializable]
public class Account
{

    public string accountID;
    public string header;
    public string mInstanceID;
    public string mEnvironmentID;
    public string humanID;
    public string personalProfileID;
    public List<Right> rights;
    public List<User> users;
    public List<Persona> personae;
    public string descrMetadata;
}

[Serializable]
public class Right
{
    public string rightID;
    public string header;
    public string mInstanceID;
    public List<RightData> rightsData;
    public string descrMetadata;
}

[Serializable]
public class User
{
cann-alberto's avatar
cann-alberto committed
183
184
185
186
    public string userID;
    public string humanID;
    public string comIp;
    public string comPort;
cann-alberto's avatar
cann-alberto committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
}

[Serializable]
public class Persona
{
    public string personaID;

    public string model;
}

[Serializable]
public class RightData
{

    public string level;

    public string processAction;
}