Complete Example of Accessing The OPEN API From Groovy

Document created by Michael Schaefer on Oct 13, 2016Last modified by Michael Schaefer on Dec 11, 2017
Version 1Show Document
  • View in full screen mode

Below is a simple example of hitting the PAPI endpoint to get a list of Akamai contracts on our account. We have obfuscated our client secret and other security tokens. But you should be able to replace these dummy values with your own and get this work in a short amount of time. Hope this helps others get through this process quicker that we did.

 

First, here are the Maven dependencies we are using: 

<dependency>
   <groupId>org.codehaus.groovy</groupId>
   <artifactId>groovy-all</artifactId>
   <version>2.4.7</version>
</dependency>

<dependency>
   <groupId>com.akamai.edgegrid</groupId>
   <artifactId>edgegrid-signer-google-http-client</artifactId>
   <version>2.0.0-RC1</version>
</dependency>

<dependency>
   <groupId>org.codehaus.groovy.modules.http-builder</groupId>
   <artifactId>http-builder</artifactId>
   <version>0.7.1</version>
</dependency>

 

And here is the code: 

import com.akamai.edgegrid.signer.ClientCredential
import com.akamai.edgegrid.signer.EdgeGridV1Signer
import com.akamai.edgegrid.signer.Request
import groovyx.net.http.RESTClient

String host = "akab-*****-*****.luna.akamaiapis.net"

ClientCredential credential = ClientCredential.builder()
        .accessToken("akab-*****-*****")
        .clientToken("akab-*****-*****")
        .clientSecret("*****")
        .host( host )
        .build();

Request request = Request.builder()
        .method("GET")
        .uriWithQuery(URI.create( "https://${credential.getHost()}/papi/v0/contracts/" ))
        .build();

def restClient = new RESTClient( "https://${credential.getHost()}" )
String auth = new EdgeGridV1Signer().getSignature(request, credential)
restClient.getHeaders().put("authorization", auth)

try {
    def response = restClient.get([path: "/papi/v0/contracts/"])
    assert response != null
    println(response.data)
}
catch (Exception e) {
    e.printStackTrace()
}

 

The important thing to note here is how "host" is used. When creating the ClientCredential, they want just the host and not the "https" or the path. However, when building the Request object and the RESTClient objects, they both need the "https" and the Request object needs the full path of the call as well because apparently that is used when signing the request in EdgeGridV1Signer.getSignature(). This is what tripped us up for several days and we found no documentation that explained how this worked. So hopefully, this will help you get started more quickly. Best of luck.

 

~Michael

1 person found this helpful

Attachments

    Outcomes