-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRESTClient.cs
More file actions
172 lines (151 loc) · 4.05 KB
/
RESTClient.cs
File metadata and controls
172 lines (151 loc) · 4.05 KB
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
using System;
using System.Collections.Generic;
using UnityEngine;
using REST;
public sealed class RESTClient : ScriptableObject
{
private static Action OnInitialized;
private static IREST _impl;
private static bool _isInitCalled = false;
private static string _accessToken;
private static string _userId;
private static IREST Impl
{
get
{
return _impl;
}
set
{
if (value == null)
{
throw new NullReferenceException("REST object is not yet loaded. Did you call REST.Init()?");
}
_impl = value;
}
}
public static string AccessToken
{
get
{
return (_impl != null) ? _impl.AccessToken : string.Empty;
}
set
{
if (value != null) _impl.AccessToken = value;
}
}
public static string UserID
{
get
{
return (_impl != null) ? _impl.UserID : string.Empty;
}
set
{
_impl.UserID = value;
}
}
public static bool IsLoggedIn
{
get
{
return (_impl != null) && _impl.IsLoggedIn;
}
}
public static bool IsInitialized
{
get
{
return (_impl != null) && _impl.IsInitialized;
}
}
public static TimeSpan RequestTimeout
{
get
{
return (_impl != null) ? _impl.RequestTimeout : TimeSpan.FromSeconds(60);
}
set
{
if (value != null) _impl.RequestTimeout = value;
}
}
public static void Api(string query, HTTPMethod method, Action<Result> callback, Dictionary<string, string> formData)
{
if (IsInitialized)
{
//NetworkManager.DebugLog("HOST : " + Impl.Host);
Debug.Log("HOST : " + Impl.Host);
_impl.Api(query, method, callback, formData);
}
}
public static void JsonApi(string query, string jsonString, Action<Result> callback)
{
if (IsInitialized)
{
_impl.JsonApi(query, jsonString, callback);
}
}
public static void Init(Action callback)
{
if (!_isInitCalled)
{
OnInitialized += callback;
ComponentFactory.GetComponent<RESTClientLoader>();
_isInitCalled = true;
return;
}
Debug.LogWarning("RESTClient.Init() has already been called. You only need to call this once and only once.");
// Init again if possible just in case something bad actually happened.
if (Impl != null)
{
OnDllLoaded();
}
}
public static void Login(Action<Result> callback, Dictionary<string, string> formData)
{
if (_impl != null)
{
_impl.Login(callback, formData);
}
}
public static void Logout(Action<Result> callback)
{
if (_impl != null)
{
_impl.Logout(callback);
}
}
private static void OnDllLoaded()
{
Impl.Init(OnInitialized);
}
public class RESTClientLoader : MonoBehaviour
{
protected IREST RestClient
{
get { return ComponentFactory.GetComponent<RESTImpl>(); }
}
private void Start()
{
RESTClient.Impl = RestClient;
//if (GameInfoManager.Instance.isStageSever)
{
//GameInfoManager.Instance.isDevServer = false;
//Impl.Host = "https://127.0.0.1:8010";
}
//else if (GameInfoManager.Instance.isDevServer)
{
//Impl.Host = "http://127.0.0.1:8010/";
}
Impl.Host = "https://jsonplaceholder.typicode.com";
//NetworkManager.DebugLog("HOST : " + Impl.Host);
Impl.UserID = "1400000002";
Impl.AccessToken = "unitytest";
Impl.RequestTimeout = TimeSpan.FromSeconds(20);
RESTClient.OnDllLoaded();
Destroy(this);
}
}
}