Let's Learn Elixir - Github REST API Client with Tesla (Audio Reup)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello so i kind of wanted to do a tutorial on api clients so i'll get started on one this is a be kind of a crappy recording i might edit it a little bit later um yeah so let's get started it's like uh 9 22 in the morning so i'm trying to be a little more quiet okay so what we've got is we're going to try building a github api client and while doing that you might want to take a look at the github getting started with rest api documentation um pretty good pretty good oh there's my uh there's my bookmarks let's just hide that okay anyways uh so i want to get started with making a github api client the only reason that i want to do this is because api clients are pretty important when you're doing day-to-day work there's a lot of people who have applications where they want to be able to link those applications to another application um and so this guide will kind of teach you how to do that programmatically so the first thing that we're going to be looking at here is like this guide they're like you can do like some hello world with curl and like some get requests maybe like some json you can do like authentication so pretty cool stuff pretty cool stuff um and so with all of this uh let's see exactly what's going on so what they're describing is how to actually use like getting onto their api using just curl and so the first thing that they say is like you can curl https api.github.com so we'll do curl then we get design for failure okay let's try it again design for failure so that's that's what it does so then let's do a different get so we're gonna say curl http uh user's default which that guy was not found so that's kind of helpful you can curl ourself though oh you know what the end point wasn't because i had a typo there we go there's defunct we get back a json now a lot of things on the internet coming back as jsons so we've got stuff like there's a login has an id it's got an avatar url gravatar id it's got a company a blog a location he would have an email maybe he's got a bio it's a cheeseburger uh yeah so that's some some details about that user and start looking like myself oh there's me i was created in 2015. that's pretty interesting it's the only reason i want to look at myself so i've been doing github since 2015. pretty all right pretty alright um yeah there's like some starred some star beats so i got some stuff about our user uh let's see in this guide they show like we also want to attach say so like we get a user to say tastes like json but let's add this dash i flag so that brings back some headers so you can see that in these headers we get like http server github requests etc let's do that we'll go up here i then here's like our headers just as described and then our json but then we get to the juicy part i'm going to talk about authentication and authentication is the juicy part because unauthenticated clients can make 60 requests per hour to get more requests per hour we'll need to authenticate in fact doing anything interesting with the github api requires authentication so you can get personal access tokens i'm going to click this link and maybe edit this next part out let's see it's about getting using your personal access token setting page it's i've never made one you see so uh this next part i might blur it for something but we're going to generate a new token let's see new personal access token so let's do this at like video demo this one will expire uh say that it expires tomorrow so what's today expires tomorrow um and what can one do with this token uh i don't know there's quite a bit of things that you can do let's do like a repo status they're not full control if we go repo status a lot of these settings are like that i think it just might be cool yeah let's just generate a token like that look we get a token cool so let me go back to here and then they talk about curl iu username curl iu username let's do let's do that we're just going to copy nah just copy that let's go back to our curl do i u one and then let's take my token copy that and then let's uh paste it and then see what happens oh look i got like some stuff back but that's actually interesting because check this out we do this without our token we're going to be asked to log in gosh my voice got really raspy there see we're asked to log in get my password now we logged in and see same same thing so using our token or using our password kind of the same that's what we're learning here so but once we can make personal access tokens this becomes pretty easy so we would use these tokens like so so they describe here with curl we're going to do a dash h we'll just add a header this header is going to say authorization token blah blah blah blah let me get some stuff so let's go and actually write some code let's clear that and then let's uh gosh i might need multiple windows here so let me open this up to say a separate window i might need to see some of my already built api clients so look at that one let's go to here and let's make a new one so we'll do mix new uh hit pub api it's technically a raptor let's dash dash sub so that we have supervision trees but we've created it go to our code open that folder for our github api client github api wrapper all right so we're in here and the first thing that we're going to want to do is we're going to want to add some dependencies we might also want to define like what our client is actually going to be doing so we might make a new file called client do i want to make it there yeah i do make a new file called client.x maybe we might call it uh github api client dot ex and this next part is just going to be some knowledge that i already know so let's go back to our mix.exe file we want to add some dependencies so let's go and look for a library called tesla elixir not tesla motors um then we'll get this guide that describes tesla uh and how we otherwise might want to use it and how we would want to install it so we'll install tesla by basically copying these dependencies into our dependencies so tesla or hackney and we want jason and then elixir is going to go and get those as dependencies there'll be dependencies here then let's go and make our api client so i have to look at what i've already built before so we'll make a def module we'll call it our github api wrapper dot client let's see if that's yeah it's the name of writing some github api dropper about client and then we'll make a step new button and this new function is going to take in our uh token and by default it might take in the uh system env token will make a function be this function will do application dot it env let's say api and then we'll say github or song says so i've got a few things to set up there and let's make the rest of this client so say this client has a little layer and this middleware will have tesla dot middleware dot base url that's base url is going to be http s ppi.com i think so let's just double check so look at here and most of the time people are doing github.com just like that so we'll say that's our base url what else am i doing in this bot maybe a tesla dot middleware dot timeout and maybe we want to time out with 10 seconds and we want tesla.middleware.json that means that we want json responses tesla dot labelware headers and in our headers we're going to have a list it's going to have some tuples and we really just want authorization to be a token and then we'd want to put our system enb token here so we'll say this will be token that creates our middleware and i don't know i necessarily need this anyways let's say content type uh that's gonna be application json so that makes some middlewares and we're going to go to tesla dot client let's say middleware and we're gonna have to give it an adapter so let's go up and let's add an adapter we're going to use tesla.adapter.httpc although there are better adapters just keep that in mind so you can actually see that if we go to tesla and go and look at its documentation and we look up about adapters there we go tesla adapters so we've got httpc we've got hackney we've got eyebrows we've got gun we've got mint and we've got finch uh so let's try replacing our adapter to hack me since we brought it in and used hackney before uh let's see how it works so in part of making our api client we might need to make some requests to uh github so we've got our client let's go to like our github rapper here let's make a function called uh well i have to do two things in two places so let's make a function here called def get and get is going to take a url and then it's going to take some options but not all the time are we going to give it to options now we're going to say that this one's going to do tesla.get uh we're going to pass it our client might as well name this new client so we're going to give it a new client every time and then we'll give it our url and then maybe some options there presents that's how we make a get function and then we'll go to our kpi here and then let's make a function that describes getting one of our users so let's see it's like users optocat so we'll say def it user and we might want to ask like just a user so we have that input then we'll say yeah of api wrapper client dot get and then we'll say that our fit url is users and then we'll attach user that should result in much to the same response so let's actually test so we'll go into our terminal either here or we want to turn around here and then we can say that's i x s mix if you're on windows you might have to say i'll show you on a different terminal iex s mix dash it's either dash dash whirl or dashboard but you'll have to say that to be able to open up your little interactive elixir which is down here at the bottom so that's interactive elixir i just hit enter a bunch of times and so we're going to say api wrapper dot get github user and let's look for myself oh that issue our token is not there actually let's go look at our code so we say that we want to do application.getenv github api wrapper github personal access token and so what we want to do there technically is use some configurations uh but it doesn't look like in my api wrapper i've got a configuration file necessarily made so i'll show the configuration a different way unless i can just make a config file i've been here before folks so it's kind of the issue that we've got going here what we're doing is setting the sys the app to get a system env variable so uh one of the things that we might do with that is we might have a folder called config so let's make a a new folder called config and in this config folder we're going to make a new file called uh gosh never made an elixir config like this so let's see make a config config.exss so what we're going to do is we're going to make that config.excess so we're going to say import big now we're going to configure our app api wrapper and then we're going to say that our what do we call in here github personal access token so i'm going to say that our github personal access token is equal to system dot get dnv [Music] system enb that we want to get is our github personal access token needs to be variable say worked config this is just copying from the website so our config should look like that let's fill our application and i just want to reload this window just because it looks like there's some problems in my mix right here start permanent mix env equals prod i don't think i wanted to do that look at what's it screaming about specifically config test doggy xs do i need that name file test.exs we'll say so trying to i think we just needed test.exe to exist actually so it looks like we're good now um so our config so we set up to get a environment variable github personal access token so if we go and do export access let me make sure that i'm writing this right see [Music] yeah i'm writing okay so we'll sit with that and then let's go and get our personal access token again paste that in so we get that then we say echo uh github personal access token you can see we've got our token set up in our little linux environment and then we'll say iex has mix and it could not read dev.exe which is okay so let's uh take this and then go and do file yeah dot excess and we'll basically have the same config in here we want to import big wxs oops that's uh mix mix attempting to load it recursively so maybe i don't want that to be in there let me see how my other config file looks yeah it looks like that okay so dash yes then over in our api client we'll try doing basically the same function so we'll copy that into our terminal and look we get our token and so now we go to our api wrapper and we say github api wrapper dot get github user you can see we did get our token but we had another issue happen and it's partially because i'm using hackney uh you can see hackney is part of it so what i'm going to do is i'm going to use httpc let's do it again what do we get something about what we did is not worded right so let's see well we have to work it so we'd say authorization token get your token oh you know what i see what i didn't word right i need to say authorization like that look at my other client that i've made before uh yeah so we want to say it like that now we want to say like this okay so i had some things in the wrong place so let's recompile let's try hacking again there we go now we have 200 response and so you can see that we sent some tesla with our authorization our content type and then when we got our body back we got the avatar url some like events got like a login name and we got like repos url like header like all these headers um and then we got a 200. so a lot of the time when you're making api calls you might only care about the stuff that is in the body so uh code wise with elixir let's just let's just examine our terminal loosely here let's zoom that in hopefully everybody can see so we might want to say our github api client say this is pattern matching an elixir so we get our response okay on our response so now if we say response there's our response by itself so that we want response.body there we go we've got like our response body then a lot of times people might want to find something about the response the response body so we might say if response body and let's say the name is equal to satos let's if so do potato else let's do not eight center that and look we got a potato so now let's say that if it is not nate then we will get not mate so if the response body.name is not equal to nathan casados do not nate if it is equal to nathan's solos do potato and uh yeah so that's how we'd make a api client of sorts and you might have to do some other buildup type stuff with it so let's uh get back to 100 over here and let's look at our uh client again pull down my terminal since it's all the way up there so other things that you might do at an endpoint like you might do uh anything that is rust so let's look at rest api and there's probably a good wikipedia article representation will say transfer and so we can see that this has some things like get post put and delete there's also options but uh in our code we've made a get function so we might make a post and so let's just set that that post death whoops and so i'm not exactly sure what everything is going to require specifically when you're making them but with depth posts we might say tesla dot post and we'd say i would do client and then url and then some parameters let's just say paragraphs that you're posting to a particular endpoint so you might say uh this function takes in a url and some params and the parameters if there's nothing are just going to be some empty t-value pairs and then let's say that there's a put let's see if i've ever written a put function before tesla output i don't think i've ever written a put before oh i have written a put before here we go say tesla what [Music] it looks the same as our post and then we've got a delete so with delete we just want the url we'll say tesla dot delete client and then put our url so i've got nothing really to post put or delete but this is how you would otherwise make an api client using elixir to be able to do things on some api endpoint so yeah it's a fun time thank you for watching bye
Info
Channel: KeroTutorials
Views: 84
Rating: undefined out of 5
Keywords: elixir, programming, elixir programming, tutorials, github, rest, rest api, tesla, elixir tesla, 200 response, github personal access token, personal, access, token
Id: fmkeXRfgXEs
Channel Id: undefined
Length: 32min 51sec (1971 seconds)
Published: Sun Oct 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.