private WebhookResponse AddWebhook(string url, string description) { string apiKey = GetMandrillApiKey(); string requestUrl = string.Format("{0}{1}", MandrillApiUrl, "/webhooks/add.json"); var requestBodyObject = new WebhookApiAddRequest { key = apiKey, url = url, description = description, events = new string[] {} }; string requestBodyJson = new JavaScriptSerializer().Serialize(requestBodyObject); HttpWebRequest request = PrepareRequest(requestUrl); string response = ExecRequest(request, requestBodyJson); WebhookApiResponseError requestError = null; var requestResult = ConvertToWebhookResponse <WebhookApiResponse>(response, out requestError); var result = new WebhookResponse { Webhook = new List <WebhookApiResponse>() }; if (requestError != null || requestResult == null) { if (requestError != null) { Error = requestError.message; MandrillUtilities.Log.InfoFormat("AddWebhook error: {0}; {1}", requestError.name, requestError.message); } return(result); } result.Success = true; result.Webhook.Add(requestResult); return(result); }
private WebhookResponse DeleteWebhook(int id) { string apiKey = GetMandrillApiKey(); string requestUrl = string.Format("{0}{1}", MandrillApiUrl, "/webhooks/delete.json"); var requestBobyObject = new WebhookApiInfoRequest { key = apiKey, id = id }; string requestBodyJson = new JavaScriptSerializer().Serialize(requestBobyObject); HttpWebRequest request = PrepareRequest(requestUrl); string response = ExecRequest(request, requestBodyJson); WebhookApiResponseError requestError = null; var requestResult = ConvertToWebhookResponse <WebhookApiResponse>(response, out requestError); var result = new WebhookResponse { Webhook = new List <WebhookApiResponse>() }; if (requestError != null || requestResult == null) { if (requestError != null) { Error = requestError.message; MandrillUtilities.Log.InfoFormat("MandrillWebhookService.DeleteWebhook error: {0}; {1}.", requestError.name, requestError.message); } return(result); } result.Success = true; result.Webhook.Add(requestResult); return(result); }
private static T ConvertToWebhookResponse <T>(string dynamicObject, out WebhookApiResponseError error) where T : class, new() { error = null; var serializer = new JavaScriptSerializer(); try { var dynamicResult = serializer.Deserialize <WebhookApiResponseError>(dynamicObject); if (!string.IsNullOrEmpty(dynamicResult.status)) { error = dynamicResult; return(null); } } catch { } try { var dynamicResult = serializer.Deserialize <T>(dynamicObject); return(dynamicResult); } catch { MandrillUtilities.Log. InfoFormat("MandrillWebhookService.ConvertToWebhookResponse: can't convert response to {0}.", typeof(T).ToString()); } return(null); }