public Notification <HipChatResult> Notify(HipChatMessage hipChatMessage) { try { var notification = new Notification <HipChatResult>(); foreach (var partialMessage in hipChatMessage.Message.InSetsOf(5000)) { var message = HttpUtility.UrlEncode(new string(partialMessage.ToArray())); var content = "room_id=" + HttpUtility.UrlEncode(hipChatMessage.RoomId) + "&from=" + HttpUtility.UrlEncode(hipChatMessage.From) + "&message=" + message + "&color=" + hipChatMessage.Color.OrDefault().Key + "&message_format=" + hipChatMessage.MessageFormat.OrDefault().Key + "¬ify=" + (hipChatMessage.Notify ? 1 : 0); var result = _webServiceClient.Post(string.Format(ApiMessageRoomUrl, hipChatMessage.ApiKey), content, "application/x-www-form-urlencoded"); if (!result.IsValid) { notification.Add(result); break; } var r = JsonUtility.Deserialize <HipChatResult>(result); notification.Item = r; } return(notification); } catch (WebException exception) { // from http://stackoverflow.com/a/1140193/102536 if (exception.Response != null) { if (exception.Response.ContentLength != 0) { using (var stream = exception.Response.GetResponseStream()) { // ReSharper disable once AssignNullToNotNullAttribute using (var reader = new StreamReader(stream)) { var message = reader.ReadToEnd(); return(new Notification <HipChatResult>(Notification.ErrorFor(message))); } } } } return(new Notification <HipChatResult>(Notification.ErrorFor(exception.ToString()))); } catch (Exception exception) { return(new Notification <HipChatResult>(Notification.ErrorFor(exception.ToString()))); } }
public async Task <TResp> InvokeAsync <TReq, TResp>(string url, TReq req) where TReq : BaseReq where TResp : BaseResp, new() { var requestString = JsonConvert.SerializeObject(req, Formatting.Indented); var reqArgs = new InvokeEventArgs { Request = requestString }; OnInvokeBegan(reqArgs); _logger.Log(requestString, Category.Info, Priority.Medium); var stopwatch = new Stopwatch(); stopwatch.Start(); TResp resp = null; try { resp = await _webServiceClient.Post <TReq, TResp>(url, req); } catch (Exception e) { var sb = new StringBuilder(); sb.AppendLine(e.Message); sb.AppendLine(); sb.AppendLine(e.StackTrace); var errorArgs = new InvokeEventArgs { Response = sb.ToString(), OperationTime = stopwatch.ElapsedMilliseconds }; _logger.Log(sb.ToString(), Category.Info, Priority.Medium); OnInvokeFinished(errorArgs); return(resp); } stopwatch.Stop(); var responseString = JsonConvert.SerializeObject(resp, Formatting.Indented); var respArgs = new InvokeEventArgs { Response = responseString, OperationTime = stopwatch.ElapsedMilliseconds }; _logger.Log(responseString, Category.Info, Priority.Medium); _logger.Log($"{url}: {stopwatch.ElapsedMilliseconds} ms", Category.Info, Priority.Medium); OnInvokeFinished(respArgs); return(resp); }