From f57d0cc21b67108b9afcab6eb68514b2e3c6cf30 Mon Sep 17 00:00:00 2001 From: Will Strohl Date: Tue, 8 Sep 2020 19:06:22 -0700 Subject: [PATCH] Resolves #2 --- .../Controllers/ClearCacheController.cs | 9 +-- .../Models/CloudFlareResponse.cs | 10 +-- .../Services/ServiceHelper.cs | 52 +++++++++++++ .../Services/ServiceProxy.cs | 3 +- .../Views/ClearCache/Index.cshtml | 74 +------------------ 5 files changed, 57 insertions(+), 91 deletions(-) diff --git a/Modules/CloudFlareClearCache/Controllers/ClearCacheController.cs b/Modules/CloudFlareClearCache/Controllers/ClearCacheController.cs index c655988..e968c43 100644 --- a/Modules/CloudFlareClearCache/Controllers/ClearCacheController.cs +++ b/Modules/CloudFlareClearCache/Controllers/ClearCacheController.cs @@ -107,14 +107,7 @@ public ActionResult Index(ClearCacheInfo model) //if (originalStatus.LastModifiedOnDate != currentStatus.LastModifiedOnDate) if (result) { - if (currentStatus.SettingValue == "true") - { - ViewBag.Status = "success"; - } - else - { - ViewBag.Status = "failure"; - } + ViewBag.Status = string.Equals(currentStatus.SettingValue, "true", StringComparison.OrdinalIgnoreCase) ? "success" : "failure"; } else { diff --git a/Modules/CloudFlareClearCache/Models/CloudFlareResponse.cs b/Modules/CloudFlareClearCache/Models/CloudFlareResponse.cs index 388f256..f5f3020 100644 --- a/Modules/CloudFlareClearCache/Models/CloudFlareResponse.cs +++ b/Modules/CloudFlareClearCache/Models/CloudFlareResponse.cs @@ -29,9 +29,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE namespace Upendo.Modules.CloudFlareClearCache.Models { [DataContract] - [JsonObject(MemberSerialization.OptIn)] [Serializable] - public class CloudFlareResponse : ICloudFlareResponse, IDisposable + public class CloudFlareResponse : ICloudFlareResponse { [DataMember(Name = "result")] [JsonProperty(PropertyName = "result")] @@ -48,15 +47,9 @@ public class CloudFlareResponse : ICloudFlareResponse, IDisposable [DataMember(Name = "messages")] [JsonProperty(PropertyName = "messages")] public List Messages { get; set; } - - public void Dispose() - { - // do nothing - } } [DataContract] - [JsonObject(MemberSerialization.OptIn)] [Serializable] public class CloudFlareResult : ICloudFlareResult { @@ -66,7 +59,6 @@ public class CloudFlareResult : ICloudFlareResult } [DataContract] - [JsonObject(MemberSerialization.OptIn)] [Serializable] public class CloudFlareError : ICloudFlareError { diff --git a/Modules/CloudFlareClearCache/Services/ServiceHelper.cs b/Modules/CloudFlareClearCache/Services/ServiceHelper.cs index f46f3c4..9e5f71f 100644 --- a/Modules/CloudFlareClearCache/Services/ServiceHelper.cs +++ b/Modules/CloudFlareClearCache/Services/ServiceHelper.cs @@ -28,6 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE using System.Text; using DotNetNuke.Instrumentation; using Upendo.Modules.CloudFlareClearCache.Components; +using Upendo.Modules.CloudFlareClearCache.Models; namespace Upendo.Modules.CloudFlareClearCache.Services { @@ -49,6 +50,12 @@ public static T PostRequest(string uri, string data, string contentType = "", return result; } + public static ServiceResponse PostRequest(string uri, string data, string contentType = "", Dictionary customHeaders = null) + { + var result = SendWithData(uri, "POST", data, contentType, customHeaders); + return result; + } + public static T PutRequest(string uri, string data) where T : class, new() { @@ -106,6 +113,7 @@ private static T SendWithData(string uri, string method, string data, string Logger.Debug(response.ToString()); var result = JsonHelper.ObjectFromJson(response); + //var result = DotNetNuke.Common.Utilities.Json.Deserialize(response); return result; } @@ -125,6 +133,37 @@ private static T SendWithData(string uri, string method, string data, string } } + private static ServiceResponse SendWithData(string uri, string method, string data, string contentType = "", Dictionary customHeaders = null) + { + try + { + var strResponse = SendRequest(uri, method, data, contentType, customHeaders); + + var oResult = new ServiceResponse(); + + //Logger.Debug(response.ToString()); + + oResult.Content= JsonHelper.ObjectFromJson(strResponse); + //var result = DotNetNuke.Common.Utilities.Json.Deserialize(response); + + return oResult; + } + catch (Exception ex) + { + LogError(ex); + + var result = new ServiceResponse(); + var apiResponse = result as IServiceResponse; + + if (apiResponse != null) + { + apiResponse.Errors.Add(new ServiceError("EXCEPTION", ex.Message + " | " + ex.StackTrace)); + } + + return result; + } + } + private static string SendRequest(string serviceUrl, string method, string data, string contentType = "", Dictionary customHeaders = null, WebProxy proxy = null, int timeout = DefaultTimeout) { WebResponse objResp; @@ -178,6 +217,19 @@ private static string SendRequest(string serviceUrl, string method, string data, objReq.Proxy = proxy; } + Logger.Debug($"Request RequestUri: {objReq.RequestUri}"); + Logger.Debug($"Request ContentType: {objReq.ContentType}"); + Logger.Debug($"Request Method: {objReq.Method}"); + for (int i = 0; i < objReq.Headers.Count; ++i) + { + string header = objReq.Headers.GetKey(i); + foreach (var value in objReq.Headers.GetValues(i)) + { + Logger.Debug($"Request Header Key: {header}"); + Logger.Debug($"Request Header Value: {value}"); + } + } + if (byteReq != null) { var OutStream = objReq.GetRequestStream(); diff --git a/Modules/CloudFlareClearCache/Services/ServiceProxy.cs b/Modules/CloudFlareClearCache/Services/ServiceProxy.cs index 6323454..820a56e 100644 --- a/Modules/CloudFlareClearCache/Services/ServiceProxy.cs +++ b/Modules/CloudFlareClearCache/Services/ServiceProxy.cs @@ -55,7 +55,8 @@ public ServiceResponse PurgeCache(string zoneId, Dictionary< Logger.Debug($"Calling: {fullRequestUri}"); - result = ServiceHelper.PostRequest>(fullRequestUri, "{ \"purge_everything\" : true }", "application/json", customHeaders); + //result = ServiceHelper.PostRequest>(fullRequestUri, "{ \"purge_everything\" : true }", "application/json", customHeaders); + result = ServiceHelper.PostRequest(fullRequestUri, "{ \"purge_everything\" : true }", "application/json", customHeaders); return result; } diff --git a/Modules/CloudFlareClearCache/Views/ClearCache/Index.cshtml b/Modules/CloudFlareClearCache/Views/ClearCache/Index.cshtml index 2d88c45..8ba9e4d 100644 --- a/Modules/CloudFlareClearCache/Views/ClearCache/Index.cshtml +++ b/Modules/CloudFlareClearCache/Views/ClearCache/Index.cshtml @@ -36,76 +36,4 @@ } - -@if (ViewBag.ReadyToUse) -{ - -} \ No newline at end of file + \ No newline at end of file