Azure Functions: Step by Step Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so the ability to write called without setting up any infrastructure on which to run it sounds pretty good doesn't it so no salvos nor virtual machines no containers even not even a development environment on your machine sounds pretty good doesn't it so if you stick around today you're gonna learn exactly how to do that [Music] well hello wherever you are when ever you are where am I Melbourne Australia and when as it's November 2019 well as I alluded to in the introduction today we're going to be talking about several us compute and in particular functions on Azure or as a more well-known as your functions so today we'll go through a bit the theory I've got a couple of very quick slides on that and more importantly we're going to delve into a practical example on how to use as your functions in your own applications and the pattern I'm going to teach you today is hopefully something you can take and be use elsewhere so I think we're just about ready to get started just before we do that one last thing by way of an introduction if you want to get notified of one of my videos that I put up please remember to subscribe to the channel and Bing the little bells to get notified and if you like the video please give it a like but I think other than that I think we're ready to to get started so yes we're going to be going over as your functions in c-sharp step by step as always no skipping no jumping so what you'll need to for this video you'll need an account on Azure you will need a web browser and internet connection I'm assuming if you're watching this on youtube you already have that sorted out and about 40 minutes of your time so what are as your functions well those small discrete pieces of code and what I mean by discreet is they do one thing usually in response to an event so they're not long full fully-featured sprawling and applications that comprise multiple classes and all that kind of stuff that's really not what as your functions are about they are about doing one thing doing one thing well in response to some kind of trigger so this should be relatively small and relatively compact you could almost think if you've heard of something like microservices or not you could use them in the micro services environment potentially but they're kind of that seems sort of ideal they do thing they do really well that's basically what your functions are and the service I don't personally particularly like the town several is because ultimately the cord is going to sit on a server somewhere but I suppose what they mean by that is as a developer some of the writing and using as your functions you don't have to worry about setting up a virtual machine setting up containers or anything like that you just basically write the function in Azure and Sewer takes care of the rest of everything else and that includes scaling it out and you know making sure it can deal with the demand you don't worry about any of that stuff as part of the is your free sign up if you second out signing up for Azure and you get 1 million as your function calls per month so depending what you're doing you may or may not bust that limit I am certainly not going to come anywhere near to that so in effect what I'm doing here today is free and this as I said as your functions react to triggers there's a number of different triggers they can react to the two main ones are HTTP traversal no surprises you make a HTTP request to your trigger probably providing it with some data maybe not and the function will then take that data or take that try go and do something with it maybe call another API or do something else the other trigger from my perspective is the time of trigger that's the one we're going to be using today so we're going to execute our function on a schedule could be any time once per day once every minute doesn't matter but that's what we're going to use hours for and and then you've got all these other triggers that you can use as part as your so as your has a number of service offerings the event hub is one of them you can trigger up functions from an app you can trigger functions for a number of other azure services which I'm not going to go into today so what are we building so we're also going to be using and as your function and in this instance it's going to get triggered by a timer once per day and what it's going to do is it's going to go to stack overflow or stack exchange and query the question repository for any new questions that have arrived based on a particular search criteria within the previous be now the idea behind this was actually I was a question I was asked in a job interview about how would I use Stack Overflow as part of developing a builder community builder as part of developing a developer community building a developer community to say so having to use Stack Overflow to build a developer community and my can answer was well I would write some kind of looking but called that would go to stack or floor pull down any questions that people had had about our company's product within the last day and would then notify me to go back to our little schematic here notify me via slack or could be email or it could be something else and with the number of new questions that have arrived and then that would enable me as a developer community relations person to go to stack or floor get the questions and answer them for the developers and thus being quick and responsive and all that kind of stuff so again you may not want to use Stack Overflow is your trigger point or not your trigger point is your data source and you might not want to use slack as your endpoint you may want to use a messaging API or something nonetheless the pattern I think it's quite a useful one so that's what we're going to build today so make use of the stack exchange API and we'll make use of the slack web hook in order to say nice just to it so enough talking let's start coding okay so the first thing we want to do is set up slack now if you're not use slack before it's a collaboration tool think of it just like a group chat tape application it does more stuff than that it's a bit over simplifying it but it's used a lot by developer teams to collaborate with each other be a chat and you can separate those chats into channels and are you set you define how you want to structure your channels it could be along and what products your develop or it could be based or around your development workflow of Department or something like that so not important for this video but you can have multiple people chatting to each other based on channels basically it so I'm not going to show you how to sign up for an account I mean that's just insulting your intelligence just go to slack column sign up it's free and once you've done so you'll be presented with something like this I already have a few channels created in my slack instance but I'm going to create a new channel just for this application so all you do is create click channel or call it something like because your functions and will click create it's going to ask us to add people gonna skip that for now what we then want to do is we want to then add the ability to send messages to this channel from another application so what we do what we need to do is add an application so if we click if you click on get rid of that and click the little information icon you'll get this menu up here just click on add app and the one we want is incoming web hook so here you are if you start taping an incoming you'll see this one here and this just tells you what it is allows you to send data to slack in real time click install and it just gives you a bit more detail about what it is and we simply just click Add to slack so just ask you what channel you want to post to so I have my existing channels here but I want to select the new as your functions channel I've just created and then we simply click add incoming webhook cool and it gives you a bit more detail about how you use it don't worry I'm gonna take you through that but the one thing you will want to take note of is this web hook URL so just copy that I think I had one from before so to get rid of that one and just paste it in here so just a notepad with this way I put because we'll reuse that or they so on I think that's about it it's just alderman yeah you just want to see if say things you can change things like the icon that will appear when the messaging comes in and all that stuff I'm not going to bother doing that here that's just you can do that yourself play around with it and when you have the time to do so cool so that's added to our Channel hopefully cooking on I hate when people do that you can't actually hit back I've really annoys me so let's just go back to slack calm my workspace is beneath this all and yeah here's others your functions channel and you can see we've got a little message and I added an integration to this channel I was cool so that's that set up what we wanted to know is just test that this is all working properly so let's do that now so in the interests of keeping this completely several us we're going to use a an application on a website called API test on test calm now usually I would recommend you use something like postman I have Postpile installed on my desktop again and this video trying to just use all web-based stuff and this is actually really good it's a really good site allows you to basically taste AP ice which is pretty awesome so all we want to do is set this to post because we're actually posting data to our web hook we want to get our web hook URL copy that and paste it in here and then our post data so this is just a very simple JSON string so curly bracket curly bracket double quotes text as our key call on and then our data so anything you want the message you want to send to slack so hello on API tester so before we click anything else you would click on test before we do that let's just move this out the way here and here's our as your function if we click test and sing about cookies it's closed up then all things being well there we go so we've actually got a incoming Red Hook awesome form a third-party app in this case API test and it gives you it's a really good website gives you all sorts of feedback and how y'all performed and all that sort of stuff so don't need to delve into that anymore this was really just to test the fact that our API Norway perc is working correctly and is okay so now what we want to do you'll be pleased to hear is actually create our is your function on as you are now I've just logged in to my as your portal here and this is what it looks like today and I'm not going to take you through how to sign up for another account it's pretty straightforward you do need to provide a credit card just be aware but if you're following along with this tutorial and you've signed up for the free account then you're not going to incur any charges unless you make more than 1 million function calls per month to the function we're going to create now that's up to you obviously if you do that I think it's unlikely you will and you're therefore not going to incur any charges the other thing I did want to say as with most cloud platforms as you're as always having new features it's always being slightly redesigned and optimized so just be aware that you know I'm making this video today which is sometime in November 2009 teen if you're watching this video any time later than that you know in 2020 or whatever what I'm going to take you through might look slightly different it shouldn't look so radically different that you can't follow along but just be aware that you know this is made in a point this is made at a point in time and it will probably change subtly or time to just be aware of that anyway so overall as your portal we want to click on create resource and what we want to search for it is function app not whoops I didn't I didn't I don't know what was happening in my mind a function app click create okay so subscription the page you go that's my page you go subscription what resource script resource groups are just groups of resources I'm going to reuse an existing one by all means create and you want if you want that's fine the name of our app are let's call it stack as a stack over floor and then aggregate a stack AG that's all good it's unique on as your websites which is cool how do we want to publish it do we only just publish cord yes that's what we're going to do here you can publish a docker container if you want what I'm going to go with that option the runtime stack now this is something that didn't mention in the introduction and I probably should have you can use a number of different languages with these your functions obviously given my channel we're going to use C sharp on donate call but if you're a Java coughs not a Python person you can of course select that but the game this video is on dotnet cord and c-sharp the region I'm in Australia might so my regions actually Australia South East but I'm gonna select Australia East because we're going to set up something called insights and for some reason they are not available in this region and it starts complaining a little bit if you have a function app in one region and your insights in another region it still works better just complain so enemy for an easy life we will select Australia East move on to hosting storage account so with a lot of applications with a lot of services you may incur I mean need to store data somewhere so that's why you need a storage account again with the free package stuff you get with Microsoft that sign up they shouldn't really incur any charges here so we're just going to create a new one that's all good the operating system that I actually think this a really strange option here because again we're talking about Salva this computer why do I care whether it's Linux or Windows I shouldn't care about that I don't care about that but we'll leave it as Wendell's and then finally the plan though again one of the great things but as your functions are is that you don't have to worry about setting up how you scale out virtual machines or anything like that it just takes care of it for you and the consumption plan is perfect for that so we're gonna leave it on the consumption plan you can select other plans but that requires a bit more safer and for me sort of defeats the whole purpose of just writing code so next we move on to monitoring and this is what I was talking about the application insight stuff so we're going to create a new range of insights for this application which is cool and we're going to put it in the Australia East region so yep we want insights yes sleeve defaults that's all good move on to tags we don't need anything like that and then finally you click create and it will just give you a bit of a review screen with everything they've selected it all looks pretty good yep so I think we click create and that will go away and I'll start to deploy your as your function and it will probably take a minute or so so you'll get notified when it's done so I will just edit the video appropriately to jump to the point at which it gets created but when you're doing this in your own in one environment or your own you know session it will take a bit longer than how it will appear in this video okay so we're just got a notification our deployment succeeded I'm just interestingly oh I did get a very strange message here I just interesting know that like the deployment preceded that looked like it succeeded so let's go to the resource so don't panic get that strange messaging clicking on it it looks like it's flying sort of the stakes as now into the is your function a configuration page and then we're going to start using this is a launch board to start writing or called okay so we're going to start writing our code now we'll take a look at the different environments you could potentially use to do that but let's step through it first of all so the first thing you want to do is on this functions tab here you just want to click on the plus sign and yes as I'm saying it gives you some options about the environment that you will use or want to use to write your code now I would suggest if you're doing a lot of this stuff that you would probably want to use something with Visual Studio or be escort to do it in interest of keeping this all in browser and totally service we're going to use the importing option which is fine for doing small bits of coding that's totally okay but again if you did want to do some more and hands-on stuff and you were doing this a lot then I would probably recommend using a proper development environment nonetheless we're going to click on import and then continue and then as I said before you can create different types of as your function so the way Perkin API triggered event functions timer or there are other templates that you can pick from again we're going to select timer and click create and I'm sure that basically creates our type of triggered function for us named Titan or trigger 1 and it puts in some default code into this file called run CSX now if you go over here you can actually have a look at the files that make up there is your function so we have our main code file which is one CSX and then we have function dot Jason which has some configuration in it and we'll come back to that when we start talking about configuring our time or schedule you can actually see something here that alludes to that but for now let's jump back to run CSX and start to write our first function to call the slack web hook so the first thing we want to do is add another using statement to system text because we will be making use of that later and then we want this main one method to run asynchronously and so we just put in the e sync keyword as so and we also therefore need to pass back a task so public static async task run not going to go into the asynchronous program model within c-sharp but basically makes use of the async and a wait key once and we will make use of it through the rest of this cord now actually just to take you through what we are going to do we are going to create two more method functions both of which are synchronous one is going to make a call to slack with the message and the other one is going to make a call to stack and get a list of questions so what are going to go backwards and we're gonna test our very similar and point names a will attest and I'll make sure I get it correct our slack end point foster one we just set up to make sure that we can get we can trigger it from within or as your function so in order to do that we're going to create a new synchronous method and we call it public static async passing back a task and this time but I'm gonna give it we're expecting a string to come back this time and we just call it anything you like so call it a neat stack request and we're going to expect to pass in a string now one thing it does do but it looks of it is it will actually caller called the object instances here and give me the mistake it will you know it's thing it doesn't recognize that so that that is something so you should be able to see whether you've made any mistakes using that if nothing else so this is our first synchronous method we're going to make a request to the stack endpoint and the first thing we really need to do is make use of a HTTP client object and we're going to use that to make our request to our endpoint so we're going to put out within a using statement we're going to create a client HTTP client I'll type var initially and then that's just a new HTTP and again you've got to be really careful that the case sensitivity and all that kind of stuff otherwise you'll end up with some difficult arrows to debug but nonetheless you will actually when you can you can save and compile that you will actually get some open in the console down here so you can generally find out what mistakes you've made and the reason we're putting this within a using statement it just means it will be automatically thus paused when we no longer need it and I'll be cleaned up cool so the first thing we want to do within this method is create the string data that we want to attach to our HTTP client object when we make the request so create another variable we'll call it request data and we're going to use something called string content to create this string so first thing we want to pass in is actually our JSON payload now if you remember when we tested it within the web client we used just before and the JSON string is open curly bracket single quote this time text close single quote call on and then what we're going to do is we're actually we're not just passing the message here as part there's the actual payload that we want to use okay and Italy people just need to make sure that we actually yeah we cause a headquarters as well so this is really gets a little bit you have to be quite careful how you take this and I mean we just want to close off with a following a single quote and the choral a bracket maybe go so that's basically our messaging string completed and then we have to add a couple of other things in here just to make sure the specifying the right encoding and data types so in here you just do encoding utf-8 and then the data type which in this case is application Jason and that should be it so again if we get arrows we will come back to this but let me just check all of us about creating our string message which is basically this stuff here by passing in the key of text in single quotes and then we're appending our actual message within another pete of the value effect and live within another peel of single course so the next thing we then want to do is actually make a call to our slack endpoint we're going to do that by creating a response object [Applause] which has basically made or populated as the result of making a call to our post async method on our client so again we're using the synchronous programming model within dotnet so you need these sync await pale keywords and order for all that to work and basically a weight keyboard is basically just saying we're gonna make a request then we're going to come back to this screaming eventually get a response so we're gonna make use of our HTTP client object here and then we're going to call post async method on that know if you're making a get request you call get async etc etc now all we need to really pass into this here is this this string here which is our should be familiar to as our slack web hook now I'm gonna prepend the string with the dollar sign which basically means this is a kind of literal string and we don't get any arrows based around and delimited characters that cause the the string to arrow out don't unpack that for take that I actually want to show you because we're not running it in Visual Studio code but just take my word for it but you need to put this dollar sign in ahead of the string so it just takes it as a literal string and then the only other thing we need to pass in is this request data here that we've already created this is just basically telling this object the data type and the payload that it's going to expect as part of the post request so it's quite straightforward actually so then the last thing we want to do is just pass back the string result let me get back from that call and again use the await keywords on our response a little read any string data that we get back I mean content get back from it and we're just going to read as string you think and that's just reading the contents of this any content in this response object here and then we simply pass back the result and then the only thing left to do really is actually make a call to this method for math in our main method I mean run method will do that up here so oh wait I'm just copy this and we'll pass on a test message no we will eventually when we make our to our second function and make a call to stack or floor we will then actually rework this message but for no but it's going to do something like hello long as you are now you can also see that we're not actually really doing anything with the string result I've just put this Pat on and just because in case we do want to use it subsequently but really all we're interested in is making a call to our stack a stack so you to our slack endpoint so let's save that and this is when we will find out if we have any arrows down here looks okay I think when I send the log sign on the console it's okay yep so the yep I think that looks thing is okay so all we need to do now I can minimize this kind of seem to do that click on that is if we bring back up oh yeah there's our slack application let's get rid of that and let's if you can minimize this cool no let's run it and this is not operating off our timer function or anything like that at the moment we're just going to run it manually and if everything works correctly then we should see and there we go actually I didn't notice that yeah I think we saved that it obviously ran it so if you click it let's just change that and we'll do a save and run we go so that is working so that's great so basically what we've done is we have wired up our first a synchronous method that uses a HTTP client object we've constructed and some string content which basically tells the client about the payload that needs to send and we've made a call to our web hook with a static message so that's good that's all wired up so what we need to do Nick is move on to the next function which is making a call to stack or for an order to then send a proper message back to our slack endpoint okay so before we move on with coding up our second method that I'm just going to take you over to the stack overflow API so if you just type in stack overflow API to Google you will get this the first link should be documentation to stack exchange API stack exchange is just the kind of pain and organization of stack overflow I believe they run a number of different websites stack a little I think it's probably the most famous one for those of you who have not used stack or floor it's basically I can't believe if you're watching this you haven't used the bit if you haven't used that it's basically a question-and-answer platform for developers so I personally would have been very stuck without it general if you come up against the problem it will usually have a good answer for you and the very usefully provide you with an API that allows you to hook in and just create that database via api's which is awesome and that's what we're going to make use of here you can see here as I'm scrolling them there are lots of different methods you can call but you're just going to make use of the Search API here and it gives you a bit of background about what it is and how you use it and you can even try it down at the bottom of the page so it's entirely up to you what you want to search for I'm just going to use the in title search criteria you can put anything you like in here let's try something like RCS which I'm quite interested in and we'll run it and you can see here I've decided before I run it you can see the the URL that's generated for you which is really nice and click run and it will return back a JSON payload with all the relevant questions that have RCS in the title ok so let's take this JSON payload and let's just see if we can let's just copy it into something like JSON editor on line and we can take a quick look at the payload and how it's constructed because that will come in useful later when we come to pausing it no I would have thought that you could have copied this P Lord somehow without having to do it the way I'm doing it here which is rather painful but nonetheless I've I've done it that copied and we'll just do it use something that Jason edit are online which is my preferred Jason editor there we go so just get rid of the default Jason and paste that in and make sure it's well formatted and then we just push it over to this next bundle which I I really like yo so you can see here that we had a RT an array of 30 items got it returned back so each one of these represents a question that has RCS somewhere in the title so there we go Arceus January it's bad prediction blah blah so it's cool so the idea will be that we will make a call to this API and we'll say I want to I want you to get me all the questions that have been asked in the last 24 hours with RCS in the title so the call we have just made they are a test called we've just made there has only put the search criteria of RCS and their title what you would need to do in order to just give you a get a result set for the last 24 hours is make use of this from the year and you know it quite useful e gives you a calendar drop-down with the ability to select a date however it does get a little bit more complex than that when you actually come to using it from a programmatic point of view so let's just select let's select the third of the pod of November will rerun it and you can see there are no questions that have been asked within that time period that's all okay but what you will notice it has again very usefully formatted the API string that we would need to call and you can see here that this attribute here from D has given us her cues keeps going to another page or you can see is the fron date is this rather strange string here that looks nothing like a date or time string now what that actually is if you go back up here to the documentation it does tell you about the date format that they have decided to use and it's something called UNIX epoch Titan and it's the time and seconds from when Linux was released or something like that seems to be taking a while they go yeah there we go all dates in the API are in UNIX epoch time which is number of seconds since midnight UTC January the 1st 1970 which is but we owned but ok fine that's what they've decided to use so when we come to writing a function we're just gonna have to bear that in mind that when we make a call we're gonna have to convey out to that particular time format and make the call now in this instance when we have bring them back to our API when we made the test call we put in you know not the last 24 hours but the last few days and it came back with nothing so we probably want to play around with that and just for the purposes of testing an API so I think from memory if you put it back to something like the first of July we should get a result no we don't I move must be a bit further back let's try it April maybe go so he gets looks like one result back there let's just copy that and just test it looks like you did only get one result she's probably a good one too to start with so we might just taste that and move it over yeah you only have one one question or a ton back and with that created date so we might to begin with just hard called that string into our hey the next method call that we're going to write so let's go on and do that no okay so we're gonna narrate our Stack Overflow request and just looking at the cordon we've already written or I've already already written i incorrectly named our original method call stack the question really it should be slack request otherwise it's going to get very very confusing so we'll just first off rename our original function which is making our request to slack as slack request and now we're going to come on and write our function to create our stack overflow request now it's actually very similar to this so where are those exact identical cord I am just going to cut and paste to save me from typing it and more importantly to save you from watching me type it but anything you of course I'll type it step by step so the first thing we want to do in order to play off for our new function we are going to use jason so when you have seen already when you get the reply back from stack or before you get a JSON payload so we need to deserialize that internal object and we need to use the json soft library to do that now that's not included by default when you create an azure function so we need to include that library and the way you do that it is your functions is hash are double brackets and then the name of the library which in this case is Newton's soft jason and again just be really careful with the capitalization there if you get it wrong you may end up some arrows there is no semicolon at the end of that now if you've done any dotnet core programming it's very similar to putting our a reference in your si esta el c m-- sort of thing we're basically just telling the record to include this library or require it and then of course we need to make a reference to it here as well so you get an incorrect Newton soft dot Jason and that does of course require a semicolon in fact what I'm going to do is gonna copy this I think that's as identical I'm just gonna paste that in now we do actually need one other using directive in here using system dotnet which I'll come on to why we need that in a minute other than that all the using statements I believe we require a thing that looks correct cool so let's come on to rating our a Stack Exchange request so I'm going to copy from he'll a previous request and in this instance this Nathan's not going to take anything you can change that of course if you want to maybe pass in your search criteria I'm just going to hard code it in this video and I'm gonna change the name of this honestly to stack and I'm going to over floor and here as well otherwise all gonna get quite confusing between stack and slack so this is make a stack overflow request and I'll just close out these curly brackets and again we're going to create a HTTP client to make that request all good now one thing before we actually do that and one thing that's I don't know if I've seen it anywhere else but certainly on the stack overflow site if I just bring it back over I actually don't even really need to see it yes as you know it will bring back in JSONP Lord however when you call that API programmatically actually returns back the jason as a compressed pay lord zipped payload effectively so you can't work with it natively what you actually have to do is decompress it before you can work with it now that caught me out when I started using this API so we need to make some provision for that and it's relatively straightforward and that's why we needed to include this system dotnet using director because we make use of our of our class and in that namespace so an hour make Stack Overflow request we're going to do a bit of work in here and we're going to use this HTTP client handler object we'll call it something like a lot and spell it right and it's just the HTTP happened now I'm copying this because again if I make a typo and I want it to be in the same place throughout rather than just in one place because it'd be harder to track we're going to create that object now just make sure you put us let me call on them now otherwise you'll get forced an error and then we're going to set this automatic automatic decompression equal to the compression methods and then assess where you select the method of decompression that you want and in first instance we want to try and use gzip to unzip it otherwise we'll use the flate so I'm just copy this again because it's quite hot so basically we are creating our handler object and we're saying that we want to say automatic decompression and first of all the gzip otherwise to deflate and what we then do is we actually pass this object in to our HTTP HTTP client object when we created so it's basically saying okay climb if you receive a people would automatically decompress it if required cool so let's just save that we'll check the logs no there we go we're getting an error it's seeing a mixed a quarrel request not all called paths return the value that's okay but not got to that but yet because we are we aren't expecting each string to be returned them a lot yet they're taunting a string so that's Auto key otherwise it looks okay we're not getting any other complaints which is good so the next thing we want to do is make our request a get request using a client we don't need to create any request data because it's a gate request we're going to call so we don't need to provide any data like we did with request so I'm just going to copy this bit of cord here it's not new the only thing that is new is instead of post async we were gonna call gate leasing and in exactly the same way we're just going to paste in our string into that so let's go back over to prea we were playing with maybe on Stack Exchange I'm just going to click on that and that will give us the full URL you know that we need to use so I'll just copy that and go back over to sure and paste that in know what you me again want to provide dynamically as this search parameter here and you may want to provide it in through your method signature that's cool I'm not going to do that here it's fairly trivial to do but what we will need to do is dynamically provide this UNIX epoch value but we'll come on to that in a bit we'll leave it hard-coded for now and we'll rectify that in a bit cool and then finally it's actually going to be identical to the stuff here so I'm just going to copy it I'm just quickly go through what it does again so this is going to make a get and this thing turns get request to stack exchange it should get the table back if there's anything decompress it and then put it into this result which we pass back and we're using this and reducing and string method to do that so let's save that again and we'll just quickly look at the logs and this time you get a compliment compilation succeeded so we were tunneling each string back so they're getting rid of that original error that we had okay so that should be okay no the only thing we then need to do is actually make a call to our new function in order to do that we will create a adjacent object the result that we're going to pass the result into from our new method and because on the synchronous method we should use the await keywords okay now let's say that check out our logs now one of the things I will just point out we've got a second completion succeeded does great well I have noticed over tighten when you save it and check the logs and I've done if I'm doing something wrong but the logs stopped updating and we no longer get these messages so what I do in that instance is I just make sure I save my work I come out of Azure close the browser go back into it and it works again I'm not sure why that happens I think it's a bit of a bug anyway so what we want to do let's just change this angle how often is your with cold to stack or floor and we'll just make sure that even though it's compiling we'll just make sure that everything is still working the way we expect it to so here we've got loads of messages a little module again let me just save and run that and bring that back up and hopefully you get our new message coming through so everything is still working as we expect which is good so we've only really got a couple of things left to do now the first thing we want to do is we want to count the number of questions new questions that we get back and our payload and in order to do that we need to DC Eli's the JSON string that we get the ton back and then the final thing we need to do is dynamically create that UTC time string so that when the function runs every hour or every day that will be dynamically generated and actually the final thing we need to do is update our schedule which will set this function to run every day as opposed to every I think it's running by default every 5 minutes that's why we had loads of messages splaying up our stack screen and we want to obviously change that and set that tell those frequent time so again back over an hour cord Wendell what we want to do is deserialize our adjacent string now that's quite straightforward using the Jason dotnet library cool so I actually just going to rename this to a nice name it's a result actually because I want to reuse that other name for what we're going to do now so yeah again we're getting a decent result actually let's call it something JSON string okay so we're getting our JSON string back and we want to convert it to a JSON object that makes more sense so we're going to create a new object jason object and then this is where we make use of our and reason and soft library and there's a class and a cold Jason color belt and then we call D Co deserialize object using the American Zed instead of the correct s and we then and I've done better using this previously so if you're interested look at my Jason deserializing Jason video and here you can actually pass a class name of a JSON object that you want to DC Eli's too I'm not gonna do that so simply we just provide the dynamic keyboards or the the deserialize or pickups like what the object should look like based on the tabled so it's really powerful comes with a couple of downsides but nonetheless that's what we're going to use and then what we're gonna pass and we're going to pass in there's JSON string so just to recap we're going to create a proper JSON object which will allow us to count the number of items that we get back and in order to do that we use the deserialize and method on this jason can be our object and we are letting that DC eliezer dynamically decide what the object should be let's just save that because I just want to make sure I've not made any arrows looks okay now this may be an instance where this is stopped working maybe not but let's let's give it the benefit of the doubt from the woman looks okay we can come back and fix up if I have me the mistake and when I'm going to create another object don't really need to do this but just doing it step by step it probably makes it a bit some trouble Gunners create an object called question can't and let's say something new question camp and then what we're going to basically do is make use of our JSON object and let's go back here actually to our Jason tabled I just want to make this really clear to you we get this object back and that then has this items a key which then contains our array of questions okay so we actually have to reference if you look at here we actually have to reference this before we can start to count the items and side it so the way we do that it's very simple we just take an items here now this again this is real this use of the dynamic keyword can be a little bit problematic because this use of the word items isn't determined until one time a compilation time so if you made an error it can be can cause some problems but I'm hoping that we should be your key and then we simply just count the number of items number of objects but then that items re you go back here items is actually an array and that's denoted by this opening square bracket again watch my video on Jason if that doesn't really make a lot of sense to you so what would be nice is if we actually write out this count to our to our stack overflow not a stack overflow our slack or slack Channel so we got you I mean you can actually if you do this string literal and then we just rather than using that horrible plus citing the foundation stuff that I've done before you can just insert in your question counting here you have questions on and a stack overflow so let's see if that works let's save it first and let's just move this over here and let's bring up our getting a bit messy now let's see if we can run it and get that message so we're not getting the message saw those something wrong somewhere along the line so we're not getting anything in you so what I'm gonna do as I've said make sure you save us and I'm gonna kill my browser and I'm gonna go back in go to portal it one is you back to all resources and make sure I pick the right one because I got you cuz you can see here I've got a few that have very similar names and we'll look at our function again make sure it is the right one is that right one it is the right one so let's look at our logs and let's save that I just there's been no change so maybe we just make a quick change somewhere oh I want Stack Overflow information mark so let's save that okay so we're getting this Jason convert does not contain a definition from deserialize object so I've clearly made a typographical error there let me just check what I've done wrong okay so I'll just type also the SIA allies object I think that's the only thing that was wrong let's bring our logs back up let's save that again okay we got a compilation succeeded cool sort of compilation has succeeded no guarantee that it's gonna run let's just try that again in and with any luck if we run it and I think we're getting another arrow down here so let's have a look at what that is didn't saw Jason link gee container count cannot be used like a method so maybe we don't know totally brackets yeah maybe that's all it is save it and let's run it again there we go my bad so long no curly brackets on the count method and Henry's you can see that we have one question on Stack Overflow so that's all working quite nicely now I deliberately showed you and the fact that I've made a mistake and what I did to rectify that mistake I could quite easily edited that out and you would have been none the wiser I wanted to show you that I made a typo you do and that's one of the downsides of using this environment because it's not as obvious when you use something like Visual Studio or Visual Studio code so one of the problems with it and I wanted to show that limitation also a limitation of my pure typing skills but also how you can then rectify it by looking at the logs and if the logs stopped working how you rectified that so just wanted to provide some little life examples of how of problems you may encounter as a developer so the second to last thing we need to do is deal with this weird ahem UNIX Titan string at the moment we've just got a hard-coded value in there we want to set that dynamically so that we just pulled back questions within the last 24 hours rather than having something hard-coded so let's say let's clear that down get ourselves a bit more room know where should we put it I think we just probably put it within the make Stack Overflow request method probably makes more sense and we'll just create it up here I guess that's probably the place I would put it ok so we're going to create effectively that's not really a string it's a number of seconds that we want to inject into here so let's create a new thought and we'll call it I don't know the pocket I'm good enough name is anything now let me just type out the the string and then I will go through and explain it afterwards because trying to explain it as I go I think is probably going to be a bit more complex than explaining it once it's fully they are [Music] [Music] okay so I just put that music over there because it was a bit pet the board is to watch me doing it so basically what we are using we're using the date time no object we're getting the universal Titan no so as of this moment but we want to get questions within the last 24 hours so in order to update that we then use this add days operator and we add negative days to it so you can add you can go forward in the future obviously you'll get zero results under the zoom or you can add negative numbers to subtract these from where we are now so you can update this if you want questions from within the last 24 hours then via 140 or was it via to and you can play around with that and then what we want to do is effectively subtract from now a the this unix epoch time which is the first of january 1970 and then we want to get the total seconds of that subtraction and that gives us our time span for no it's a bit convoluted on the last thing you want to do is can be out at a 32-bit integer that's just I believe the standard that we need to use and we put it into this value here so then to make use of that we go back down to our string and we will just delete the hard-coded value put in some curly brackets and copy and paste that in so that should know dynamically update moving forward so let's save that as does say compilation succeeded okay so I fully expect now when we run this we should have zero questions because we're basically just going back the last 24 hours and from memory that questions so let's run this there we go we get zero questions on stack or thought that's good now just to maybe test that it is working let's update list to 300 though for the last year 365 days let's move this over here again and let's see them on it now there you have five new questions on Stack Overflow related to our CSS which is all looking good so I'm going to put that back to one save and run it again and that will continue to run though the last thing we need to do is update our scheduler because at the moment this thing is running every 5 minutes when I'm not running it manually as well so that is all done within this function JSON file and you can see we have a yeah Jason key value P or setup as usual and there is this schedule element and we want to update that know to just run everyday so to do that there's this expression here is something called crontab expression or an n crontab expression so just as an FYI I actually have actual thing over here so again this is my log and actually have an article step by step article on how to run or how to create this seat belt it's not exactly the same and I'll come on to fly in a minute the same function but it does have details on the scheduling stuff and I have put a link into this more detailed information about how you can configure this scheduling operation again it's a UNIX type setup so interesting we were using a unix time string and we're also using a unix cron scheduling expression to run our scheduler i'm not gonna go into too much detail about that but each of these little stars represents like a day a month blah blah blah so at the moment this is running every 5 minutes i want to change that to a more standard date and time so rather than every 5 minutes I'm gonna run it at 9:30 FBD so that's 90 are to en obd okay that's the seconds component and that's the one Theo I guess looking forward so you want to read up more of them on that you can do it but I'm just going to quickly move on and say I want to bond that at 90 every day and that's basically how you do it and it will just one every 24 hours so saving one that shouldn't make any difference it's just the configuration file and it will just more than likely get another yeah we go another looking thing pop up on here now just before we finish I'll just circle back to the original solution that I had come up with for this if you are interested looked a bit like this so when it's different you'll notice the inclusion of a sequel database or the way I'd originally designed this was there's your function with one it would go to stack exchange get a list of every single question on your slide two topics or no time string or anything like that it would bring all those questions back store them in the sequel database then the next day when it ran it would go again pull everything back and compare what I had in the database from yesterday compared to the new payload and then determine the new questions that have arrived hugely hugely inefficient really horrible almost embarrassing that I wrote that and I think the solution had gone through in this video as much later but the article on online on the blog details the old solution where you use a database and it uses that method now I've left it there because of nothing else it actually is useful from the perspective of telling you how you can integrate or introduce a database into your as your functions which may be a really useful thing that you want to do so I've left that in there and from that perspective it's useful but the solution itself is really horrible from our logical prospect well once again we come to the end of yet another video and once again I'll thank you for watching and supporting the channel I really do appreciate it if you want to get notified of any new videos that I post up subscribe to the channel click the little thingy bell and you'll get email notifications when I do that and if you like this video give it a like but you know what if you don't want to do that I'm totally okay with that so until I make the next video I'll see you again next time
Info
Channel: Les Jackson
Views: 26,279
Rating: undefined out of 5
Keywords: dotnetplaybook, dotnet playbook, azure, azure functions, .net, .net core, api, webhook, web hook, stack overflow, stack exchange, slack, slack api, les jackson, step by step, tutorial
Id: Ft34VWPpiA4
Channel Id: undefined
Length: 65min 49sec (3949 seconds)
Published: Sat Nov 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.