Intro to Elixir | Lesson 24: Decoding HTTP Requests

Elixir
Transcript

English (Auto-generated)

it's let's continue our Star Wars api integration. So if you recall from the last lesson what we did we basically make a get request to fetching information about a particular person. And then what we did is we Um handled the response by looking at the status code. If it's 200 And then if it's not 200 we do something else. In most cases all we're doing actually is we're we're displaying the body of the response which is a strength that in this particular case it will come as Jason. So this is why um I think it may be better for us to handle this a little bit better by decoding to the um the one that we get back. There are different ways to um deco Jason but the popular one is to use this poison gem. Um Sorry it's not the jam. It's a library gem is more coming as a ruby developer. So poison. Oh not this poison. It's a poison elixir I should type. And then here incredibly fast electoral focusing um sorry poison is a new Jason library focusing on work at fast speed without sacrificing simplicity and completeness. Um what I like about poison it's really easy to use. It's basically you give it a string you call poison dot decode and then you get back in most cases a map it could be an array could be an array of maps and so on. So let's go ahead and do it only in the case of um this one here where we have a success. So first of all actually poison is a library. So before we can use it we have to add it as a dependency to our project. So let's go ahead and add this to our mix dot the excess. So I'll go here to depths and then I will put poison here. Although it's not a requirement. It's generally nice to sort these alphabetically. So it's easier for the reader to know to find out. Especially in bigger projects where this list will be huge. So remember we have to run a mixed depths that get in order to get uh this dependency and now this red error should go away. Maybe we'll give it a little bit of time Depending on your editor and then we know for sure that poison 5.0.0 was installed. I went wait on it longer. It could it could could take a while. So but I know right now that poison is installed. So what I can do here is I can actually, by the way if you're not displaying a string it's better to use inspector. So in this case if I want to use poison I want to use poison dot oh he called body. In this case I used inspect because it's not a string. If you're not displaying a string or something that can easily be converted to a string you have to use inspect which is a better way to this display it. So let me run I. X. With loading mix well So we can do this by doing that capital dash capital X. In the mix. So it does here showing us a warning but this warning is coming from the library itself. I find those in most cases for stable libraries. It should not be a concern. It could be a concern if you're dealing with the library that's relatively new or not as maintained. But in this particular case I believe this should be okay. Um So what I would do now is we can do star wars dot fetch information about the person and we'll put one which we know exist. So notice what I got back here is I actually got back a map and that's exactly what I want. So I got back a map which makes it easy to pattern match on and it's basically easier to uh to deal with using the library that we've learned before. So we noticed for example here that you know the keys are all strength. It's usually the case when you're dealing with Jason because they may not be um easily convertible to adam. So that's why they all come as as strings. The values could either be strings or they could be also a list like we're seeing right here. They could also be numbers but we're not just here. Even numbers are coming as a strength as well. So for example what we can do here is um we basically need to know whether it succeeded or not because if it didn't succeed, we don't really care about using poison to decode it. So here's where we could use something interesting and this is a very common pattern in in a lecture. So I'm gonna move these printouts for now. So what we we we can do at the moment is we can return which again is a very common pattern in elixir. The first element of the tuple, it's just have two elements in this particular case. The first element, it's usually an atom and that atom will tell you whether that function, you know in this case succeeded or not. So I give okay. And then the second item in this case is the decoded. Yeah. Body. Which we know will come back as a map. Now in this particular case again I'll remove these printouts and I will put a tuple this is an error. And then the second argument here we could put a string that says something like a resource not fact because we have have four of them. Now notice here I'm getting this yellow line right here which says variable body is unused. If you try to run this app it will not give you an error but it will give you a warning. So what you need to do here if we don't need the body and then we can simply just eliminate it. If you want to keep it for one reason or another. Maybe for informational purposes you can put underscore before the variable name and then you would be okay. But in our case here we really don't care about using the body because we just want to send back an error telling the user of this function that we did not find that resource in the last case. We basically also return an error but then we return something like this where it says error. Unknown. Okay, So how do we use these in here in this particular case? I think I'll give you a hint here which is using case case do so remember here, I could actually have case as the last thing in a pipe operation. In a series of pipe operation. Usually you put a case and you put something and then you basically do pattern matching on a particular output. In this case, basically because we're calling this function handle api response the first element to the case would be the output of these functions. So it's either gonna be this or this or this. So what we can say, well if I get an okay and then data, what I would want you to do. I want you to just for now display that we're going to do something more interesting later. If you get an error, it doesn't matter whether it's this case or that case physically and then we get an error message here. Then what I want you to do. I want you to say and we could use what's here because it's a string error card and then display the error message. This is basically how you know, I I really think you know Alexa is really elegant and beautiful with the way it puts the code because if you, you know if you're able to follow this here, a lot of elixir apps are really just what you're seeing in this particular block of code. They can string interpolated but give it to a module function within the module. Take that then take the response in a pattern matched fashion here by passengers through different functions and then at the very end use a case statement to basically handle the response. Now you may be wondering could I have used another function here with different heads. Absolutely. But you know I see this pattern more common than not. That the very last thing in a series of pipe operators is a case statement where we basically uh you know do pattern matching, pattern matching, pattern matching and then the very last step we do a case, it makes it a little bit easier to read in my opinion for the user who's reading those dysfunction. It's like okay I'm handling the api response and then it's a case based on the case of year. It makes it a little bit easier to read what you're doing at the very end of this particular uh scenario, awesome. So um yeah so this is basically how we could take a piece of data and then, um, which is a string and then decoded using, using elixir, awesome. So I'll stop here for today's lesson and a let's do some more interesting things with the API later. Let's practice more of our elixir by integrating with more things in the Star Wars. Api using electrical. Thank you for tuning in and see you next time.
138 Views 0 Likes 0 Comments

In this lesson, we learn how to make use of the responses from HTTP requests by decoding their JSON into Elixir data types.

Comment
Leave a comment (supports markdown format)