private void AfterAddStatusAsync(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;
HttpResponseMessage resp = null;
StatusesList statuses = null;
BlipCommunicationState state = BlipCommunicationState.OK;
Exception exp = null;
HttpStatusCode httpCode = HttpStatusCode.OK;
try
{
//pobieramy odpowiedź
lock (httpAsyncClientLock)
{
resp = client.EndSend(result);
}
//sprawdź czy odpowiedź jest poprawna
//resp.EnsureStatusIsSuccessful();
resp.EnsureStatusIs(201, new int[] {200, 204});
//jeżeli status ok to znaczy że dodano status
httpCode = resp.StatusCode;
}
catch (HttpStageProcessingException stageEx)
{
state = BlipCommunicationState.CommunicationError;
if (resp != null)
httpCode = resp.StatusCode;
}
catch (ArgumentOutOfRangeException aorEx)
{
//gdy wystąpiły jakieś błędy w komunikacji
state = BlipCommunicationState.CommunicationError;
if (resp != null)
httpCode = resp.StatusCode;
}
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.AfterStatusAdd));
return;
}
if ((state & BlipCommunicationState.Error) == BlipCommunicationState.Error && ExceptionOccure != null)
{
ExceptionOccure(null, new ExceptionEventArgs(exp));
return;
}
//zgłaszamy zdarzenie że dane załadowaliśmy, przekazując nasze parametry zgłosznie wraz z statusami
if (StatusesAdded != null)
{
StatusesAdded(null, EventArgs.Empty);
}
}