Skip to content

Instantly share code, notes, and snippets.

@marsyang1
Last active August 21, 2020 04:20
Show Gist options
  • Save marsyang1/144b5d939188d6441412db0179fb2ff1 to your computer and use it in GitHub Desktop.
Save marsyang1/144b5d939188d6441412db0179fb2ff1 to your computer and use it in GitHub Desktop.

Akamai doc is really hard to understand.

Reference

Using https://start.spring.io/ to start project and add depenedency Library

build.gradle

...
dependencies {
	implementation 'com.google.guava:guava:28.1-jre'
	implementation 'com.akamai.edgegrid:edgegrid-signer-rest-assured:4.0.1'
	implementation 'com.akamai.edgegrid:edgegrid-signer-google-http-client:4.0.1'
	implementation 'com.alibaba:fastjson:1.2.73'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Using RestAssured

    @Test
    public void purgeCacheByUrlAndRestAssured() {
        final String hostname = "";
        final String token = "";
        final String secret = "";
        final String clientToken = "";
        final ClientCredential clientCreditial = ClientCredential.builder()
                .host(hostname)
                .accessToken(token)
                .clientToken(clientToken)
                .clientSecret(secret)
                .build();
        final List<String> urlList = ImmutableList.of(
                "https://{{ TARGET_PURGE_IMAGE_URL }}"
        );
        final String jsonBody = new JSONObject()
                .fluentPut("objects", urlList).toJSONString();
        RestAssured
                .given()
                .relaxedHTTPSValidation()
                .urlEncodingEnabled(false)
                .config(RestAssured.config()
                        .encoderConfig(RestAssured.config().getEncoderConfig()
                                .appendDefaultContentCharsetToContentTypeIfUndefined(false)))
                .contentType("application/json")
                .filter(new RestAssuredEdgeGridFilter(clientCreditial))
                .body(jsonBody)
                .when()
                .post("/ccu/v3/invalidate/url/production")
                .then()
                .statusCode(201);
    }

Using Google Http Api

    @Test
    public void purgeCacheByImageUrlAndGoogleApi() throws IOException, RequestSigningException {
        final String hostname = "";
        final String token = "";
        final String secret = "";
        final String clientToken = "";
        final ClientCredential clientCreditial = ClientCredential.builder()
                .host(hostname)
                .accessToken(token)
                .clientToken(clientToken)
                .clientSecret(secret)
                .build();
        final List<String> urlList = ImmutableList.of(
                "https://{{ TARGET_PURGE_IMAGE_URL }}"
        );
        final String jsonBody = new JSONObject()
                .fluentPut("objects", urlList).toJSONString();
        final String url = String.format("https://%s/ccu/v3/invalidate/url/production", hostname);
        HttpTransport httpTransport = new ApacheHttpTransport();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
        URI uri = URI.create(url);
        HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", jsonBody));
        GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCreditial);
        requestSigner.sign(request);
        HttpResponse response = request.execute();
        System.out.println(response.getStatusCode()); // response status code
        String text = null;
        try (Scanner scanner = new Scanner(response.getContent(), StandardCharsets.UTF_8.name())) {
            text = scanner.useDelimiter("\\A").next();
        }
        System.out.println(text); // response body

    }

Using Google Http Api and purge all site by CpCode

    
    @Test
    public void cleanCacheByCpCodeAndGoogleApi() throws IOException, RequestSigningException {
        final String hostname = "";
        final String token = "";
        final String secret = "";
        final String clientToken = "";
        final String cpcode = "{{ YOUR_CPCODE }}";
        final ClientCredential clientCreditial = ClientCredential.builder()
                .host(hostname)
                .accessToken(token)
                .clientToken(clientToken)
                .clientSecret(secret)
                .build();
        final List<String> urlList = ImmutableList.of(cpcode);
        final String jsonBody = new JSONObject()
                .fluentPut("objects", urlList).toJSONString();
        final String url = String.format("https://%s/ccu/v3/invalidate/cpcode/production", hostname);
        HttpTransport httpTransport = new ApacheHttpTransport();
        HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
        URI uri = URI.create(url);
        HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(uri), ByteArrayContent.fromString("application/json", jsonBody));
        GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(clientCreditial);
        requestSigner.sign(request);
        HttpResponse response = request.execute();
        System.out.println(response.getStatusCode()); // response status code
        String text = null;
        try (Scanner scanner = new Scanner(response.getContent(), StandardCharsets.UTF_8.name())) {
            text = scanner.useDelimiter("\\A").next();
        }
        System.out.println(text); // response body

    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment