/// <summary> /// Sends a message. It returns upon success a <see cref="MessageResponse" /> object. /// </summary> /// <param name="mesg">Message object <see cref="Message" /></param> /// <returns> /// <see cref="MessageResponse" /> /// </returns> /// <exception cref="Exception">Exception with the appropriate message</exception> public MessageResponse SendMessage(Message mesg) { if (mesg == null) throw new Exception("Parameter 'mesg' cannot be null"); const string resource = "/messages/"; var stringWriter = new StringWriter(); new JsonSerializer().Serialize(stringWriter, mesg); const string contentType = "application/json"; HttpResponse response = RestClient.Post(resource, contentType, Encoding.UTF8.GetBytes(stringWriter.ToString())); if (response == null) throw new Exception("Request Failed. Unable to get server response"); if (response.Status == Convert.ToInt32(HttpStatusCode.Created)) return new MessageResponse(JsonConvert.DeserializeObject<ApiDictionary>(response.GetBodyAsString())); string errorMessage = String.Format("Status Code={0}, Message={1}", response.Status, response.GetBodyAsString()); throw new Exception("Request Failed : " + errorMessage); }
/// <summary> /// Sends a quick message. It returns upon success a <see cref="MessageResponse" /> object. /// </summary> /// <param name="from">Sender</param> /// <param name="to">Recipient</param> /// <param name="content">Message to send</param> /// <param name="registeredDelivery">Request Delivery Receipt</param> /// <param name="billingInfo">Billing Info</param> /// <returns> /// <see cref="MessageResponse" /> /// </returns> /// <exception cref="Exception">Exception with the appropriate message</exception> public MessageResponse SendQuickMessage(string from, string to, string content, bool registeredDelivery, string billingInfo = null) { var mesg = new Message {From = @from, Content = content, To = to, RegisteredDelivery = registeredDelivery, BillingInfo = billingInfo}; return SendMessage(mesg); }