As a backend developer, I am looking to provide frontend programmers with a simple way to get the data that the user interfaces require, namely in the Intelligenia mobile team to which I belong, this data will serve both a mobile app and an interface Web made angular, so implementing a consistent RESTful API, we could feed both, and also provide our source of services for consumption by third parties.
The application programming interface, abbreviated as English API : Application Programming Interface, is a set of subroutines, functions and procedures that offers a certain library to be used by other software as an abstraction layer.
Looking for a simple definition, REST is any interface between systems that uses HTTP to get data or generate operations on that data in all possible formats, such as XML and JSON. It is a booming alternative to other standard data exchange protocols such as SOAP, which have a large capacity but also a lot of complexity. Sometimes a simpler data manipulation solution like REST is preferable.
A RESTful web service contains the following:
1. Resource URI.
A URI is structured as follows:
{Protocol}: // {domain} {: port (optional)} / {resource path}? {Parameters}
{Protocol}: // {domain} {: port (optional)} / {resource path}? {Parameters}
2. The type of representation of said resource.
For example, we can return in our "Content-type: application / json" header, so the client will know that the content of the response is a string in JSON format, and will be able to process it as he prefers. The type is arbitrary, the most common being JSON, XML and TXT.
3. Supported operations.
HTTP defines several types of operations or verbs, which can be GET, PUT, POST, DELETE, PATCH, among others. It is important to know what each verb is thought of, so that it is used correctly by the clients:
• GET: Used to query, read and ultimately access a resource
• POST: Send data to create a resource. As in any POST request, the data must be included in the body of the request.
• PUT: Used to edit a resource. Like the POST, the data must go in the body of the request.
• DELETE: Is the option to delete a resource
• PATCH: Used to partially modify a resource, although it is rarely used. Usually used simply PUT
The correct use of these methods is basic to consider an API as REST. You should not design an API that makes modifications to a resource using GET methods, or deletes elements through a POST, however, there are many such cases.
• GET: Used to query, read and ultimately access a resource
• POST: Send data to create a resource. As in any POST request, the data must be included in the body of the request.
• PUT: Used to edit a resource. Like the POST, the data must go in the body of the request.
• DELETE: Is the option to delete a resource
• PATCH: Used to partially modify a resource, although it is rarely used. Usually used simply PUT
The correct use of these methods is basic to consider an API as REST. You should not design an API that makes modifications to a resource using GET methods, or deletes elements through a POST, however, there are many such cases.
4. Hyperlinks.
Finally, our response may include hyperlinks to other actions we can take on resources. Usually included in the same content of the response, so if for example, our response is an object in JSON, we can add a more property with the hyperlinks to the actions that the object supports.
Example with Django REST-framework
We will use Django and its Django REST framework (DRF) package for the development of our API. Django is a high-level web framework, written in Python , that helps fast development and a clean and pragmatic design. It places emphasis on reuse, connectivity and component extensibility, and rapid development. These maxims are also in DRF, it is a library that allows us to build a REST API on Django in a simple way. Offering a high range of methods and functions for the management, definition and control of our resources. So we will be able to have a RESTful API, in a very short time, generating the services in one of the ways that this framework provides.
We assume that we have a Django project with its already created models, and we have conveniently installed DRF. Let's illustrate this example with the 'programmer' model.
From django.db import models
We assume that we have a Django project with its already created models, and we have conveniently installed DRF. Let's illustrate this example with the 'programmer' model.
From django.db import models
LANG_CHOICES = (
('Python', 'Python'),
('C', 'C'),
('Java', 'Java'),
('Php', 'PHP')
)
Class Programmer (models.Model):
Nick = models.CharField (max_length = 16)
First_name = models.CharField (max_length = 255)
Last_name = models.CharField (max_length = 255)
Email = models.EmailField ()
P rogram_lang = models.CharField (max_length = 32, choices = LANG_CHOICES)
The first step is to create serializers of our models, in a serializers.py file in each of our Django apps.
From .models import Programmer
From rest_framework import serializers
Class ProgrammerSerializer (serializers.ModelSerializer):
Class Meta:
Model = Programmer
Fields = ('nick', 'email', 'first_name', 'last_name', 'program_lang ')
Next, we'll use the generic view-based classes provided by DRF to perform the services, perhaps by adding an api.py file to separate them from the conventional views included in views.py
From .serializers import ProgrammerSerializer
From .models import Programmer
From rest_framework import generics
Class ProgrammerList (generics.ListCreateAPIView):
Queryset = Programmer.objects.all ()
Serializer_class = ProgrammerSerializer
Class ProgrammerDetail (generics.RetrieveUpdateDestroyAPIView):
Queryset = Programmer.objects.all ()
Serializer_class = ProgrammerSerializer
Finally, we will include the urls of our RESTful API, in urls.py:
From django.conf.urls.defaults import url
From .api import ProgrammerList, ProgrammerDetail
Urlpatterns = [
Url (r '^ programmers / $', views.ProgrammerList.as_view ()),
Url (r '^ programmers / (? P <pk> [0-9] +) / $', views.ProgrammerDetail.as_view ()),
]
For the first url, the GET method would return the list of programmers, while the POST would allow us to create a new one. The second would perform all operations, on the indicated object, by the primary key in the parameter of the url. GET to get it, PUT or PATCH to modify it, or DELETE to delete it.
To configure our API, simply add to settings.py a dictionary with all the information. Rendering classes, parsers, permissions, for authentication, pagers, use of filters, date and time formats, etc ...
To configure our API, simply add to settings.py a dictionary with all the information. Rendering classes, parsers, permissions, for authentication, pagers, use of filters, date and time formats, etc ...
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES' :(
'Rest_framework.renderers.JSONRenderer',
),
'DEFAULT_PARSER_CLASSES' :(
'Rest_framework.parsers.JSONParser',
),
'DEFAULT_AUTHENTICATION_CLASSES' :(
'Rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSIONS_CLASSES' :(
'Rest_framework.permissions.IsAuthenticated',
),
}
With this we indicate that, both calls and answers, will be included in format JSON, besides requiring a Token in order to obtain these answers.
The result is that with a few lines of code we have the services of our RESTful API ready to be consumed by any external software. From here, program specific details of each project that require so much time, but thanks to Django and DRF, we have streamlined the development of the bulk of our API and we can devote part of that time to these other tasks.
The result is that with a few lines of code we have the services of our RESTful API ready to be consumed by any external software. From here, program specific details of each project that require so much time, but thanks to Django and DRF, we have streamlined the development of the bulk of our API and we can devote part of that time to these other tasks.
Writing an essay can be a tough task, but some prior preparation and planning can assist you to get it done. Writing is a type of art and, like all other arts; it needs a personal touch to get to its readers. There are no correct rules and indications, but tips and pointers; the rest put in practice. Outline, write, read it and write it again if needed; it is not concerning getting it correct the first time, but rather getting it right. If you are interested in online essay services, you can visit custom essay writing service this is the best writing service. Read first, write later. It’s not possible to write on something you don’t know, so study your topic strictly. Outline the most vital points to focus on. Analyze your readers. The characteristics of your audience, including their education stage and reading skill, will assist to determine the level of language that you utilize and the kinds of data that you contain to persuade them. Prewritten to list the tip you need to cover in your essay. Don’t hesitate to write down each idea that arises, regardless if it’s joined to the paragraph you’re focusing on or not. Thoughts can pop into your mind by chance, so write them down on your draft as they come. Write an introduction. The introduction should bring the reader into the subject and give him an idea where you’re heading without overlapping the fact you present in the body of the essay. Present each thought in a paragraph, sustaining your claims with hard arguments. Use citations to add right to your claims and show you know what you’re talking about and not just writing from the best of your head. Make a conclusion. Your essay should end with a part summarizing the main points. As well, you can give your own opinion or present a twist of logic something to leave the reader involved in the subject.
ReplyDeleteWriting marvelous essays is a complex process that requires much efforts, skills, and time. Some students have already got used to composing such papers and can easily cope with the task regardless of the topic. However, even they may sometimes require additional help. With the advent of modern technologies, all aspects of life have been improved by the use of different kinds of applications. Nowadays, you can install them anywhere: on your mobile phone, computer, tablet, or even smart watch. Unfortunately, there are myriads of them flooding the Internet, and it is quite difficult to find the necessary one to meet your requirements. Here you can find top five most helpful apps that will benefit your writing process.
ReplyDeleteWhen it comes to the question whe to buy an abstract, many of the students feel hesitant and confused. We are an international company known for its perfect skills of dissertation writing and abstract writing in all subjects.
ReplyDeleteWriting a dissertation abstract is truly one of the most difficult parts in dissertation writing, mainly because it is the very last point of the process, so getting quality dissertation abstract help online is not an option; it is a must. If you find it challenging, get timely dissertation abstract help online from our abstract writer.
ReplyDelete
ReplyDeleteThanks a lot. Now I know how to create a RESTful API with Django REST-framework. I can delve deeper into this topic once I finish placing my order at dissertation literature review services.
Thanks for this elaborate explanation on how to resolve the system problem and get it work fine again.
ReplyDeletedissertation literature review services
Thanks to your article I have finally found out how to create a restful API with Django REST-framework. To feel equal, here you can find hypothesis testing help at affordable price.
ReplyDeleteThanks for sharing this.,
ReplyDeleteLeanpitch provides online training in agile coach during this lockdown period everyone can use it wisely.
icp acc
icagile certified agile coach
Excellent content ,Thanks for sharing this .,
ReplyDeleteLeanpitch provides online training in CSPO everyone can use it wisely.,
Join Leanpitch 2 Days CSPO Certification Workshop in different cities.
CSPO TRAINING
CSPO certification online
ReplyDeleteCertified scrum product owner
CSPO online certification
Great content, thanks for sharing with us.
ReplyDeleteapple series 6 nike
Thanks for sharing the informative content with us, it really very informative and helpful.
ReplyDelete