Skip to content

Commit

Permalink
Resolves #2
Browse files Browse the repository at this point in the history
  • Loading branch information
WillStrohl committed Sep 9, 2020
1 parent f236d3b commit f57d0cc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 1 addition & 9 deletions Modules/CloudFlareClearCache/Models/CloudFlareResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -48,15 +47,9 @@ public class CloudFlareResponse : ICloudFlareResponse, IDisposable
[DataMember(Name = "messages")]
[JsonProperty(PropertyName = "messages")]
public List<string> Messages { get; set; }

public void Dispose()
{
// do nothing
}
}

[DataContract]
[JsonObject(MemberSerialization.OptIn)]
[Serializable]
public class CloudFlareResult : ICloudFlareResult
{
Expand All @@ -66,7 +59,6 @@ public class CloudFlareResult : ICloudFlareResult
}

[DataContract]
[JsonObject(MemberSerialization.OptIn)]
[Serializable]
public class CloudFlareError : ICloudFlareError
{
Expand Down
52 changes: 52 additions & 0 deletions Modules/CloudFlareClearCache/Services/ServiceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -49,6 +50,12 @@ public static T PostRequest<T>(string uri, string data, string contentType = "",
return result;
}

public static ServiceResponse<CloudFlareResponse> PostRequest(string uri, string data, string contentType = "", Dictionary<string, string> customHeaders = null)
{
var result = SendWithData(uri, "POST", data, contentType, customHeaders);
return result;
}

public static T PutRequest<T>(string uri, string data)
where T : class, new()
{
Expand Down Expand Up @@ -106,6 +113,7 @@ private static T SendWithData<T>(string uri, string method, string data, string
Logger.Debug(response.ToString());

var result = JsonHelper.ObjectFromJson<T>(response);
//var result = DotNetNuke.Common.Utilities.Json.Deserialize<T>(response);

return result;
}
Expand All @@ -125,6 +133,37 @@ private static T SendWithData<T>(string uri, string method, string data, string
}
}

private static ServiceResponse<CloudFlareResponse> SendWithData(string uri, string method, string data, string contentType = "", Dictionary<string, string> customHeaders = null)
{
try
{
var strResponse = SendRequest(uri, method, data, contentType, customHeaders);

var oResult = new ServiceResponse<CloudFlareResponse>();

//Logger.Debug(response.ToString());

oResult.Content= JsonHelper.ObjectFromJson<CloudFlareResponse>(strResponse);
//var result = DotNetNuke.Common.Utilities.Json.Deserialize<T>(response);

return oResult;
}
catch (Exception ex)
{
LogError(ex);

var result = new ServiceResponse<CloudFlareResponse>();
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<string,string> customHeaders = null, WebProxy proxy = null, int timeout = DefaultTimeout)
{
WebResponse objResp;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion Modules/CloudFlareClearCache/Services/ServiceProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public ServiceResponse<CloudFlareResponse> PurgeCache(string zoneId, Dictionary<

Logger.Debug($"Calling: {fullRequestUri}");

result = ServiceHelper.PostRequest<ServiceResponse<CloudFlareResponse>>(fullRequestUri, "{ \"purge_everything\" : true }", "application/json", customHeaders);
//result = ServiceHelper.PostRequest<ServiceResponse<CloudFlareResponse>>(fullRequestUri, "{ \"purge_everything\" : true }", "application/json", customHeaders);
result = ServiceHelper.PostRequest(fullRequestUri, "{ \"purge_everything\" : true }", "application/json", customHeaders);

return result;
}
Expand Down
74 changes: 1 addition & 73 deletions Modules/CloudFlareClearCache/Views/ClearCache/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,76 +36,4 @@
}
</div>
</div>
</div>
@if (ViewBag.ReadyToUse)
{
<script type="text/javascript">
var $lnkSubmit = {};
var $divSuccess = {};
var $divFailure = {};
var $pFailure = {};
(function ($) {
$(document).ready(function() {
$lnkSubmit = $("#lnkSubmit");
$divSuccess = $("#divSuccess");
$divFailure = $("#divFailure");
$pFailure = $("#pFailure");
/*
$divSuccess.hide();
$divFailure.hide();
$lnkSubmit.on("click",
function(e) {
e.preventDefault();
$.ajax({
url: "https://api.cloudflare.com/client/v4/zones/@Model.Config.ZoneID/purge_cache",
type: "post",
headers: {
"X-Auth-Email": "@Model.Config.Email",
"X-Auth-Key": "@Model.Config.ApiToken",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "https://api.cloudflare.com",
"Access-Control-Allow-Headers": "*"
},
crossDomain: true,
data: { "purge_everything": true },
dataType: "json",
success: function (jqXHR, textStatus) {
var response = JSON.parse(data);
console.log(textStatus);
console.log(response);
if (response.success){
showSuccess();
}
else {
$pFailure.text(response.errors[0].message);
showFailure();
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
$pFailure.text(errorThrown);
showFailure();
},
complete: function (jqXHR, textStatus) {
console.log(textStatus);
return false;
}
});
});*/
});
})(jQuery);
function showSuccess() {
$divSuccess.show();
$divFailure.hide();
}
function showFailure() {
$divSuccess.hide();
$divFailure.show();
}
</script>
}
</div>

0 comments on commit f57d0cc

Please sign in to comment.