Golang Bubble Tea - Fetch HTTP Responses | Part II

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up YouTube today we are going to be building a very simple program that makes an HTTP request to a server and then reports the status code of the response [Music] I'll show you a little demo of what the end result looks like let's do a little go run and charm.sh is the endpoint that we're hitting and 200 is the status code that we're getting which is good our website is live that would have been very concerning if it was a different number but that's pretty much what we're building we're going to build it out with bubble tea and we're going to run into a little bit of information on commands and give you some context on how you can use them effectively in your own programs without further Ado let's get into it all right let's start building out our commands tutorial if you haven't seen part one go back watch the basics video first this one's diving into T commands a little bit more it's going to be a little bit more complicated a very strongly recommend that you see our video on the basics or read the basics tutorial before watching this video and following along disclaimer complete now let's talk about commands this is the second tutorial for bubble tea covering commands Okay first one was Basics which we went over in the last video and for this tutorial we are going to be building a very simple program that makes an HTTP request to a server and reports the status code of the response I vote that we just get right into the code so let's toss aside Firefox and get in there okay let's do HTTP codes that's what I'm going to call it I could call it commands tutorial but that's that would be too too sensible so we're going to go mod in it HTTP codes there we go hide my mistakes okay cool and then let's go ahead and create our mean.go so here we'll just declare that it's package Main and we will go ahead and add some imports fmd we'll add net slash http we will add OS and we will add time and then finally last but absolutely not least we will add bubble tea github.com charmbracelet slash Bubble Tea perfect now let's create a const for URL that is going to be our very own website and let me just zoom that in a little bit to make sure you can see it clearly without further Ado we'll get now let's start declaring our model so this is going to be a data structure so we'll just a lot of struct we're going to have a status which is going to be the status code that we're getting back and we'll have an error okay so we get any errors okay let's talk a little bit about commands and messages so commands are functions that perform some kind of i o and then return a message for example a key press or hearing back from a server checking the time taking a timer reading from a disc Network stuff all of those are also Under the Umbrella of IO and should be run through commands so the commands are basically under the hood a little go routine that's running in the background when it completes it Returns the result that might sound harsh but it will keep your bubble tea programs straight forward and simple so let's write a command that makes a request to a server and Returns the result as a message the code perfect and then let's define our Command it'll be called check server and it returns a type 2 Dot message this is the basic basic function signature that you're going to need for it to be considered a command it's basically anything that doesn't have any parameters and returns a t Dot message so go ahead and do that let's create a new little HTTP client okay create a client set the timeout to 10 seconds we'll get the response error is going to be C dot get and the URL and then we'll check if the error does not equal nil we're going to return an error message we'll show you some cool stuff we can do with that in a little bit and then finally we'll return a status message which we haven't defined yet perfect perfect and then let's go ahead and Define those types so status message is going to be a wrapper which is going to be an INT and then we're also going to have a type for error message and that is going to be a struct with an error struct error error for messages that contain errors it's often handy to also implement the error interface on the message so let's do it so func we'll do e error message we'll have error which returns a string and it will just return e dot error dot error perfect so now error message there we go just add a little context there just for fun for funsies and then here you can see that in our check server we're returning two different message types so they can be any type even an empty struct and basically the point of these messages is that we're going to be able to handle each of these cases when they come into our update we'll get into that a little bit later our model isn't a t dot model yet because we haven't actually implemented the interface but we're going to implement our init method first so here we'll get that get that going get a knit in there returns a t dot commands and then here we are going to return check server the initialization method is very very simple we're basically returning the commands that we made earlier check server and we're not going to call the function because we're actually returning the command not the message so the bubble tea runtime will do that when the time is right it will call check server when it is time let's talk a little bit about update function so first I'm going to write down the little signature here get our message in there which is type T Dot message and then we will also return a TDOT model TDOT commands and let's talk about the update for a second here internally commands run asynchronously in a go routine the message they return is collected and sent to our update function for handling remember those message types we made earlier when we were making the check server command we handle them here so the status message the error message we're going to decide what happens when those come back with information in them in our update so this makes dealing with asynchronous operations super super easy so first we are going to do a switch on the type of the message so you do message.type and we'll get a case for status message which is one of our custom types that we've defined and in this case we're basically going to set MDOT status is equal to the message that we receive and we're just wrapping it in int in the inch type okay and then we're going to return M comma T dot quit because we're only going to run it once basically we're going to run it a single time and then in case of an error so if we have an error we're going to get an error message back and then in this case we can set m dot error is equal to the message and then we can return M comma T dot quit and this is where it's very very helpful that we implemented the error interface for error message because we can just Define it we can just directly assign that to our error field and then let's handle our key presses so T dot key message that way we'll have some way that we might want to quit so we'll do if message message.type is equal to T dot key control C then we are going to return M comma T dot quit this is a bit of an older way of doing it so I'll show you a another way that you can actually do that that is a little bit more up to date I might even PR this later we'll see I mess around PR it a little you know we can do if message dot string is equal to and then just compare it to the actual string representation and then we would do return M comma T dot quit and that will do the same thing so these are equivalent gofund is very confused but it's fine and then finally we're just going to return M comma nil and then finally let's build out our review function so the view is super straightforward we're basically going to look at our current model and then build our string accordingly so here we'll do the standard little signature of the function that we need to implement and we're going to check if I'm not error does not equal nil we are going to just return the error message sprintiff and we're going to do n we had some trouble and then m dot error okay and then otherwise we're going to give it a little bit of feedback so in the meantime while we're reading potentially for the status message to arrive we are going to have a little string that tells us that we're checking it's working it's in progress checking and then let's add the URL as well perfect declared but not used that's fine if MDOT status is greater than zero so if we've gotten something back then we'll also add that to the string we'll add the status code and the status text so we'll do m.status and we'll set http dot status text we'll have a string that gives us the status code and the text surrounding that status code and then we will also just send off whatever we came up with above for rendering do that right here add a little new line at our string and then add a couple more new lines for fun mostly for fun and the last step is to create our main method let's go ahead and type that out speedrun and do if error is equal to T dot Newport wow new program model dot start so in this case you could also use if you had an initialization like new model initial model you could also use that function here but in this case we don't have that so we're just doing a little empty model we're fine with the default values that'll give us and if error does not equal nil so if there's a problem starting it let's do a little print in action I will say uh oh there was an error one more thing that you need to know about commands is that commands are defined in bubble tea as a command with a function signature that has no arguments and returns a message so they're functions that don't take any arguments return a message which can be any type if you pass arguments to a command you just make a function that returns a command for example there is this command with ARG where it takes an ID and then it returns it uses the ID later down the line and a more real world example might look something like this checksum URL where you're given a URL string as an argument you return a TDOT command and then here you're basically doing all of the stuff that we had already defined in our example which is creating a client doing a get request for that URL checking if there's an error if so returning an error message otherwise returning status message and basically the only thing that would change from our example here is that let's see here we would have you would be able to actually call it and add your whatever information that you needed so in this case if it was the URL actually I can just use the URL const it would look something like this as an example where you actually need to have arguments and you end up actually just returning a TDOT command from that function so that you can actually pass in that data this is how you would call it in your bubble tea programs for that to work I hope I explained that well let me know in the comments what you liked or didn't like we're always very open to feedback we want to create content that you are genuinely interested in learning about and things that will add value so please don't hesitate to let us know if there's anything that you feel could be improved from here you can check out some of the example programs you can check out bubbles start working to tie some of those together we have some projects that are featured in our uh Bubble Tea in the wild section of our readme and we also have a an upcoming repo that will have a lot of project inspiration project ideas and some example implementations of some different projects as well and that should give you a good amount of context don't forget that our own projects are also built with bubble tea so you can always look at charm or soft serve if you want to look at more full-fledged examples on how we use Bubble Tea in production thanks again for watching congrats if you made it this far and definitely let us know what you're building we'd love to share it with our audience and help you find contributors if that's what you're looking for we'll see you next time bye [Music]
Info
Channel: Charm CLI
Views: 8,140
Rating: undefined out of 5
Keywords:
Id: Zyp_NrVjRvA
Channel Id: undefined
Length: 17min 7sec (1027 seconds)
Published: Fri Oct 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.