# Fetching data from APIs using Django and GraphQl without hitting the rate li..

Source: https://djangotv.com/videos/djangocon-europe/2019/djangocon-2019-fetching-data-from-apis-using-django-and-graphql-without-hitting-the-rate-li/
Date: 2019-04-21
Speakers: Speaker 1, Speaker 2, Speaker 3, Speaker 4, Speaker 5, Speaker 6

## Summary

Manaswini Das explains how she integrated GitHub data into the Open Humans platform using Django, OAuth, GraphQL, and background task processing. She describes rate limits as both a practical constraint and a security measure, then argues that GraphQL can reduce the number of requests by retrieving related data through one precise query and returning a single response. The project authenticates users with Open Humans and GitHub, downloads their GitHub data as JSON, and lets them update it; future work would visualize the data.

## Transcript

### [00:00] Introduction and Project Goals

**Speaker 1:** [00:00] So hello world, hello DjangoNots, hello netty zenz. Yeah I'm here to talk about fetching data from APIs. Using Django and GraphQL without hitting rate limits. It's a pretty long title. It's my first international talk, so I may have to work on the titles more. So I'm Manishuni Das and I'm a student. from computer science and engineering from College of Engineering and Technology Bhuneshwar Ordesandya. So this is a project that I developed as a part of my Art Preach internship with Open Humans Foundation and 2018. So Open Humans Platform provides a single platform for all the

[00:47] users to store their data, including Ancestry DNA, 23andMe Fitbit, all of that. So my project proposal included including GitHub and Twitter data sources to open humans. So before moving on to that, I would like to say who am I? So I'm a student, I'm almost a graduate, I'm almost there But since I haven't received my degree yet, so I'm still an undergraduate. I'm from I'm also a Processing Foundation Fellow 2019. That's going on now and a girl script summer of code mentor and an RFiji 2018 alumni as well yeah so I had the liberty to choose APIs to choose data sources. So why did I choose GitHub API and Twitter API is due to the fact that

### [01:35] API Rate Limits

**Speaker 1:** [01:35] GitHub API the data is already sorted and organized and which makes it an excellent source for visualization When you see all those green color contributions, it gives you an inspiration to work more and to work for that darkest green in your profile. Then we have meticulous information regarding pull requests, issues, activities, repositories. So yeah, that's just a meticulous whole lot of data that you would like to see in your own Open Humans account. Yeah, so moving on. So I mentioned without hitting rate limits. So I need to cover what rate limits is all about. So it is like the amount of traffic outgoing and then coming to a particular network or a particular API. So Yeah, so that's what rate limits are.

[02:21] So you may have come across these requests like it has already hit the rate limits. GitHub APA has five thousand requests per minute, Twitter API has six hundred sixty requests per minute, so that is rate limits. It ensures better flow of data and increases security and makes sure that You don't you know fetch a lot of data and that is not visible or not you know not able to organize, very difficult to maintain. So yeah, so it increases security in the way that it prevents DDOS attacks, that is distributed denial of service attack that is not really for like It doesn't really pose a risk to the se security risk, but it renders a website or an online business inaccessible, so it's it's dangerous.

### [03:09] Data Fetching Challenges

**Speaker 1:** [03:09] It prevents users from making an attempt to load on some information. I covered that before, so Yeah. Moving on. These are some of the challenges that I faced while working with GitHub API. It uses rate limits, so I had to respect that. And take care that if I fetch a lot of data that will not lead to a lot of data, but it will lead to a lot of errors. So moving on we have fetching data from a GitHub takes time, not because of the rate limits, but it can be a lot of data, depending upon your number of countries. contributions. We want to regularly update data and take into account data we already did upload to open humans. Yeah. Open humans has this ability to, you know, update the data that we already have since you don't like

### [03:53] Rate-Limit Management

**Speaker 1:** [03:54] stop contributing, you just keep adding to your contribution. So yeah, it helps to update your data as well. So for rate limiting I used a salary and requests respectful. For salary it's a simple, flexible, reliable distributed system to provide vast amounts of requests. So it is it helped me a real lot and rate limiting. Apart from that, we also have we also used a request respectful, which is minimalist wrapper on requests to you know respect the rate limits. It also helps to scale out a single thread single process or even a single machine, it also proxies HTTP requests for small code changes and It works with Python 2 and Python 3 as well. You if you want to check you check it out, it is the link is in the description like github.

### [04:43] GraphQL Advantages

**Speaker 1:** [04:43] com slash serpent eye slash request restrictful, you can just have a look there so I used GraphQL, so it was also upon me. I faced a lot of issues with REST APIs while REST APIs while fetching data because I had to fetch data from a number of APIs, from multiple APIs, so there was a risk of hitting rate limits. So I shifted to GraphQL is an open source server-side runtime specification developed by Facebook. And it is as the name suggests, it's QL, which is a query language to fetch and mutate, query, fetch and mutate data from data sources. is the logo for you. Yeah. By GraphQL. Yeah. There are some reasons why I changed to GraphQL.

[05:29] The first reason being versioning. REST APIs have a lot of versions. Taking into account GitHub API, REST API's versions are version one, two and three. But in GraphQL there is a single version. If there is any updates, there is to the single version itself. So it made that version versioning thing really you know handy. Apart from that we have Ask for what you need. Yeah. If you are asking something and you don't want anything more or less than that, so this is like a very to-the-point and concise way to fetch data. I don't know whether I can show you a demo here

### [06:15] GraphQL Queries and Responses

**Speaker 1:** [06:15] let me say. Yeah, I can I think Oops, I can't. Okay, it's fine. Oh Yeah, I would like to show the net this screen. Okay, so this is the demo. This is the version four of GraphQL API. It has Explorer available to us. You can see that this is the query on one side and this is the response on the other side. Whatever you ask for like a name with owner Stargazers, sparks, it's just shows the exact data, which is very similar to the query, so you can easily correlate.

[07:07] If you the best thing about explorers is that the thing is you can easily predict what will be your next node or what will be your next edge by simply clicking on control space. So that's the best part about GitHub GraphQL API or version 4 I don't know whether I can Yeah, so this was the demo. Then we have get many APIs response in a single request. So that helped me a lot That helped me not writ not hitting rate limits and you know stitching a number of REST APIs and getting a single response out of that. Because GraphQL provides a schema stitching which is really essential for

### [07:52] OAuth Authentication

**Speaker 1:** [07:52] querying from multiple REST APIs. This is how GraphQL work works. It has the client can work and operate via you know any computer or any device. Then it sends a request to the GraphQL, then GraphQL can fetch data from legacy systems, microservices, or third-party APIs and then produce you a single response. Yeah, this is OWAT. I know all of you know OWAT, right? You have heard about OWAT, I think. How many of you know about OWAT that's pretty a lot of people. But since this talk is for intermediate as well as bigners, so I would like to cover what it is. We have almost come across this thing like sometimes ESPN

[08:38] asks to, you know, log in with GitHub and then you are sorry, ESPN asks whether you can log in with, you know Facebook. So you get to log in with Facebook without even entering your password. So that is what OWAuth is all about. So this is how pretty much how OAuth works first an authorization request is sent, then the resource on a sense an authorization grant, then the client sends An authorization grant to the authorization server, and then the authorization server returns an access token and the access token is then returned to the resource server and then the protected resources sold to the client So that is pretty how pretty much how OWAT works. This is the workflow that I followed in a project.

### [09:23] Project Workflow

**Speaker 1:** [09:23] That is first the user goes to the website provided by this repo. Then authorizes with open humans. You can also authorize with a Google account, so that is another OAuth authentication taking place there. Then we have redirect to the GitHub page. Then you have to authorize with GitHub, that is again login with GitHub. And then GitHub data is available as a file, as a JSON file. You can easily download that and Yeah, you can alm always click on that update button and get regular updates of your own data that is in JSON format. So demo time, you can find this work in this link. This is there. Manishuni Das says what Ogithub source, so you can go there and check. So this is the video

[10:09] Actually the thing is I was trying to run it in my own computer but there was some issue and it suddenly stopped working. You can relate to me, right? Yeah, so I ha I somewhat managed to take you know a screen recording of that so I'd like to play it Oh, sorry. I think it's not plain

### [10:55] GitHub Data Upload Demo

**Speaker 1:** [10:55] Yeah, so first you have to start the server which is by me by typing Hiroku local in your terminal or going to manage. py run server and then you have this app running at localhost 5000 Yeah, so just go on to localhost 5000. Then you have this home screen which helps you integrate your GitHub account to Open Humans and this is the homepage. So you have to click on start with open humans authorization yeah, then it directly leads to this page, but actually it skipped the award part. You can sign in and sign up at the you know first thing. Then yeah, so this is authorized GitHub data upload. Then you have to authorize this app

[11:42] by clicking on authorize project. Then Out. Yeah, so this leads to the redirect page that is for logging in into GitHub and then yeah, you have to log in into GitHub. At first, your data may not be visible but when you reload this, your data will be visible. Yeah, you can all see your data and in the terminal as well, like this. How load of data? It's in JSON format, so Yeah,

[12:27] just reload it and we get download GitHub data. So when you click on download, you have that file available in your local system. Yeah, so again that's pretty lot of data. All the data will be as available. Yeah Say so that's how I fetched data from GitHub API and made it visible to the Open Humans platform. This data will be visible in your open humans account as well. I think it's still loading.

### [13:20] Closing Remarks

**Speaker 1:** [13:20] It's boring. Okay, we'll we'll switch to the next slide. Yeah. So this was all I had to say. Very I extend my heartfelt gratitude to DjangoCon Europe for organizing this awesome conference and having me speak my work out and

[14:05] I also have to give a huge shout out to Outreachy for giving me an opportunity to work with Open Humans Organization. I would like also like to thank my mentor Mike Escalante, Bastian, and Matt Price Ball for extending me the help and support. And apart from that, I'm very grateful to my parents and my friends for helping me reach where I'm today. So that's all I had to say. Thank you.

### [14:38] Questions

**Speaker 2:** [14:38] That was awesome. Thank you so much. So we have a few minutes for some questions. Again, we are using DjangoCon QA online and you can also come up present. In the center, if you are in the auditorium.

**Speaker 1:** [14:59] Any questions? I know it's already lunchtime, but yeah, we can answer questions. Oh ,

**Speaker 3:** [15:06] I have one question. Did you use the actual was it only a test or did you actually use the GitHub data? With anyone?

**Speaker 1:** [15:17] I did use GitHub data.

**Speaker 3:** [15:19] For what?

**Speaker 1:** [15:21] For?

**Speaker 3:** [15:22] For what? Like why f no I mean what did you do with it? Like what what's it?

**Speaker 1:** [15:27] It just gets uploaded to the Open Humans platform and your data is visible like along with the other data sources that Open Humans provides, such as twenty threeandme. And ancestry DNA and all of that. It the next future scope is you know visualizing your data. That's it.

**Speaker 3:** [15:44] Thank you very much.

**Speaker 1:** [15:45] Thank you.

**Speaker 4:** [15:47] Hi, so I've got a question for you in terms of the practicalities of building a system like this. Okay. GraphQL is well documented and they've got its APIs. OpenAP OAuth is documented, has its APIs, but the actual mechanics of trying to get one to talk to the other and making all the pieces fit together can be complicated Do you have any tips or suggestions for someone trying to debug why their big system like this isn't working? Any tools or tips or tricks that were useful to you building this system?

**Speaker 1:** [16:18] Okay, a lot of my friends reached out to me. I used like Stack Overflow was my home that time 'Cause I had to deal with a lot of you know errors. There was a simple error, like all of the that stuff was working all good and fine, but suddenly the data wasn't sewing. So I just had to, you know, there was this tags, so I just had to give GitHub tag. So that's that's what that's all I had to do. There may be minor errors, there may be major errors, but it's better if you reach out and you know to forums. I don't know whether that answer your questions or not. Thank you.

**Speaker 5:** [16:55] Hi, thank you for your talk.

**Speaker 1:** [16:56] Hi.

**Speaker 5:** [16:57] I had a question about you mentioned for as part of the Open Human Project you need to with a certain number of requests that you can make before you get rate limited, you need to figure out timely updates versus bringing in new records. So if you're only working with a thousand if you have millions of records but you only have a thousand API calls per minute, how do you decide how many of those are going to be bringing in new records versus how many trying to check and confirm that your historical records are still up to date?

**Speaker 1:** [17:28] I think Gras RafQL provides everything in a single API call. So that pretty much worked for me, but I don't know whether I didn't have a chance to explore the limits. Thanks for the question. Okay, cool.

**Speaker 6:** [17:45] There is a question from the internet. If there are any high-profile GraphQL API providers apart from GitHub Like are there any gre most APIs tend to be REST APIs or XML stuff? If there are any other GraphQL APIs that you're aware of that are rather large apart from GitHub.

**Speaker 1:** [18:10] I think there's a Twitter thing as well. I was really lucky to find a GraphQL cohort for the REST API of GitHub. So I think it's pretty much difficult if there is no documentation for GraphQL APIs of different data sources but I was lucky to find the GitHub cohort.

**Speaker 6:** [18:27] Thank you.

**Speaker 1:** [18:28] Thank you. Any other questions?

**Speaker 2:** [18:35] That was awesome. Thank you so much.
