API

Introduction

There are two seperate API calls. One is to get the raw JSON data. The other is to get html that you can place in a iframe to render 3D/2D previews.

  • /api/call/raw
  • /api/call/preview

Both calls use a RESTful approach and the same interface. They are both POST calls.

Raw JSON API

The raw JSON API returns back an array of JSON object representing the bins. Again, best way to learn it is to make a sample call and analyze the result. There are 3 parameters that need to be passed:

bins

bins should have the following format. Note that you can pass as many bins as you want by comma separating them.

id:weight_limit:size1xsize2xsize3

Here we have the following:

  • id = Should be unique for this bin
  • weight_limit = The weight limit you wish to impose on this bin
  • size1xsize2xsize3 = The height, width and length of the bin.

Example Bin:

0:50:5x5x5

items

items should have the following format. Note that you can pass as many items as you want by comma separating them.

id:constraints:weight:size1xsize2xsize3

Here we have the following:

  • id = Should be unique for this item
  • constraints = This is the constrains for item orientation. Setting this will prevent the algorithm from rotating the item in the associated direction. See below for details.
  • weight = the weight of this item
  • size1xsize2xsize3 = The height, width and length of the item.

0 = NO_CONSTRAINTS
1 = CONSTRAINT_WIDTH
2 = CONSTRAINT_HEIGHT
3 = CONSTRAINT_LENGTH
4 = CONSTRAINT_ALL

Example Item:

0:0:15:1x1x1

2D

For 2D, simply leave off last dimension.

0:0:15:1x1

3D Preview API

The 3D preview API displays a 3D preview of a single bin that is packed. While viewing this mode you can zoom and rotate the 3D scene to get a better view! It uses three.js as the rendering engine but it's all included in a single page so that you don't have to install anything. Just make the call and either display the returned html page in an iframe or display it directly.

The API call inherits from the Raw JSON API and add just one more post parameter

binId

binId is the id of the bin from the raw json that you wish to view. This of course means you have to call the Raw Json API first to get bins that you know have items in them

0

Limitations

  • Currently there is a limit of 500 bins + items combinations. Example would be 100 bins and 400 items. This can scale in the future.
  • For the time being there is a unit of measure limit of 10000. This can scale in the future.

Examples

The best way to learn an API is playing with sample code. Below you'll find them in some popular languages.

Java

                        HttpURLConnection conn;
                        URL addr = new URL("http://www.packit4me.com/api/call/raw");
                        conn = (HttpURLConnection) addr.openConnection();
                        conn.setRequestMethod("POST");
                        conn.setDoOutput(true);
                        conn.setReadTimeout(30000);
                        conn.connect();
                        OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());

                        String params = "bins=0:50:5x5x5&items=0:0:15:1x1x1";

                        osw.write(params);
                        osw.flush();
                        osw.close();

                        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        StringBuilder sb = new StringBuilder();
                        String line;
                        while ((line = rd.readLine()) != null) {

                            sb.append(line);
                        }

                        rd.close();
                        String result = sb.toString();
                        System.out.println(result);
                    

C#

                        string data;
                        System.Net.HttpWebRequest req = System.Net.WebRequest.Create(http://www.packit4me.com/api/call/raw") as System.Net.HttpWebRequest;

                        string queryParam = "bins=0:50:5x5x5&items=0:0:15:1x1x1";
                        byte[] postBytes = Encoding.ASCII.GetBytes(queryParam);
                        req.Method = "POST";
                        req.ContentType = "application/x-www-form-urlencoded";
                        req.ContentLength = postBytes.Length;

                        Stream postStream = req.GetRequestStream();
                        postStream.Write(postBytes, 0, postBytes.Length);
                        postStream.Close();

                        using (System.Net.HttpWebResponse response = req.GetResponse() as System.Net.HttpWebResponse)
                        {
                            StreamReader reader = new StreamReader(response.GetResponseStream());
                            data = reader.ReadToEnd();
                        }

                        return data;
                    

php

                        $url = "http://www.packit4me.com/api/call/raw";
                        $data = "bins=0:50:5x5x5&items=0:0:15:1x1x1";

                        $params = array('http' => array(
                                    'method' => 'POST',
                                    'content' => $data
                                  ));

                        $ctx = stream_context_create($params);
                        $fp = @fopen($url, 'rb', false, $ctx);
                        if (!$fp) {
                          throw new Exception("Problem with url");
                        }
                        $response = @stream_get_contents($fp);
                        if ($response === false) {
                          throw new Exception("Problem reading data from url");
                        }
                        echo $response;
                    

ruby

                        require 'net/http'

                        uri = URI.parse("http://www.packit4me.com/api/call/raw")
                        http = Net::HTTP.new(uri.host, uri.port)
                        response = http.post(uri.path, "bins=0:50:5x5x5&items=0:0:15:1x1x1")

                        puts response.body
                    

python

                        import httplib
                        import urllib

                        conn = httplib.HTTPConnection(host='www.packit4me.com', port=80)
                        params =  urllib.urlencode({'bins': '0:50:5x5x5', 'items': '0:0:15:1x1x1'})
                        headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
                        conn.request("POST", "/api/call/raw", params, headers)
                        content = conn.getresponse().read()
                        conn.close()

                        print content
                    

ruby (preview api)

                        require 'net/http'

                        uri = URI.parse("http://localhost:8080/packit4meweb/api/call/preview")
                        http = Net::HTTP.new(uri.host, uri.port)
                        response = http.post(uri.path, "bins=0:50:5x5x5&items=0:0:0:1x2x3,0:0:0:1x2x3,0:0:0:1x2x3&binId=0")

                        puts response.body
                    

Raw JSON Demo

3D Preview Demo