Transcript for:
Introduction to Microservices and APIs

microservices are a way of invoking a service remotely across the internet this gives you access to services and data that may be impractical for you to host on your own machine in this video I'll give you an overview of how you access and build your own microservices so let's get to it well what exactly is a microservice microservice services are also called apis you may have heard that term and API stands for application programming interface these are lightweight services that are available via the internet using a simple interface and protocol they provide a wide range of services to applications and today in some sense they are the glue that binds many of the internet systems together you know Twitter and Facebook and all these things they use microservices to carry out things on your behalf other examples are having a microservice to send a message translate some text maybe search a streaming service for our program access some data like say covid-19 data consult Airline timetables gather Geographic data food nutrition data or weather data and so on any sort of data that you can imagine is probably available somewhere using a micro service on the internet sometimes they're used internally as I mentioned Facebook and Twitter they use microservices internally to the application and they're not available across the public internet so the next question is what is an API what is an application programming interface well you can think of it like a function call with parameters and a return result now as you know all programs use function calls and functions the neat thing about microservices is that they're like a function call where the function runs on a different machine and your call on the function goes via the Internet to run it so in this example you can see that we have a program client on the left that's calling a a function we call add up and all that add up does is add two numbers together and return a result the call of this function is translated and sent across the internet where it's received by a microservice server and it collects the parameters calls the function takes the result and returns it back over the internet to your program and that in a nutshell is how a microservice works there are many ways to implement this somehow we have to package up those parameters at the client end and send them over the network in a message to the server uh the server calls the appropriate function with the parameters and then packages up the result and sends that back in another message there are many ways to communicate the parameters and return the value between the client and the server standard web protocols like HTTP are commonly used for this but instead of a request for a web page that you might do from a web browser the request is via program other than a present and the parameters and the result are returned instead of the web page there are a number of standard methods within the web protocol that are used to package up the parameters and return the results one way of doing this is to make the parameters part of the URL that sent to the web server with an HTTP get request another way is to use a structured text encoding of the data such as JavaScript object notation or Json or the extensible markup language or XML to represent the parameters and the result this is uh the incentive the server in an HTTP post request the result can then be returned as plain text or as a Json or XML or some other proprietary encoding we could also use a combination of these send the parameters in the URL get the result in a Json string and so on so what is Javascript object notation or Json well this is the syntax used in a JavaScript program to represent a JavaScript object a JavaScript object is a little like a python dictionary a list of key and value pairs the keys are like variable names and the values Can Be Strings or numbers or booleans or arrays or they can be in other objects another object in other words it can represent any JavaScript data structure even if it's nested this is a perfectly good way to transport API parameters and results because it easily transforms data to and from the simple text form and text as you know is easy to transport across the internet XML which stands for extensible markup language is a language for describing any data structure it's actually much more complicated than Json because it can be extended with a what they call a schema that's yet another language that describes the words used in your XML unlike Json which is related to JavaScript XML is not related to any particular programming language note that this is just a fragment of XML there are other parts used to wrap this into a complete document but it's a little too complex for me to go into here there are many publicly available microservices on the internet implemented using web apis in order to discover the details of all these there are also API search engines and aggregator services that provide access to a large number of microservices they categorize and manage the different services to make it easier for you to browse or to search for the particular service you need some of the aggregators let you experiment with the a part pi and provide a Gateway which you have to pay for to the API itself to make it very easy to use this is an example that is something called rapid API just search for that and it's very easy to find and it provides access to a large number of apis you can get all the details the documentation and so on they do have a free account which we're going to use in some examples but you can get more facilities if you're if you pay you can very easily access web microservices from a program the Python programming language has very good support for HTTP as I mentioned in the video on application protocols and it can be used to access microservices so as an example I'm going to use a service that provides details of the weather anywhere in the world this service is called open weather map and the API endpoints in other words the URL that we use to access the service looks like this the in this example the XXX part is a string you get from the website openweathermap.org after you sign up for an account if you want a lot of fancy Services uh like forecasts and so on you'll need to pay but for our example we can use a free account the QQQ part in the example here is the city name and the country code for example Sydney commer Au is for Sydney Australia now if I do an HTTP get request with that URL it'll return a Json string that represents a data structure containing weather details for that City so let's do a real example this is a very small Pro Python program and it accesses the open weather map service that gives us information about the current weather and uh if I get through it line by line the first couple of lines are importing some modules the python requests Library which lets us use the HTTP protocol to fetch URLs across the network and the Json module which decodes and encodes Json strings then the next one is importing a variable from a file called API key and I'll explain that in a moment then there's there's one line a long line here that invokes the requests module with the get method to invoke the the get operation of an HTTP request and that's the URL this uh this big long thing here is the URL to fetch some information about the weather in Sydney Australia is where I live now you could change that to be any City and country code this part on the end here I've appended this variable API key now when you sign up for the service the open weather map service you get a an API key which is a long string of random looking digits and letters and this is your sort of personal password if you like for your account to use the the apis and I've put that variable into a separate file so it's easy to change in one place so that file simply contains the lines API key equals and the long string now then that requests operation then goes out and accesses the open weather map API returns a Json string which I then print out here with this this print statement so let's see how it works so we invoke it with python3 I've called it stage one and here it is well it's uh obviously got a lot of information about the weather it's got some coordinates of uh where I was asking for the weather it's got some information like the sky is clear and so on it's got some funny temperature numbers and so on not very human readable though if we look at the documentation of it you can see that a lot of the information is for this particular place is included in the main section you can see there and I know that the temp value gives us the current temperature so let's have a look at that first we'll convert it from the Json string and now we can get out the the temp value and so there it's printing out the temperature well the reason for the 300 is that it is in degrees Kelvin from absolute zero so we have to adjust this value again to reflect that and that's pretty easy is 273 degrees 0.15 degrees and there it is so the current temperature is 26.85 something something something 23 so we can and there it is if we round it up to a full degree it's 27 degrees roughly I think you'll agree that that simple program was pretty easy to um to write once we read the documentation of the open AI open API and you can find all sorts of information about the weather in your location also this API provides information about forecasts and so on you can also build your own microservice using web protocols for that you need a web server a program that listens for HTTP requests and returns results I'm going to do an example of this but I'm going to use a really super simple web framework called bottle and I'll show you how that works in a moment to build your own web micro service you're going to have to have python installed and access to the command line if you don't have these you can still follow along and I'm sure you'll get the idea to install the bottle nodule you can simply download the single file bottle.py from the website bottlepi.org let me work through the example from the bottle page the first line here is importing the necessary parts of the the bottle module and then the next line is what we call a decorator and I'll go into that in a minute but what it's saying is that your bottle web server should respond to URLs that have slash hello slash and some name on the end and pass it to this function I've got here which is index passing at the parameter that was the name String and all it does is it returns a filled in template consisting of some HTML it's got a a bold hello the name that you specified and then normal text and then an exclamation mark and the parameter here is the name and the last thing to do is to actually invoke the bottle server with the run and saying it's going to be serving at localhost with port 8080 which we can get at locally using a web browser if you're new to python you might be wondering what the at Route line does in fact what lines like this do in general well this is an example of a python decorator it occurs as the line before a function definition and it tells python to wrap the function below inside another function that is the name of the decorator here's a simple example let's say we have a function add up and we put at Trace as the line before it so it has the effect of redefining the function as a call on another function with the original function as parameter now this is useful in many ways the wrapper function can do things before and after calling the original for example you could produce some debugging messages on a file or in our case it allows our function to provide a microservice so let's go ahead and build a microservice that adds two numbers together and Returns the result we're going to pass our parameters in the URL and get the result back as a Json string first we'll look at the the client our client looks a lot like the previous example we import the requests module and the Json processing module and we call requests.get so it'll do a get request get HTTP request with that URL and you can see I've passed two parameters a equals one and b equals two then the result comes back it's stored in R which is going to be a Json string and we then use the Json module to convert that string into the data structure and then print it out on the server side it's also fairly straightforward using our bottle web server as you can see here it Imports a whole lot of things from the bottle module and imports the Json module then we've got a route decorator here which will recognize in url that has slashed add up on the end it calls this add up function when it recognizes that URL it extracts the two parameters A and B from the URL prints them out just some debugging and then does the calculation and Returns the value as a Json string then lastly we invoke the web server by using the Run function it's going to be at Port 8080. let's try running the server I have to run it asynchronously so the server stays running in the background and there it is so it started up that's its process number there then it printed out some information saying the the bottle servers listening on port 8080 and to hit Ctrl C to quit but I still have control here so now I can run my my client and there it is now there's some information from each of these mixed together so there's some logging information here which is this which was printed out by my server there is other logging information printed out by the bottle server itself and then there's the result printed out by my client our example there is using the bottle web server is just definitely not scalable it could not would not scale up to a large number of clients so Amazon and others provide lots of things that help you scale it for example Amazon provides a thing called the Lambda Cloud function service that lets you run functions in various languages and access them using an API Gateway uh here's a list of all the the fancy services that the API Gateway provides all aimed at helping you manage and scale and so on your uh your web service microservices or network apis are now a huge part of the internet as I said it's it's really the glue that binds a lot of it together many systems are implemented with the help of microservices and some Services could be private totally within an application but others provide Public Access and there's a wide range of them which you can find out you can discover using services like rapid API microservices are implemented in a number of ways they're just simple client server connections with a parameter and result transferred over the internet using UDP or TCP or they may run above above the web protocol http you can Implement your own Microsoft service easily using a web server or a cloud service such as the Amazon web services Lambda system or if the service you want is already publicly available you just invoke it directly using a an HTTP request or you can use one of these API aggregators which lets you access it even more easily so I hope you enjoyed this very quick introduction and overview of what a microservice and an API is and how they work thanks for watching [Music]