Categories
Javascript

REST access in AngularJS using ngResource

This will be a short and quick walk through on how to make REST API calls using AngularJS. We shall use ngResource to aid us.

Loading ngResource

If we want to use the ngResource module, we have to make sure it’s being loaded separately along with angular. We need to have something like –

Creating a Module and Resource Objects (Mapping)

Now, we shall create a module named “apiService” to keep our REST stuff separate from our main application logic.

Then we shall map REST api calls to our Angular resource objects. Here we create a Booking resource object and the factory method allows us to do that –

Here we create a new resource object which maps to the API url – “/api/booking” with an optional parameter “Id”. Then we define a set of default parameters to be used, here we instruct to extract the value of “:Id” if “Id” is present in the set of params passed.

The above resource object can make these requests:

GET /api/booking/ — Gets all booking
GET /api/booking/1 — Gets the booking with ID 1
POST /api/booking/ — Creates a new booking
PUT /api/booking/1 — Update booking ID 1
DELETE /api/booking/1 — Delete booking ID 1

Besides the basic methods, we have also added a custom method named “reviews” which would add “reviews_only=true” to make a GET query and return the result as an array. This is helpful to deal with custom queries.

Making Rest Calls

Here comes the fun, how do we use the Booking object to make the different calls?

When making the calls, we need to remember that the first parameter contains the actual data. These data will be used as the parameters of the API. This can be appended like query strings or POSTed. The API will use these data to filter the response. Where as the second parameter overrides the default parameters for the resource definition. It is used by Angular to decide how to make the REST call. In our case, we used – “/api/booking/:Id” as our api entry point and then defined default parameters as – {Id: “@Id” }. So here, the second parameter will help angular decide which URL (by providing the value for Id) to make the request to. We didn’t need to pass any data but we did need to pass the ID in some methods. This is why most of the methods were passed an empty object as the first parameter.

This should explain things a bit more –

Each of these methods allow a callback function to be executed when there is a response from server.

Simple, isn’t it?

Categories
Python

Django on Heroku: Postgres and dbshell

If you’re deploying a Django app on Heroku you should already know that Heroku uses Postgres. The surprise comes when your “dbshell” command will fail with an error message like:

You appear not to have the ‘psql’ program installed or on your path.

The workaround is rather simple, you have to copy the SQL manually and run it from the heroku pgsql console. To run the console, type in –

And you should get a prompt to type in your pgsql queries. Type in your SQL and commit. It should work! 🙂

Categories
Mac Python

Python: Sending Growl Notifications

Growl is an extremely popular notification system for OS X. The application allows developers to display beautiful notification messages with ease. If you’re developing an application in Python and would like to integrate with Growl, this blog post is for you.

Introducing GNTP

Let me quote from the official docs here – http://www.growlforwindows.com/gfw/help/gntp.aspx#intro:

The Growl Network Transport Protocol, or GNTP, is a protocol to allow two-way communication between applications and centralized notification systems such as Growl for Mac OS X and to allow two-way communication between two machines running centralized notification systems for notification forwarding purposes.

In short, GNTP allows two way communication between your application and Growl. Even shorter – you can use GNTP to publish notifications using Growl.

GNTP Binding for Python

We have an excellent library for interacting with GNTP. The project is hosted on Github – https://github.com/kfdm/gntp/ and the docs are available here – http://pythonhosted.org/gntp/

So how to use this library? First, install the library using pip –

To see if the installation was ok, we can test by running the module directly –

Sending Notifications

Sending a quick notification from the Python interactive prompt:

Simple, right?

You can find advanced examples on the github readme and the docs (links above). Have fun with Python and Growl!