Commit 2031edc2 authored by Vincent's avatar Vincent 💩

Working on dorequest, added http status to webbexception

parent d9814fee
......@@ -10,22 +10,23 @@ namespace DiveLogUploader {
public class WebserviceError: Exception, ISerializable {
public string error;
public string Error;
public HttpStatusCode Code;
public override string Message {
get { return error; }
get { return Error; }
}
public string GetMessage() {
return error;
return Error;
}
public WebserviceError(SerializationInfo info, StreamingContext context) : base() {
error = info.GetString("error");
Error = info.GetString("error");
}
public override string ToString() {
return "WebserviceError: " + error;
return "WebserviceError: " + Error;
}
}
......@@ -123,7 +124,9 @@ namespace DiveLogUploader {
using (var streamReader = new StreamReader(response.GetResponseStream())) {
using (var jsonReader = new JsonTextReader(streamReader)) {
if (response.StatusCode != HttpStatusCode.OK) {
throw serializer.Deserialize<WebserviceError>(jsonReader);
var err = serializer.Deserialize<WebserviceError>(jsonReader);
err.Code = response.StatusCode;
throw err;
}
obj = serializer.Deserialize<TOUT>(jsonReader);
}
......
......@@ -160,22 +160,41 @@ namespace DiveLogUploader {
return;
}
}
public async Task<TOUT> DoRequest<TIN, TOUT>(
public async Task<TOUT> DoRequest<TOUT>(
string path,
HttpVerb verb,
TIN data = default(TIN)
HttpVerb verb
) {
var resp = await Request.JsonAsync<TIN, TOUT>(
var resp = await Request.JsonAsync<TOUT>(
BASE_URL + path,
verb,
data,
TokenHeader(_accessToken)
);
return resp;
}
public async Task<TOUT> DoRequest<TIN, TOUT>(
string path,
HttpVerb verb,
TIN data = default(TIN)
) {
try {
var resp = await Request.JsonAsync<TIN, TOUT>(
BASE_URL + path,
verb,
data,
TokenHeader(_accessToken)
);
return resp;
} catch(WebserviceError ex) when (ex.Code == HttpStatusCode.Unauthorized) {
await GetAccessToken();
await DoRequest<TIN, TOUT>(path, verb, data);
}
}
private async Task GetAccessToken() {
var dat = await Request.JsonAsync<TokenResponseData>(
BASE_URL + "auth/access-token",
......@@ -188,10 +207,9 @@ namespace DiveLogUploader {
private async Task GetData() {
try {
var result = await Request.JsonAsync<UserData>(
BASE_URL + "user/profile",
HttpVerb.GET,
TokenHeader(_accessToken)
var result = await DoRequest<UserData>(
"user/profile",
HttpVerb.GET
);
Data = result ?? throw new Exception("Invalid response from server");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment