-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRest.cs
159 lines (124 loc) · 5.39 KB
/
Rest.cs
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace SmartMerchant
{
public class Rest
{
internal const string ServiceUrlBase = "https://smartcash-demo.masterpiecefusion.com/api/";
// holds a reference to the logon token...
public static string Token { get; set; }
public static bool HasLogonToken
{
get
{
return !(string.IsNullOrEmpty(Token));
}
}
public static async Task<KeyValuePair<HttpStatusCode, JObject>> PostAsync(string Url, string jsonstr)
{
HttpClient client = new HttpClient();
if (HasLogonToken)
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token);
}
//no caching
client.DefaultRequestHeaders.IfModifiedSince = DateTimeOffset.Now;
StringContent content = new StringContent(jsonstr, Encoding.UTF8, "application/json");
var response = await client.PostAsync(ServiceUrlBase + Url, content);
// load it up...
string outputJson = await response.Content.ReadAsStringAsync();
Debug.WriteLine(Url);
Debug.WriteLine(outputJson);
JObject output = JObject.Parse(outputJson);
if (response.StatusCode == HttpStatusCode.NotFound)
{
ResourceLoader res = ResourceLoader.GetForCurrentView();
outputJson = "{\"status\":\"error\"," +
"\"error\":{" +
"\"code\":\"404\"," +
"\"message\":[\"" + res.GetString("NoInternet") + "\"]" +
"}}";
}
else if (response.StatusCode == HttpStatusCode.Unauthorized && Url != "logout" && Url != "auth")
{
await LogOutAsync();
}
return new KeyValuePair<HttpStatusCode, JObject>(response.StatusCode, output);
}
//Get methods
//Get methods
public static async Task<KeyValuePair<HttpStatusCode, JObject>> GetAsync(string Url, string myparams)
{
HttpClient client = new HttpClient();
if (HasLogonToken)
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", Token);
Debug.WriteLine(Token);
}
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
//no caching
client.DefaultRequestHeaders.IfModifiedSince = DateTimeOffset.Now;
Url = ServiceUrlBase + Url + myparams;
HttpResponseMessage response = await client.GetAsync(Url);
string outputJson = await response.Content.ReadAsStringAsync();
Debug.WriteLine(Url);
Debug.WriteLine(outputJson);
JObject output = JObject.Parse(outputJson);
if (response.StatusCode == HttpStatusCode.NotFound)
{
ResourceLoader res = ResourceLoader.GetForCurrentView();
outputJson = "{\"status\":\"error\"," +
"\"error\":{" +
"\"code\":\"404\"," +
"\"message\":[\"" + res.GetString("NoInternet") + "\"]" +
"}}";
}
else if (response.StatusCode == HttpStatusCode.Unauthorized)
{
await LogOutAsync();
}
return new KeyValuePair<HttpStatusCode, JObject>(response.StatusCode, output);
}
public static async Task LogOutAsync()
{
ResourceLoader res = ResourceLoader.GetForCurrentView();
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
if (UIHelper.HasInternetConnection())
{
await UIHelper.ToggleProgressBar(true, res.GetString("SignOut"));
var MyResult = await PostAsync("logout", "");
HttpStatusCode statuscode = MyResult.Key;
JObject output = MyResult.Value;
Debug.WriteLine(statuscode + " " + (string)output["message"]);
Frame rootFrame = Window.Current.Content as Frame;
if (statuscode == HttpStatusCode.OK)
{
//remove Token as is done
ApplicationData.Current.LocalSettings.Values.Remove("Token");
rootFrame.Navigate(typeof(LoginPage));
}
else
{
string error = (string)output["message"];
ApplicationData.Current.LocalSettings.Values.Remove("Token");
rootFrame.Navigate(typeof(LoginPage));
}
await UIHelper.ToggleProgressBar(false);
}
else
{
await UIHelper.ShowAlert(res.GetString("NoInternet"));
}
}
}
}