Intro to Elixir | Lesson 25: Making multiple requests

Elixir
Transcript

English (Auto-generated)

and in this lesson we will exercise more of what we've learned in elixir by doing further integration with the Star Wars KPI So what I would like to implement here is basically, you know, although finding a person info by by idea is great. Um it would be nice to basically find person info buy me. It would be nice to be able to like as in something like Darth vader and then get the information about Darth vader from the A. P. I. Now, if you read up the documentation from, you realize that if you just do a request to slash people, you will actually get these results with an array of all the people that are available in in the Star Wars api in the swampy. And then um you basically get um information about them right here. So you get these results and then you get basically this is luke Skywalker and then you get information about luke right here. So let's go ahead and see how we can go ahead and implement it. So to to do this we have to find or maybe we can call it, get all people, this doesn't necessarily need any um any parameter because we want to get older people. And then I'll start by doing the same thing http poison that start and then we can make a request to the old people. U R L. So I'll start by putting the CRL for all people, just the same as the previous one. Just it doesn't have the slash I. D. And then we could actually do the same thing. We can do http poison dot get okay. And then we could also do the same thing in here handle a P. I. Response. Which will either decode the body using this poison that decode. Or it will give us an error with an error message. Okay. And then we could do the same thing at the very end. We could say case do now here what we get back is we either get and okay with data and then we do something here or we get error with an error message. And then we need to do something here. Now interestingly enough I want this one here to come back to this function because what I want to do in this particular function I want to say something like old people is you know make the request to get all the people from the A. P. I. And then here what I wanna do is I want to search for the person matching the name. Okay so you want to search in the result for the people matching the name. That's why it would be good for us here to either return these results. So this list of results or return some kind of an error message. Okay so let's handle the error one because it's easier. So you know what we could do just error and we could say something. I couldn't catch hold people now in this particular case here. All I care about is I care about these results right here. Now, do you remember what data is in terms of the data type? In a lecture, in terms of a lecture data type? Well if you remember that it is a um map then you would be correct. So this will be a maps instead of data. What I can do I can do something like this. Results results and then I can return here okay and I can return back the results awesome. So what I'm getting here, this map I'll be getting here as data will be what I will actually be getting as a data right here. Okay will be what I'll be getting back as data right here. Okay. So this is why it's either um we uh we return okay and then the results or we return error. Now notice here it gives me this um orange warning and the reason is because I did not use this error message. So if I'm not planning to use it I could simply put an underscore right here and that will solve the issue. So what I can do here well I can do something like this case. Get old people do if we get okay results then we need to basically search for that person here. Okay by the way I think yeah sorry uh this is this is has the right name. Um but we may want to make it just consistent with fetch. So I'll just use fetch in this case fetch personnel for my name, just to be consistent with this function name, even though there's there's no problem there. So okay, if we get an error here and then here a message then we could say never happened and then put the error message right after it's giving me an error right here. And it's because I didn't actually put the code right here. So let's actually implement the code here. Before I go through further, let's just do inspect and then put results. Um I haven't used name yet. So let me just prefix it with underscore for now. So we don't get that burning by the way, a virgin. Quite a bit of code without testing. I usually don't do this unless, you know, I've done very similar code before or not. I would generally encourage you after every, you know, every few lines of code. I would encourage you to go ahead and test your code that said because again, I've done many similar things to this in the past. I was able to just do it without having to test each one of them awesome. So let's recompile Star Wars. So let's recompile it and then let's try to um called fetch person by name. So Star Wars not fetch person by name. And then let's say it doesn't matter here because we're not actually doing the searching. So literally doesn't matter what we're doing here, but if you've done the job correctly, we should get a list of maps which is exactly what we want in this particular case. So in this particular case here we have a list of maps. Each map is a person from the Star Wars movies. So what we need to do now is we need to actually search to see if any one of those maps contained a name attribute that matches the name that we pass through the function if you would like here. I mean this will be an interesting elixir challenge. So maybe pause this cast right here and try to do it yourself before continuing further. So if you recall from before, the first thing that I would look into is using the nom library again in um elixir is basically um you should be your friend in a lot of the city situations and many cases what I do is I go and I search right here, okay, I searched right here for a method that may match what I'm looking for. In this particular case the find method seems to be what we're really looking for, returns the first element of which fund the function that we pass right here, returns the truc value. Okay, returns the first element of which fund returns true value. So basically in um we give it an innumerable. That's fine. And then we give it an innumerable list is an innumerable and then we give it a function and then we get the whole element back of the one that it first finds. So let's go ahead and apply that function right there. So I can do here in um dot find results and then give it an element function. And in this case what I will do, I will call this a person just some more um it's a better name for it, I should say. And then here, what I would do is I would have to say remember what person will come. So to to double check the person is a a map. Right? So we want to basically match that, the name matches the name that we pass right here. So I will remove this underscore from this one here and then what I will do, I will say return the element if person and then in brackets name matches the name argument that we passed in our function and this has turned out to be short enough that I could put it on a single line. Okay, so again, here we're looping through, we're iterating through the results map, sorry, the results list, and then we return the first element that this clause right here returns through, which means the first Burton map whose name attribute this one right here matches the name that we gave to our function right here. So let me recompile this and let me try that function. A ftf. What happens? We get help, which is probably what we want. If you don't find the person's info then it's fine. So let me try here Darth Vader and you actually get a map with all the information about Darth Vader, okay with all the information about Darth Vader and that is exactly what we want from this function, so what we're able to do here, we're able to fetch all the people from the Star Wars Api and then we were able to apply the imam that find again, you know, a big part of what I wanted to do this lesson forest to remind you that in um should be your friend, should be very comfortable going and playing with the imam library to help you um find um not just fine to to help you manipulate data to help you search within your data to help you, you know, merge data structures together, it's a very powerful library and you should make really good use of it, Thank you for tuning in and I will see you in the next lesson
30 Views 0 Likes 0 Comments

We learn how to use different Elixir libraries and techniques to handle making multiple requests.

Comment
Leave a comment (supports markdown format)