Intro to Elixir | Lesson 23: Project Dependencies & Making HTTP Requests

Elixir
Transcript

English (Auto-generated)

let us continue with our Star Wars api integration project. Now before I move forward let's just clean up this a little bit um just gonna remove this Hello world um um function here which means I also have to remove this dust and let me just run the test again here. Project and wars that accessed. So again we're down to two tests and too dark tests which is what we have right now. Well maybe I'll keep the greeting for now. If we don't need it we will um remove it later. So what will I want to show you this lesson is how can we add dependencies and how we can make http requests? So they come hand in hand because I'm either dependency I'm going to add is a http client that will be using which is http poison. There's a number of http clients um that um you could use with elixir. Indeed I think any programming language has many http clients um which one you select depends on, impart preference and impart certain features that the client may or may not have http poison is one that is well used. You know I mean last update is Now it's January 2022. So it's about 2 2.5 months ago. So it's relatively up to date. This is usually one thing I would check when I'm picking a library to use and honestly I find it to be a little bit easier to use. So this is why I picked this one for this particular series. Another one I really like is called Tesla elixir. Um so you could look at this one here. This one is a little bit more elaborate to use but you know, I tend to like it a little bit more. Um but honestly this one is easier and they're both just just as good. So um yep. So to add um to add this http poison we have to add it as a dependency. So if you recall we have to go to this mix dot the excess and then we could add this one here as a dependency. So I'm going to delete these two commented out lines here and then I will add this http poison. I mean they're saying version 1.8 I usually look at Um you know the versions that they have. If you can look at their mixed file you'll see it is the latest version is 1.8.0. So let's just go ahead and install the latest version by copying this tuple which has this too. Um Entry is the first one is an atom which tells you the library's name. And then the second one is um a string that tells you the version in this case. This wiggly line greater than will accept something that is more than eight right here let's say 1.9 comes about. Um and then it happens that you've written this line at that particular point before you install the dependency, Then you will install the one that's a little bit higher but it will not accept something like 2.0 because that would be a major version change. So this wiggly line here means it will look at the last digit which is in this case eight. So it will accept anything that is more than 88 or more I should say it should should accept 1.8. Um and then one point whatever that is greater than eight. Now the plug that I'm using here is is putting this red lines. Okay. And this red line says can continue due to errors on dependencies and this is actually you may or may not get it dependent depending on the plug in you get. And by the way, if you're on ps code simply go to uh this um where is it here this year extensions and then look for elixir. Um As you can tell here the ones I have installed is elixir, L S and V. S code elixir. This one is probably a great one to start with. So maybe you can go ahead and use this one if if you don't have it installed already. So adding this dependency doesn't automatically install it to install it. You have to run mixed depths but get mixed depths don't get and that will install the dependency and any other libraries that that dependency relies on. So in this case it basically installed http poison and it also installed a bunch of others that that library depends on. And notice those red lines have gone. So you don't see that error anymore. And it also added this mix dot lock which means that this particular library library will be locked at version 1.8 which means even if 1.9 comes about, unless you explicitly upgrade it, it's gonna install that exact version for the next person who runs this project, which I think is very important and it's it does seem to be the standard in most um in most frameworks these days rails. Does that no, does that and and so on, awesome. So now we have http poison installed uh and you know, I'll just add a little bit of notes here to install pregnancies. You can run mix that stock it after adding the dependency depends see um information in the um let's copy it here. Okay. Yeah. And uh mhm thanks are here in the decks function was in mixed dot E X S for example, something like this. So you can add as many dependency as you want to drive more dependency. You put the comment and then you put here whatever you want based on what you see in the documentation. So before we actually integrate it with our code, let's actually go ahead and um try it. So to try it remember you can't just do I X because I X will not load our project in memory. So we have to do that capital S and then mix, which will load our project. So we have all the modules from our project available and also all the dependencies that we have in our project will be available as well. So let's go ahead now and try actually making the request. So first you need to enable http poisoned by writing http poison dot start and then we make the get request. So what the request I'm going to make, remember? We're going we're going to be using this swampy which is Star Wars Api which again it's kind of a nice dev um ap I like it. It's really good for practicing connecting with a P I. So I'll put in here instead of this kind of default one. I'll put sloppy dot de slash api slash people slash one. That will make the request and it will give us the response. So notice the response. Have this right here. So little little memory quiz for you, what do we call this thing here where you see a percent sign some kind of a module name and then curly bracket if you have guests, it's obstruct, you would be correct because this is obstruct and this truck has attributes. Remember with destruct we get a little bit more features and control than we do with regular maps. We can control things like what attributes we allow there, we could have uh required attributes and so on. So that's why you will see many libraries use structures instead of just plain maps and that's a good practice in general. So notice here a lot of the things we would care about. Come in this body in here. Okay, come in this body. So what this body is as you can tell it's a strength now that spring looks like something that you may or may not be familiar with. Can you think what that something is right there? If you have guests that this is a Jason, you would be correct. So this is the Jason string which means for this to be useful. We need to decode that Jason string and I will tell you how we can do this later. But one thing I want to note here notice how there is this uh backward backslash that you see right there. This is to escape these double quote characters. So because again, if you if you try to put name like this, if you try to put this will basically not compile because electoral will think the end of the string is there. So you will have to escape those quotes by putting these backsplashes before each one of them. This is why this string looks pretty ugly because it's it's keeping all these um all these quotes, double quotes. Uh So for this to be a valid string, it comes in with other things, I mean the one I'm going to focus on here is this status code. So status code 200 meaning if you remember in http it's status code 200 is okay? Is success? Um By the way I want to go like I would assume that you know something about http here. If you don't maybe go and try to take to read up a little bit more about it because I won't be delving into the specifics of http. I'm just showing you here how to integrate with it using um using elixir. So if I put like let's say a number that the person that I kind of kind of guess doesn't exist, what would happen. We get a 404 which means status code is 404 meaning that uh detail not found meaning that they couldn't find a person with with that idea. We can try ourselves to try something like I. D. Three. So I. D. Three. What do we get? We get R. Two D. Two. This person here we go like four and then you will get basically the excavator um and so on. So basically as we pass in different ideas for people here we are getting uh different um different people and different responses from that. Api Alright so one interesting feature about http poison is that you can define your own modules, it's called wrapping. So you could drop a module here and um this will um help us um do you know ease the way we integrate with certain api s okay is the way we go, we deal with certain api Yes but before that so we're gonna do this maybe in the next lesson but for now let's actually try to do this in our code. So I'll go here to Star Wars and all I have to do here is I simply let's say for example I define a function that's called fetch person by and then here I. D. What's the person you okay? So I need to do here we'll need to replicate what we did in the console. So we do http poison start and then we basically make their get requests using http poison. So http poison dot get and then we put the U. R. L. Now instead of the four here what should I do? You might have guessed it by simply inter plating the idea of the person that we are passing. So in this case what I'm doing here I'm passing the ideas so far fetched person by I. D. Alright. You know what such person I'd say info by I. D. Then we do that. Here's where it gets interesting. So I want to show you here how we can use pattern matching to basically take the body of of the response because that's all we care about really. We want to get the body of the response that we get. So this is here an excellent place to use pattern matching. Okay this is where I can have different function heads use pattern matching. So I'll show you right here. So first of all we get back a structure that is http poison dot response. So what I would recommend is you put something like handle FBI response and then we expect the http poison to be instructed that we received back from that A. P. I. Alright. And now this has status code that is 200. So maybe what we should do is we could have two heads. one is gonna just go 200 mm hmm one is That is code 404. And I would always recommend having a fallback. So if you know we don't know what we get here then we could return something else. So what I will do right now is I will take this one here this is the response and then we can say something like this handle api response and pass the response to it. So here's what we're doing again to recap we're starting http poison. So we have availability to all the methods that we have with the http poison and then we make the request so we get back a response. So we make a get request to this U. R. L. After injecting the idea of the person we get a response back and then we call this function that have different heads based on the response. Alright so right here what I'll do I owe that puts by the way each one of those two should come with the body. So the way you can do this is your new body. So again here I'll remind you a little bit in case you've forgotten about pattern matching. So this will pattern match. The status code is exactly 200. Okay. Which is what we're getting right here in the case of a success and this will only match if the status code is exactly four or four but then the body will could be whatever you want. So if I want to take that body instead of just having to fetch it from the structure which I can it's easier to just do this in the pattern matching. So I'm fetching the body and my signing the body to the variable. So I'll put something like this here. Success I. O body you hear something like this resource not found and then we put the body in the third case we do I owe that puts unknown error. So it could be some other error. Let's say a. P. I. Is down. Maybe we get 500 several crashes. There could be some other things that we don't know. So let's just say go ahead and put iota puts unknown error. Awesome. So again here review this here because it does include a lot of the principles, a lot of the concepts that we've learned for elixir so far so right remember here because I've already loaded this. I have to recompile Star Wars. So the way I do it, I basically note the module name and then I put our for re compiling and but now I put star wars dot Experts and by i. d. one it says success and then it displayed this is a string by the way, that's why you may be surprised that this doesn't look like a tuple which you would think is in this case and it's not the map because you would see a percent sign this is actually a string so don't be confused but this string is a Jason which we will need to decode later. Let's say I passing something else. Like you know, some other number it says resource, not found detail, not found. Okay. Uh you know, I can't think of a way to induce this one right here for now but it could actually be an error that comes from the Api itself at some point maybe were rate limited or some other errors as well, awesome. Before I end this lesson, I want to just improve this code right here with something we've experienced quite a bit. So I'm calling a function and then I'm passing the output to another one. Can you think of a way to improve it? If you thought that this should be with the pipe operator then you would be correct. So anytime you see yourself calling more than three functions or more, I would say more than two functions then it's usually a good or actually I should say two functions or more. This is a good thing that you could consider using the pipe operator. And remember with the pipe operator the first argument to each function we have in the pipe is whatever is output it from the previous uh from the previous function. So in this case I take this um string I passed it as the first argument to http poison baguette and whatever that returns, I pass it as the first argument for handled api response which I expected to be of struck type http poison dot response. Let's make sure this still works. I'll need to recompile this and then I basically it seems to work. Let's try with one that we know let's try maybe 10 and now it's just Obi wan Kenobi with I. D. 10 so it is working as expected and yep so this lesson we learned how to add a dependency to to our mix and we learned how to make an http request using http poison library. Uh we'll continue the journey with next lesson so we'll improve the solution a little bit by re factoring to using the wrapped resource So bye bye bye wrapping this http poison to make this more uniform and then we'll have some fun by connecting with different ap eyes and implementing different functions. Thank you for joining and I'll see you next time
40 Views 0 Likes 0 Comments

We learn how to add libraries as dependencies to Elixir projects. We will also learn how to make HTTP Requests using the HTTPPoison library.

Comment
Leave a comment (supports markdown format)