1

Im building small web-service which will provide my users with data through API. Also, some data will be available right on my website.

The question is about how to use my own API? Should my website make a query to, for example, http://website.com/api/users/?format=json and then render data? Because I can use standard Django ORM features, but this is not corresponding with using of own API.

update: Imagine that I have a database full of games with release date, game name, game platform etc. And on my own website I will show this games as a list or a grid. Also, this data can be reached through API in JSON format, this data can be updated through API and even deleted. And on my own website I have html forms that allow me to do this actions in a user-friendly way. So how should I access my own API? Via POST request to my API or via django ORM directly to database?

5
  • Please tell us a little more about your architecture.
    – Marcel
    Commented Jun 5, 2013 at 13:41
  • Slightly offtopic: If you're trying to build a sort of "single page web application" (where all the data comes through API calls and all the pages are rendered on client side using retrieved data), Google won't be able to index this part of your website, so you'd better think twice. Commented Jun 5, 2013 at 16:03
  • Thanks, but no, it's not a single page webapp, but even if my website will be one page webapp based on ajax calls, it still can be indexed by google, this also mentioned on google support website.
    – xelblch
    Commented Jun 6, 2013 at 6:30
  • You seem to be confused between should I use my own API and should I just directly use ORM. If you are already having an API why do you want more of similar code to get access to the DB?
    – arunmur
    Commented Jun 6, 2013 at 9:28
  • Yes, thats my question. Can you review this code spinet? gist.github.com/xelblch/bde4c8f107f1ed398a7e#file-gistfile1-py
    – xelblch
    Commented Jun 6, 2013 at 10:37

3 Answers 3

5

Unless the performance overhead of using the web service is an issue, you should definitely use your public API.

This will help you get a consistent behavior between your application and the consumers. It will also avoid code duplication and the best part - if you break your web service you will most likely be the first one to notice it.

The concept is usually referred to as "dogfooding" and Twitter is one example of an application, that dogfoods it's own API.

6
  • Please, read my question again. I know that I should use my own API, but Im asking how to use it? Should my website make calls to my public API through HTTP or query database directly, which is NOT looks like using my own API?
    – xelblch
    Commented Jun 6, 2013 at 6:27
  • What do you mean by "which is NOT looks like using my own API?".
    – Dante
    Commented Jun 6, 2013 at 7:11
  • My public API - example.com/api/call-name, and my website can speak with Database through ORM directly, not through HTTP calls :)
    – xelblch
    Commented Jun 6, 2013 at 8:19
  • Well if you know you should use your API, then what is the question?
    – Dante
    Commented Jun 6, 2013 at 9:22
  • 2
    Do you provide plain database access to the public? If not then that is not a part of your public API.
    – Mike
    Commented Nov 15, 2013 at 18:22
1

I am making a few assumptions here because your question has a few holes in it, but what I think you are asking about is whether the back-end (server-side) code for your web site should make http requests to your public API.

Assuming that this is the question, then the answer is no. Instead of calling the public API from your server side web site code, your web site and the public API should both sit on top of a shared module that implements the functionality of your application.

With this model, the shared module contains all the database calls and business logic. The web site authenticates the user, calls the shared module and renders the UI. The public API might have a different authentication scheme, calls the shared module and returns JSON.

How you implement the shared module depends on your situation. If the web pages and public API are the same compiled application then the shared module might be just a set of interfaces that can be injected. If they are separate applications the shared module could be a separately compiled library (such as a .Net assembly) or it could be a web service that is not public (remember that authentication is done by the web site and the public API).

0

Without understanding how your web-service is set up and how web methods are intended to be called, I don't think there is a way to say you should implement it in a specific way.

For best practice and practicality, I would implement it as close to the way you intend developers to consume/implement it as possible.

2
  • Let me explain. Imagine that I have a database full of games with release date, game name, game platform etc. And on my own website I will show this games as a list or a grid. Also, this data can be reached through API in JSON format, this data can be updated through API and even deleted. And on my own website i have html forms that allow me to do this actions in a user-friendly way. So how should i access my own API?
    – xelblch
    Commented Jun 5, 2013 at 13:42
  • What I have been doing with my own project that have a similar type of output is use the JQuery Ajax method (api.jquery.com/jQuery.ajax) to pull the data from my web-service then have a javascript method that displays the returned data. Might not be the best way, but the same two functions can then be used if you want to display data from a different web method with no changes to the code other than method called and parameters. Commented Jun 5, 2013 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.