This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackendService.cs
executable file
·209 lines (173 loc) · 6.21 KB
/
BackendService.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Specialized;
using Debug = System.Diagnostics.Debug;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Veiling
{
public class BackendService
{
const string EndPointString = "/api.php";
const string PingKey = "ping";
const string BetaalKey = "betaal";
const string BetaalSoortKey = "betaal_soort";
const string InfoKey = "bieer_naam";
const string DetailKey = "bieer_id";
const string NumberKey = "bieer_id";
const string PhotoKey = "foto";
const string UserAgent = "ANDROID APP";
private string apiEndpoint;
private string apiKey;
public BackendService(string endpointDomain, string key)
{
apiEndpoint = endpointDomain + EndPointString;
apiKey = key;
}
WebHeaderCollection AuthHeader
{
get
{
var header = new WebHeaderCollection();
header[HttpRequestHeader.Accept] = "application/json";
header[HttpRequestHeader.Authorization] = $"Bearer {apiKey}";
return header;
}
}
public async Task MarkPaid(List<int> nommers, string betaalSoort)
{
var payload = new NameValueCollection();
payload.Set(BetaalKey, JsonConvert.SerializeObject(nommers));
payload.Set(BetaalSoortKey, betaalSoort);
using (var client = new WebClient())
{
try
{
client.Headers = AuthHeader;
Debug.WriteLine ($"Updating {this.apiEndpoint} ...");
byte[] response = await client.UploadValuesTaskAsync(this.apiEndpoint, "POST", payload);
Debug.WriteLine("Response: " + Encoding.UTF8.GetString(response));
}
catch (WebException ex)
{
throw MapException(ex);
}
}
}
public async Task PingAsync()
{
Debug.WriteLine("PingAsync");
using (var client = new WebClient())
{
try
{
client.Headers = AuthHeader;
string response = await client.DownloadStringTaskAsync($"{apiEndpoint}?{PingKey}");
//Debug.WriteLine($"ping response: {response}");
}
catch (WebException ex)
{
throw MapException(ex);
}
}
}
public async Task<BieerItem> PullBieerInfoAsync(int id)
{
string json = "";
using (var client = new WebClient ()) {
try
{
client.Headers = AuthHeader;
json = await client.DownloadStringTaskAsync($"{apiEndpoint}?{InfoKey}={id}");
Debug.WriteLine($"response: {json}");
}
catch (WebException ex)
{
throw MapException(ex);
}
}
BieerItem bieer = JsonConvert.DeserializeObject<BieerItem> (json);
// TODO: endpoint should ideally return this
bieer.id = id;
bieer.nommer = $"{id}";
return bieer;
}
public async Task<List<VeilingItem>> PullItemsAsync(int id)
{
string jsonItems = "[]";
using (var client = new WebClient())
{
try
{
client.Headers = AuthHeader;
jsonItems = await client.DownloadStringTaskAsync($"{apiEndpoint}?{DetailKey}={id}");
Debug.WriteLine($"jsonItems: {jsonItems}");
}
catch (WebException ex)
{
throw MapException(ex);
}
}
var list = new List<VeilingItem>();
JArray items = JArray.Parse(jsonItems);
list = items.Select(item => new VeilingItem(item)).ToList();
return list;
}
public Task UploadImageAsync(int number, string imageFile)
{
return Task.Run(() =>
{
using (var client = new WebClient())
{
client.Headers = AuthHeader;
try
{
Debug.WriteLine($"upload to {apiEndpoint}");
client.UploadFile(new Uri($"{apiEndpoint}"), "POST", imageFile);
}
catch (WebException ex)
{
throw MapException(ex);
}
}
});
}
Exception MapException(WebException ex)
{
Debug.WriteLine($"Status code: {((HttpWebResponse)ex.Response).StatusCode}");
Debug.WriteLine($"message: {ex.Message}");
switch (ex.Status)
{
case WebExceptionStatus.NameResolutionFailure:
return new NoNetworkException();
case WebExceptionStatus.ProtocolError:
var code = ((HttpWebResponse)ex.Response).StatusCode;
Debug.WriteLine($"Code: {code}");
if (code == HttpStatusCode.Unauthorized)
{
return new AuthenticationException();
}
if (code == HttpStatusCode.NotFound)
{
return new ItemNotFoundException();
}
if (code == HttpStatusCode.Forbidden)
{
return new ForbiddenException();
}
if (code == (HttpStatusCode)422)
{
return new ValidationException();
}
return new WebException("Cloud error", ex);
default:
return new WebException("Cloud error", ex);
}
}
}
}