DEV Community

Farhad Ali
Farhad Ali

Posted on

help creating django REST API

hello guys!

I need help with a particular project that I'm doing with django REST API framework.

so in this project, I have access to a REST API (let's call it API # 1)

and I'm asked to develop my own django API (API # 2)based on the API # 1

so I'm just confused. normally, I have a django project with all data inside database and I create API using data using product models. but in this case, I have no database, no models. the data is coming from API # 1.

so how do I go about implementing this project??

thanks for your time.

Top comments (8)

Collapse
 
tngeene profile image
Ted Ngeene

Can you provide further context on what API#2 is doing from API#1?

Collapse
 
alifarhad profile image
Farhad Ali • Edited

hi thanks for your reply.

the API#2 will be doing exactly the same thing as API#1. so for example if one of the API#1 endpoints says: /api/v1/users gets us a JSON like {name: jhon, age:1}

then

this API #2 would have exactly a mirror endpoint and it is supposed to return exactly the same data so in this case API#2 would be:
/api/v2/users ===> {name: jhon, age:1}

Collapse
 
hedgy134117 profile image
Graham Smith

Then what's the point of API2 if you get the same result from API1?

Thread Thread
 
tngeene profile image
Ted Ngeene

Also wondering the same.
Are you supposed to dump this data on the models of API#2?

Thread Thread
 
alifarhad profile image
Farhad Ali

hi friends, thanks for you reply.

I know it's pointless. but this is an academic activity. I don't see the point of doing such a practice, but I'm supposed to do it anyway, so could you tell me what's the best way to do this?

so far what I have planned is that create a regular django project, and create URLs that match with the API#1 endpoints, and connect those URLs to their views and then inside views just get data in JSON from API#1 and return the same JSON data back. and have little error handling thrown it.

so what do you think? does that sound good enough for this job? let me know if there's a better way to do this. API#1 also involves few POST requests and most of the endpoints use Auth/login as well.

Thread Thread
 
tngeene profile image
Ted Ngeene

Oh, that makes more sense now.
So, your approach is fine. The authentication part would be a bit tricky but you can pass the token as a parameter in your headers as you're posting to API#1. Then the response you get, you dump it to the db of API#2.

Collapse
 
ziggo profile image
Giuseppe • Edited

I just think that, if you use django-rest-framework, according to the example in django-rest-framework.org/#quickstart, you should define into the class:
class MyTypeViewSet(viewsets.ModelViewSet)

Instead of: queryset = MyType.objects.all()
Something like: queryset = requests.get('yourApi1.com/mytype').text

Maybe you will have to parse the data, or something like that about the formatting of the data, I don't remember right now, I haven't used django-rest for a while

Collapse
 
alifarhad profile image
Farhad Ali

hi thanks for your reply. actually I searched few things, and so far it looks like I might not need to use DRF at all. it should be doable with Django itself.