Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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. For more explanation please look this gist: https://gist.github.com/xelblch/bde4c8f107f1ed398a7e

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?

share|improve this question
    
Please tell us a little more about your architecture. –  Marcel Jun 5 '13 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. –  loki2302 Jun 5 '13 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 Jun 6 '13 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 Jun 6 '13 at 9:28
    
Yes, thats my question. Can you review this code spinet? gist.github.com/xelblch/bde4c8f107f1ed398a7e#file-gistfile1-py –  xelblch Jun 6 '13 at 10:37
add comment

2 Answers

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.

share|improve this answer
    
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 Jun 6 '13 at 6:27
    
What do you mean by "which is NOT looks like using my own API?". –  Dante Jun 6 '13 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 Jun 6 '13 at 8:19
    
Well if you know you should use your API, then what is the question? –  Dante Jun 6 '13 at 9:22
1  
Do you provide plain database access to the public? If not then that is not a part of your public API. –  Mike Nov 15 '13 at 18:22
show 1 more comment

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.

share|improve this answer
    
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 Jun 5 '13 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. –  StMotorSpark Jun 5 '13 at 14:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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