private void AfterStatusesLoaded(IAsyncResult result)
{
//pobieramy obiekt HttpClient, dzięki któremu został wysłany request
//przekazaliśmy ten obiekt jako state
var client = result.AsyncState as HttpClient;
//pobieramy odpowiedź
HttpResponseMessage resp = null;
StatusesList statuses = null;
BlipCommunicationState state = BlipCommunicationState.OK;
Exception exp = null;
HttpStatusCode httpCode = HttpStatusCode.OK;
try
{
lock (httpAsyncClientLock)
{
resp = client.EndSend(result);
}
httpCode = resp.StatusCode;
resp.EnsureStatusIsSuccessful();
//deserializujemy z json
if (resp.Content.GetLength() > 2)
{
statuses = resp.Content.ReadAsJsonDataContract<StatusesList>();
}
}
catch (HttpStageProcessingException stageEx)
{
state = BlipCommunicationState.CommunicationError;
if (resp != null) httpCode = resp.StatusCode;
}
catch (ArgumentOutOfRangeException aorEx)
{
state = BlipCommunicationState.CommunicationError;
httpCode = resp.StatusCode;
//gdy wystąpiły jakieś błędy w komunikacji
}
catch (SerializationException serEx)
{
state = BlipCommunicationState.CommunicationError | BlipCommunicationState.Error;
httpCode = resp.StatusCode;
exp = serEx;
}
catch (Exception ex)
{
state = BlipCommunicationState.Error;
exp = ex;
}
finally
{
if (resp != null)
{
resp.Dispose();
resp = null;
}
}
if (((state & BlipCommunicationState.CommunicationError) == BlipCommunicationState.CommunicationError)
&& CommunicationError != null)
{
CommunicationError(null, new CommunicationErrorEventArgs(httpCode, BlipActions.AfterLoad));
return;
}
if ((state & BlipCommunicationState.Error) == BlipCommunicationState.Error && ExceptionOccure != null)
{
ExceptionOccure(null, new ExceptionEventArgs(exp));
return;
}
if (statuses == null)
return;
//gdy zostały zwrócone jakieś statusy
if ((statuses.Count > 0) && (StatusesLoaded != null))
{
StatusesLoaded(null, new StatusesLoadingEventArgs(statuses));
}
}