[ckan-dev] [ckan-global-user-group] CKAN java library

Matthew Fullerton matt.fullerton at gmail.com
Mon Jun 22 11:26:50 UTC 2015


ckan-dev in cc.

Hmm, it might be simpler to just write a Java wrapper around
https://github.com/ckan/ckanapi using Jython (http://www.jython.org/)

However, if you want to start from scratch, I can maybe give you a little
help to get started; this is how we access the API for the few things we
need from inside Java (url is the API endpoint, you can include url
arguments; apiKey is your API key, needed if the dataset is private):

package package.name;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.json.JSONException;
import org.json.JSONObject;

public class CkanApiClient {
    private JSONObject getJsonResultForResource(String url, String apiKey)
throws JSONException {
        JSONObject resourceProperties = null;
        Client client = ClientBuilder.newClient();
        WebTarget resourceTarget = client.target(url);
        //Set API key
        Response response =
resourceTarget.request(MediaType.APPLICATION_JSON).header("X-CKAN-API-Key",
apiKey).get();
        String responseString = response.readEntity(String.class);
        resourceProperties = new JSONObject(responseString);
        return resourceProperties;
    }
}

What's not in there is sending a JSON body which you will need for e.g.
creating datasets.

Best,
Matt

--
Matthew Fullerton
Freelance Software Developer und EXIST Stipend holder with the start up
project "Tapestry" - http://www.smartlane.de/


On 19 June 2015 at 23:33, Skaros Ilias <skaros.ilias at gmail.com> wrote:

> Hi all,
> I am trying to create a java application that would eventually get use of
> the API and mainly upload files(csv and zip) to a ckan
> installation(currently a local installation under a VM).
> The problem is that i cant find any libraries that would help me do this.
> I tried to do it manually, using json and http commands but i hit a wall
> (more than once)
> I have found some that claim to be java libs, but either i cant get them
> to work, or they are not what they say they are.
>
> Mostly i think ckan clients are written in Python, the problem is that i
> dont know python. I would really love if there was a library that i could
> just use and do all the hard work for me.
>
> In case someone might be able to help this is the code I did in java
>
> static String myApiKey="fa0499d1-ffda-4590-82b3-4afdb9c91576";
> static String uploadFileName="/home/ilias/2013/05/csv/2013_05_15.csv"; public static void uploadFile()
>
>  { HttpClient httpclient = new DefaultHttpClient();
>
>      Date now=new Date();
>      File file = new File(uploadFileName);
>      httpclient = new DefaultHttpClient();
>      try {
>
>          SimpleDateFormat dateFormatGmt = new  SimpleDateFormat("yyyyMMMddHHmmss");
>          dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
>          String date=dateFormatGmt.format(new Date());
>    /*      First we specify the data             */
>           HttpEntity reqEntity = MultipartEntityBuilder.create()
>             .addTextBody("package_id","test2")
>             .addTextBody("url",page)
>             .setCharset(Charset.forName("utf-8"))
>             .addTextBody("file", date +"/"+uploadFileName)
>             .build();
>   /*    Set up the headers       */
>         HttpPost postRequest = new HttpPost(page+"/api/action/resource_create/");
>         postRequest.setEntity(reqEntity);
>         postRequest.setHeader("X-CKAN-API-Key", myApiKey);
>  /*     execute       */
>
>         System.out.println("request2: "+postRequest.toString()+"\n"+postRequest.getRequestLine().toString());
>
>         Header[]a=postRequest.getAllHeaders();
>         HttpResponse response = httpclient.execute(postRequest);/*     get respond    */
>        int statusCode = response.getStatusLine().getStatusCode();
>         BufferedReader br = new BufferedReader(
>         new InputStreamReader((response.getEntity().getContent())));
>
>         String line;
>         while ((line = br.readLine()) != null) {
>                 System.out.println("__"+line);}if(statusCode!=200){
>    System.out.println("statusCode ==" +statusCode);}}catch (IOException ioe) {System.out.println(ioe);
>  } finally {
> httpclient.getConnectionManager().shutdown();}
> /*Upload the file */
>   file = new File(uploadFileName);
> httpclient = new DefaultHttpClient();try {
>        FileBody bin = new FileBody(file,ContentType.TEXT_PLAIN,now.toString()+file.getName());
> /*   add the data     */
>        HttpEntity reqEntity = MultipartEntityBuilder.create()
>         .addPart("file", bin)
>         .addTextBody("key", now.toString()+file.getName())
>         .build();
>
>        HttpPost postRequest = new HttpPost(page+"/api/action/resource_create/");
>        postRequest.setEntity(reqEntity);
>        postRequest.setHeader("X-CKAN-API-Key", myApiKey);
>        HttpResponse response = httpclient.execute(postRequest);
>        int statusCode = response.getStatusLine().getStatusCode();
>        BufferedReader br = new BufferedReader(
>                new InputStreamReader((response.getEntity().getContent())));
>
>        String line;
>        while ((line = br.readLine()) != null) {
>          System.out.println("+"+line);
>        }
>        if(statusCode!=200){
>           System.out.println("statusCode =!=" +statusCode);
>        }}catch (IOException ioe) {System.out.println(ioe);} finally {
> httpclient.getConnectionManager().shutdown();}}
>
>
> And the error i was getting is a 301 Error
>
> I also tried this, using httpGet
> URIBuilder builder = new URIBuilder();
>         builder.setScheme("http").setHost("192.168.1.1:5000").setPath("/api/action/resource_create/")
>          .setCharset(Charset.forName("utf-8"))
>             .setParameter("package_id", "test2")
>             .setParameter("url", "http://www.example.com");
>
> URI uri = builder.build();HttpGet postRequest = new HttpGet(uri);
> postRequest.setHeader("X-CKAN-API-Key", myApiKey);
>
> Which returned a more understandable error of
>
> "Bad request - JSON Error: No request body data"
>>
> Any help/guideline is appreciated
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "CKAN Global User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ckan-global-user-group+unsubscribe at googlegroups.com.
> To post to this group, send email to
> ckan-global-user-group at googlegroups.com.
> Visit this group at http://groups.google.com/group/ckan-global-user-group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com
> <https://groups.google.com/d/msgid/ckan-global-user-group/921a86fd-38e1-4701-97d7-8ae1256fb7b7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.okfn.org/pipermail/ckan-dev/attachments/20150622/b097028c/attachment-0002.html>


More information about the ckan-dev mailing list