Skip to content

Commit

Permalink
Add purge files call
Browse files Browse the repository at this point in the history
  • Loading branch information
HackAttack committed Apr 25, 2017
1 parent 7845050 commit f3664d0
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mhackner.cloudflare

import groovyx.net.http.AsyncHTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.HttpResponseException
import groovyx.net.http.Method

Expand Down Expand Up @@ -77,6 +78,15 @@ class AsyncCloudFlareClient {
}
}

Future<Map> purgeCache(String zoneId, Collection<String> urls = null, Collection<String> tags = null) {
def body = urls || tags ? [files: urls, tags: tags].findAll { it.value } : [purge_everything: true]
new ResultExtractor(http.doRequest(new HTTPBuilder.RequestConfigDelegate(
http,
[path: "zones/$zoneId/purge_cache", body: body],
new HttpDeleteWithBody(),
null)))
}

private static class ResultExtractor {
@Delegate
Future delegate
Expand Down
10 changes: 10 additions & 0 deletions src/main/groovy/com/mhackner/cloudflare/CloudFlareClient.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mhackner.cloudflare

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient

class CloudFlareClient {
Expand Down Expand Up @@ -64,4 +65,13 @@ class CloudFlareClient {
http.put(path: "zones/$record.zone_id/dns_records/$record.id", body: record).data.result
}

Map purgeCache(String zoneId, Collection<String> urls = null, Collection<String> tags = null) {
def body = urls || tags ? [files: urls, tags: tags].findAll { it.value } : [purge_everything: true]
http.doRequest(new HTTPBuilder.RequestConfigDelegate(
http,
[path: "zones/$zoneId/purge_cache", body: body],
new HttpDeleteWithBody(),
null)).data.result
}

}
23 changes: 23 additions & 0 deletions src/main/groovy/com/mhackner/cloudflare/HttpDeleteWithBody.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mhackner.cloudflare

import groovy.transform.PackageScope

import org.apache.http.annotation.NotThreadSafe
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase

@NotThreadSafe
@PackageScope
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {

public static final String METHOD_NAME = 'DELETE'

HttpDeleteWithBody() {
super()
}

@Override
String getMethod() {
METHOD_NAME
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,49 @@ class CloudFlareClientTest {
}''')))
}

@Test
void purgeCacheAll() {
client.purgeCache('023e105f4ecef8ad9ca31a8372d0c353')
asyncClient.purgeCache('023e105f4ecef8ad9ca31a8372d0c353').get()
verify(2, deleteRequestedFor(urlEqualTo('/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache'))
.withRequestBody(equalToJson('''
{
"purge_everything": true
}''')))
}

@Test
void purgeCacheFiles() {
client.purgeCache('023e105f4ecef8ad9ca31a8372d0c353', ['http://www.example.com/css/styles.css'])
asyncClient.purgeCache('023e105f4ecef8ad9ca31a8372d0c353', ['http://www.example.com/css/styles.css']).get()
verify(2, deleteRequestedFor(urlEqualTo('/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache'))
.withRequestBody(equalToJson('''
{
"files": ["http://www.example.com/css/styles.css"]
}''')))
}

@Test
void purgeCacheTags() {
client.purgeCache('023e105f4ecef8ad9ca31a8372d0c353', null, ['tag'])
asyncClient.purgeCache('023e105f4ecef8ad9ca31a8372d0c353', null, ['tag']).get()
verify(2, deleteRequestedFor(urlEqualTo('/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache'))
.withRequestBody(equalToJson('''
{
"tags": ["tag"]
}''')))
}

@Test
void purgeCacheBoth() {
client.purgeCache('023e105f4ecef8ad9ca31a8372d0c353', ['http://www.example.com/css/styles.css'], ['tag'])
asyncClient.purgeCache('023e105f4ecef8ad9ca31a8372d0c353', ['http://www.example.com/css/styles.css'], ['tag']).get()
verify(2, deleteRequestedFor(urlEqualTo('/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache'))
.withRequestBody(equalToJson('''
{
"files": ["http://www.example.com/css/styles.css"],
"tags": ["tag"]
}''')))
}

}
23 changes: 23 additions & 0 deletions src/test/resources/mappings/purgeCacheAll.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"request": {
"method": "DELETE",
"url": "/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache",
"bodyPatterns": [{
"equalToJson": "{ \"purge_everything\": true }"
}]
},
"response": {
"status": 200,
"jsonBody": {
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "023e105f4ecef8ad9ca31a8372d0c353"
}
},
"headers": {
"Content-Type": "application/json"
}
}
}
23 changes: 23 additions & 0 deletions src/test/resources/mappings/purgeCacheBoth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"request": {
"method": "DELETE",
"url": "/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache",
"bodyPatterns": [{
"equalToJson": "{ \"files\": [\"http://www.example.com/css/styles.css\"], \"tags\": [\"tag\"] }"
}]
},
"response": {
"status": 200,
"jsonBody": {
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "023e105f4ecef8ad9ca31a8372d0c353"
}
},
"headers": {
"Content-Type": "application/json"
}
}
}
23 changes: 23 additions & 0 deletions src/test/resources/mappings/purgeCacheFiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"request": {
"method": "DELETE",
"url": "/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache",
"bodyPatterns": [{
"equalToJson": "{ \"files\": [\"http://www.example.com/css/styles.css\"] }"
}]
},
"response": {
"status": 200,
"jsonBody": {
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "023e105f4ecef8ad9ca31a8372d0c353"
}
},
"headers": {
"Content-Type": "application/json"
}
}
}
23 changes: 23 additions & 0 deletions src/test/resources/mappings/purgeCacheTags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"request": {
"method": "DELETE",
"url": "/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache",
"bodyPatterns": [{
"equalToJson": "{ \"tags\": [\"tag\"] }"
}]
},
"response": {
"status": 200,
"jsonBody": {
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "023e105f4ecef8ad9ca31a8372d0c353"
}
},
"headers": {
"Content-Type": "application/json"
}
}
}

0 comments on commit f3664d0

Please sign in to comment.