cURL examples – Testing your API

I found myself many times googling for a simple don’t-want-to-read-too-much about cURL examples to test my backend APIs. Since I always ended up reading the man page (and based on google’s first results doesn’t seems like other people did as well), here is my shortcut version for cURL examples. Let me explain you the flags I most commonly use first:

  • –connect-timeout: in case the domain becomes unresponsive (very unlikely if you are in localhost) I’ve set to wait only 1 second.
  • -m <seconds>: the max time in seconds I’m willing to wait for a response from my API (we don’t want the user becomes impatient)
  • -G: it allows to send data as query string via GET
  • <id>: represents an object ID in your API (no cURL related)

cURL GET calls

  • curl http://localhost/api/v1.0/method --connect-timeout 1 -m 10
  • curl http://localhost/api/v1.0/method --connect-timeout 1 -m 10 -Gd "param=value"

cURL POST call

  • curl http://localhost/api/v1.0/method --connect-timeout 1 -m 10 -d "param=value&param2=value2"

cURL DELETE call

  • curl -X DELETE http://localhost/api/v1.0/method/<id> --connect-timeout 1 -m 10 -d "param=value&param2=value2"

cURL UPDATE call

  • curl -X PUT http://localhost/api/v1.0/method/<id> --connect-timeout 1 -m 10 -d "param=value&param2=value2"

cURL man page: http://curl.haxx.se/docs/manpage.html

Python Logging – First Steps

Adding logging to your python script is as simple as adding these 2 lines:

1
2
import logging
logging.warning('Something went wrong!')

If you run this you’ll get this printed out in your console:

WARNING:root:Something went wrong!

This won’t prevent your script to keep running, it will just inform you that something went wrong at some place in your code where you explicitly wanted to show that message.

What about if I want to record this event in a log file?

You just need to specify the file name in the basicConfig method:

1
2
3
import logging
logging.basicConfig(filename='example.log')
logging.warning('Something went wrong!')

If you run your script again you won’t see the console output this time.

List your files where you have your python script and you will find a new file called “example.log”.

OK makes sense… and what about if I want to change the output format?

Let’s say the format of “WARNING:user:text” is too boring for you. How do I change it? How can I add, for example, the event’s date time?
To do so just add the format parameter:

1
2
3
4
import logging
logging.basicConfig(filename='example.log',
                    format='%(asctime)s %(name)-8s %(levelname)-8s %(message)s')
logging.warning('Something went wrong!')

Wait! what is that warning thing you’ve been adding?

The library has different logging levels. If you start to log everything that happens in your code it’s likely you will end with a huge file with tons of junk. You want a way to differentiate info events to those that were catastrophic.

Those levels are (in order of severity): DEBUG > INFO > WARNING > ERROR > CRITICAL. The default one is WARNING.

Why do you need these levels? Let’s say at development time you want to capture the DEBUG events. On production you might only want to log CRITICAL’s. So your production logging configuration will looks something like this:

1
2
3
4
5
6
import logging
logging.basicConfig(filename='example.log',
                    format='%(asctime)s %(name)-8s %(levelname)-8s %(message)s',
level=logging.CRITICAL)
logging.debug('Something went wrong! It is OK since this is production')
logging.critical('OMG! this should not happens on production')

As you might already know you only will see the second -critical- message on production.

There are many other -more sophisticated- things you can do with logging.
For example: using different logs files for different events, adding variables’ values to the messages, rotate the logs files based on time or file size, etc.

These are a few links you would find interesting if you want to learn more about the logging lib:

Interested in working on some amazing python projects? Drop me a line to leocelis.startups@gmail.com!

How to move a DIV with your fingers… and the Leap Motion

For those who don’t know Leap Motion is a 3D gesture controller that captures your hands and fingers movements.

Leap Motion

playing Jenga with the Leap Motion

One cool thing the Leap Motion guys did was to create a JavaScript Client Library which allows us to capture the device data in a html page.

Since I don’t have a glass monitor like in Minority Report or 3d holograms like Tony Stark has, I though I’d be cool to use a browser and the Leap Motion to manipulate DOM objects.

So let’s say we have a DIV like this one:

1
<div id="box" style="width: 200px; border: 1px solid #666666; padding: 10px 10px 70px 10px; display: inline-block;"></div>

And now we don’t want to move it with our thoughts because that’s impossible but instead with our hands:

First thing to do -obviously- is include the Leap Motion JS lib.

Secondly, we need to capture the frames data the device is sending us through the Leap.loop event function:

1
2
3
4
Leap.loop(function(frame)
{
    console.log("Frame:" + frame); 
});

Third step is to capture the current frames vectors [x,y,z] and use this information to manipulate DOM objects. The way to do this is to track the position derived from the motion between a first frame and the current frame:

1
2
3
4
5
6
7
8
9
var firstValidFrame = null
 
Leap.loop(function(frame)
{
    // use first frame as comparison
    if (!firstValidFrame) firstValidFrame = frame;
    var translation = frame.translation(firstValidFrame);
    console.log("X:" + translation.x);
});

Now the fun part! As you can see we could grab the x coordinate with translation.x (in millimiters) – on the same way we could do with the coordinates y and z.

By using the x coordinates we can manipulate the DIV marginLeft CSS property and simulate as if the user were moving the DIV with her hand.

If you have the Leap Motion try this full example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf8">
  <title></title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  <script src="leap.js"></script>
  </head>
  <body>
<script>
var firstValidFrame = null
 
Leap.loop(function(frame)
{
  // use the first frame to serve as comparison
  if (!firstValidFrame) firstValidFrame = frame;
  var translation = frame.translation(firstValidFrame);
  $('#box').css({marginLeft: translation.x});
});
</script>
 
  <div id="box" style="width: 200px; border: 1px solid #666666; padding: 10px 10px 70px 10px; display: inline-block"></div>
  </body>
</html>

Don’t have a Leap Motion? Pre order it!

Preventing parent page to reload after closing a dialog in jQuery Mobile

Long short story, my app flow is something like 1) user clicks on Send button, 2) app make a post to the same page and 3) user clicks on the newly created row and open a dialog window.

What happens is if you close the dialog jQuery Mobile will automatically reload the parent page. In my flow, resulting in inserting a new row.

The solution? data-dom-cache=”true”. You need to place that property in your div data-role=”page”. jQuery Mobile will cache your page and prevent dialogs reload any page after you close them.

More info: http://jquerymobile.com/test/docs/pages/page-cache.html

Install PyCrypto for Windows in less than 5 minutes

I just tried to install PyCrypto by running “easy_install.exe pip PyCrypto” and I got this pretty message:

error: Setup script exited with error: Unable to find vcvarsall.bat

So I googled about it and ran into some time expensive and crazy solutions like “Install Microsoft Visual Studio” or “Compile the package with mingw32″.

Fortunally there is a guy that already compiled it for us. You can download it here, install it and done!