I am going to make a small app but I do not know how to do it that's why I need you advice. For example, I have a api_key like that API_KEY='AdsbdmAYugjhnvcvbnRDgfcvbnb' from third party service, let's imagine this computer service group and I need to do is that when I post a computer model number (i.e XS54125) and it should return name, manufacturer, produced date, etc and I should save this details in the database but I do not know how to implement this app. Can you give me idea please? because I have never done this kind of app before. If anything is still unclear please let me know I'll try to explain in more detail. Thank you beforehand!
-
Create a view, that accepts the computer model and send requests to the third-party service from the view and receive the response. You can use Django models to save the response/data into your Database.JPG– JPG2019-12-13 01:35:08 +00:00Commented Dec 13, 2019 at 1:35
Add a comment
|
1 Answer
You can follow the django rest framework tutorial to create an endpoint that basically do what you need, it means:
- Create your project
- Create your api app
- Set in the core project your
API_KEYin thesettings.pyfile usingconfigparser(i.e: getting the credentials from asettings.inifile) - Install django rest framework
- Create a
Computermodel with all the attributes that you'll need (model number, name, manufacturer, produced date, etc) - Create in your api.py file a view (inside of your api app), where you'll make a request to the third party service (i.e with requests library and if the response is in Json format, manipulate the response as you need) and in the urls.py set the endpoint url with POST method. This can be a Class that inherit from
viewsets.ViewSetinstead ofviewsets.ModelViewSet, because you don't need to extract data from a model, but you'll be able to save the response in yourComputermodel. Optionally you can create a Serializer to save the data into yourComputermodel.
2 Comments
Nerd
thanks for the answer! I think requests library I should give url right? or can I give api_key? if so how can I do this can you tell plz?
Manuel Carrero
Probably you'll need to do something like this:
response = requests.get("<third_party_service_url>/?key=API_KEY") but you should check the third party service documentation to make sure of how to pass your key to the request