logo

NJP

Inside the Barrel - Streaming Scripted REST APIs ServiceNow

Import · Mar 10, 2022 · video

welcome to another episode of inside the barrel where john and i and our our our guests today lucas are going to talk about consulting live we're going to talk about uh scripted rest apis and you know trying to solve some unique challenges when it comes to performance and specifically around scripted rest apis so excited to have you here today uh lucas and uh maybe you want to talk a little bit about like how this topic came up in your work and yeah no definitely this uh happened quite recently actually i um i was i'm actually in uh right now working on a on a project on a csm project that has a bunch of different integrations one of the integrations is to allow the client to pull data um from servicenow and it's like huge amounts of data and so the reason that all this came up was they said we don't want to change too much they they basically want to like switch the end point to the new endpoint and get the exact same thing and so i was thinking oh okay is there a way of doing that because they have a very specific payload and so i then kind of i was trying to figure out a way of doing this and i kind of text dorian saying hey have you ever tried because every time we do a rest api or a scripted api service now adds the result attributes to the payload uh no matter what you actually return that is always the result so at that point i thought oh okay can i actually tell the client because you know i don't want them to change anything they can't change anything we need to return the exact same thing and dorian said well have a look at this streaming api and i hadn't you know i didn't actually know about that so you know like in a couple of minutes we kind of exchanged a few messages and i read i was going to you know i was reading it and then i kind of started making some changes and then you know it went back and forth and then we kind of i think this is the the reason that we wanted to kind of show this today because this is interesting yeah just for for the audience right like so normally servicenow has their their scripted rest api that you know returns a serialized object json object um and it's in a very standard format and they also provide the ability to modify that and utilize streaming which is a little bit different from your normal rest apis because streaming is in some ways stateful because it it's essentially a stream of information that's kind of going through um the the reason that uh i recommend lucas to kind of look at this was because there there is performance gains from streaming like normally when you try to send a huge payload which we had a video related to that on like millions of records kind of coming into servicenow there is a a response that you know can take 10 seconds 20 seconds 30 seconds and that's not a really good practice for your apis to have a long response time for whether something is a 200 or not right and so the the beauty of streaming is you actually get that 200 response or that 400 response like instantly so that it keeps the standard there um from a performance standpoint and then as the data is streamed through um the client's able to accept it yeah yeah so the reason the for the specific reason for me wasn't performance at the beginning because the api that i'm building is actually paginated so because we also use pagination today so of course if we are already controlling you know the number of pages and the amount of records per page that they can actually consume you know there wouldn't be too many problems because you know we are controlling that but um again i think we're going to be able to see that through the demo because there's a lot of points that i um i want and so again the rest api as you said is kind of like an ongoing conversation it's like hey oh no i'm going here oh uh when so it's kind of like you ask and you get responses you know immediately back and then yeah it's quite interesting because it's like a yeah it's kind of like through like events so like you know it keeps coming uh coming back as long as there is more data to come yeah cool well you said demo so shall we try to jump into a demo yeah sure it's very interesting because this is a demo that is very well it's something that i am building currently but i'm using of course i'm using a a different uh different instance but um so let me know if you guys can see uh you can see you can see my screen and i'm gonna zoom in if necessary um so we can't see it yet if if you are trying to share it there should be like a share yeah there's here i uh yeah i want to proceed so if i do this share i click share ah okay i think oh this is because of the the the [Music] this um oh it's probably because of the this uh tool uh melon app i think it's not going to do that well yeah well there should be when i normally when i click share it like we'll do a little pop-up saying which window you want to share and then you select it and then now you're able to to share it yeah uh yeah but if he's never done it before it could be a permissions issue and especially on a mac you know how to be like oh well you've got to restart well because like um like chrome does not have permissions to access your screen please follow these steps so on the the top left where the url is there should be like a little like permissions for for the website just too simple right so here like camera microphone pop-up sound is all um all enabled allowed and the share just still doesn't work oh this is bad this is actually bad you'll figure it out i think oh and someone uh someone really likes our sessions so i thought i'd uh show that up while you're doing that um maybe i'll try again before i'll come back uh maybe that is going on yeah am like we're really happy that you enjoy these and hopefully we'll get a good demo for you on this one and actually what i could do while we're waiting for for lucas to come back um is show the documentation from servicenow um so is this on the the stream component or yes yes uh both um because they do they do a nice comparison so let's get lucas back while lucas figures out how to do the share i'm gonna share mine yeah just just on an off topic i see that dorian's wearing a green shirt huh yes so i'm not pinchable right happy saying daddy's day right i oh i forget how this video is but there's like there too you know like south park just came out with an episode i don't know if you watched south park yesterday and it's all about saint patty's day and how like pinching and is like not a thing anymore you can't do it because there's no consent right like it's like can i can i uh have consent to pinch you please yeah exactly exactly so um so while lucas is figuring that out i thought i'd start us off with some some of the docs so if you go to servicenow stocks there is one specifically on um like scripted rest api example for streaming versus the serialized response and essentially um what servicenow kind of shows here is you're essentially able to set certain like headers and then you use this uh this getstreamwriter object and you essentially write to the string uh to that stream uh whatever you want so they're they're showing how they do their results one as an example but you could you know modify the payload however you want and then they essentially iterate over some objects and then they they close their their response and that essentially shows a response similar to this which you know in this case looks pretty identical um but in comparison uh the normal response looks something like uh or the normal one would look like notice here there's no there's no headers but it's doing pretty much the same thing and just pushing and returning the response and it adds that you know result to it and and so they look pretty identical if you want them to to look that way um it's really the difference on like performance or if you wanted to fully control all of the uh uh the example the response yeah exactly yeah um so like if you wanted pagination right which you know lucas is you know in his case needs to do like he needs to be able to modify that entire object so uh so that's kind of um where that is and we'll uh we'll kind of show uh an example of uh in postman or in command line um actually how long it would take to to do a certain type of stream uh a stream versus a serial response so you can actually see the performance games that you would get when it's like an api to api nice nice and that's important that's important because normally like there is like a best practice when it comes to how long a response should take so if you do have 10 seconds there may be certain things out there that if you're waiting that long they decide like you know i'm not gonna i'm gonna say it's a failed response and then never actually connect to your servicenow data so so we're um and i think while i don't even know if my instance is set up this is uh let's let's see let's see if i can uh let's see if i can uh help and and worst case uh lucas can just give me access on his instance and i'll uh i'll i'll do that so speaking of developer instances this morning i was trying to log into mine and i got to tell you i am not a fan of their single sign-on you log in and then it takes you right back to the login screen so yeah again yeah there's definitely some like weird looping that that happens yeah yeah we'll see access to uh any any success lucas no it's in fact when i go to my system preferences on uh on my mac it actually says recording and google chrome is there uh just like the documentation says here but it doesn't he doesn't let me actually share so do you got firefox let me try firefox yeah if not just shoot me like a create an account for me and then just you know walk us through and i'll try to i'll try to i'll try to do all of the things that you're you were planning to do the luck of the irish is not with us so true i'll give you this here let me just text you so um then you can do the um yeah but you know this this is another thing about um you know the cohesiveness of the community you know so lucas reaches out to dorian because they know each other and they they've been there and and i've seen this a number of times you know you have the people that you've either worked with or met you know knowledge back in the day and that you keep in contact with because there's always questions floating around between each other uh even outside of the s and devs slack channel or uh tim's uh discord disclosure yeah yeah yeah i used to used to collaborate with jace i need we need to get jason so i've been talking to him lately he he's interested on getting on the show but uh he's put together uh an rss thing oh man i'll i'll show that off while we wait for lucas to send me some some information because that's like a really good one yeah cask's blog is all messed up oh oh really it's got malformed links or something so we got to talk to someone that cask about getting that squared away but i had him so i asked i told him i said hey get our video feed on there from youtube's right so we're on there i saw this yesterday yeah it's like we're we're on here so for those that like this would be like a cool like just news podcast i think john that we could you know explore right just like talking through some of the things that we see on here because i think there's some like really there's some really really good stuff on here but sometimes it's hard to like know like what what are you actually need or looking for but jase did an awesome job of uh you know rss feed style like being able to to find things so uh dorian i said let me just see um can you get out here like uh well i sent here as well because in fact you don't need i can give you the access to the instance but nice if you open your i can actually guide you through like if you open your uh postman for example uh then we can uh then we can we could do this yeah i could i could do uh i can open postman and share some postman yeah i i gotta say i love postman when it's coming to api work yeah it makes things so easy um yeah we've also made this api like no yeah let me open up postman real quick yeah just open postman in fact uh what happens is the uh the the example is gonna go through there is like over 55 000 task records in the system and the the api called a few api calls that we're gonna make it's a paginated between one and seventy thousand so we're gonna we're gonna be able to see exactly what's coming and how it's coming back gotcha yeah you know what i i think it'd actually be better if we did this in uh curl just like command line um just because postman doesn't technically support streaming so it wouldn't look any different to a person um whereas we do want to show that we could show it through uh yeah i think like i have a yeah the idea was that we would show imposed men to say like hey i have because basically i have like two calls one i call good and another one i call bad gotcha and yeah but we can do in curl because uh we'll show you this yeah go to your terminal we're gonna paste you the actual curl um perfect script and then right oh look look how beautiful we we have a terminal if i would have been prepared i would have like uh made some like fancy fancy profiles so i feel a little a little out of it with my vanilla can you actually copy this or is it how does it look there because uh in the in the message there in private yeah let me check in private last one yeah i mean let's try it um i think it gets cut off though yeah so let me see on that on what's up here if i said we were close yeah if you if you have your whatsapp open there yeah yeah yeah i do i do yeah that one should work wow nice all right you'll have to change this password at some point since whoa whoa sharing plain text passwords right now my okay there we go all right so yeah there we go cool so well this is called like bad and so you see it's it's kind of it's counting it's probably like around 12 seconds to come back because this one um essentially uh so let's yes yes so let's let's kind of let's kind of talk through that um what what that just did um sorry my my micro okay so first thing what we're doing is we're we're doing like a a curl command to you know this url um or this this rested api and we call this endpoint bad data um and we're saying that we want you know 70 70 000 records uh per page right yeah yeah yeah and you can see that we get like uh their number yeah we read 50 000 58 000 something yeah yeah so at the end it was 58 000 you know what would have been cool is uh if we could like time how long that took but uh i think you can it's around 12 seconds or something because like 12 seconds yeah like that and um um well the reason just explain uh we don't have the code here but explain that basically this one we have a call to an a script included that is basically saying it's going through always know 58 000 and it's building that object and in the very end after we actually were done with reading the records we then say okay stream this out and we stringify the entire object at the very end and that's the reason it took like all this and all this time to come back uh and the other one now i'm gonna send it to you and we're gonna see the difference um of that one so let me just open here is dorian is there not a way to like call a start time before you do the curl and then have it so i think you can um but i i think for i'll try it afterwards right uh okay because i feel like there is like a time like you can do let's see i'll just try the same this is the same command but with time in front of it and i think when you put time in front of it there's probably some options for it but we'll see what uh what time tells us at the end and it's interesting that it's kind of like it feels like it is a streaming but it's not this is just the interface yeah this is just accepting that entire payload right yeah um so 22 seconds yeah that was quite quite fine um so let's so but with the good with the the streaming one yeah so this one so you see how instantly we're getting response right like that's that's kind of the beauty of streaming right is like we're we're just instantly getting the response that we needed um so so it doesn't look like much to a user but it is a lot to a system so let's try that same command with time and see if there is any difference on there yeah i don't know what time it's gonna actually uh is it a because in the end of the day it's kind of streaming but we you know we got the response pretty pretty quickly um yeah so i mean it did show 14 seconds so at least we're we were i think the the biggest thing again like we're getting a 200 response right away which is which is the key difference between like a stream versus uh the other one um and and i don't know if do you have a do you have a an admin user account i could i could hop into that uh if you want to send that over to me i'll hop into the instance that's right and i'll show you yeah uh i'll give you yeah you can just like walk me through did that does that make sense though john for for you just to see that difference uh and i think i missed it at the beginning if there was a a change um but in my mind that you know if you're getting that 200 right off the bat you can definitely handle you know as far as the front end goes you can handle presenting what's happening to the user right yeah exactly so let's and it's like you know the the user calling and consuming this api needs to be prepared for this but we know and uh you know this is kind of like you know backwards compatible because even a system that is not ready for example if you look at postman postman is actually working on this problem right now but when you execute it still executes as a normal rest api so he waits until the end but if the other system is actually doing streaming then probably you know you can probably see some difference there um so let's uh and uh if you go to studio there it's quite easier for us to actually skip there's a there's an application there called uh demo uh streaming i think i i call something like that uh yeah that one and so uh if you go to the uh we have like at the very bottom we have the uh the calls like the bad and the good ones but you can go to this include called ws poll request and there is a method called bad request and then good and i put some comments as well so just after the initialize there are two methods let's go down let's go down a little bit yeah so this one is process good and the reason it is good if you look at the uh right there like 80 basically here we we start the streaming we then stay the content type is up you know json and then we set the status and just after that we go through the you know we start streaming so the first one is like the data so this is exactly how i want my object to be and then as we go through the loop you can see that we stream out straight away so we read one object we stream it out and then we go to the next one and then in the end we kind of like you know we just end it with the awesome yeah and that's the difference of between this one and the next one down there if you kept going the difference between this one now is uh you see that i don't stream any for any point only within the the while loop sorry even after that sorry uh while loop we built that object entirely and then in the at the very end we stringify the whole thing and that's the reason that it takes a lot of time too yeah and and just just it's it's nice that you your example is it's technically using servicenow stream but you're you're you're mimicking the sec the the bad one as if it was a serialized object right so that's what essentially you're doing versus using the stream and the reasoning was because uh remember uh dorian you said to me like oh read uh read the the documentation what i did was because i was realizing the object i said oh it's quite quick let me just you know string fine you know and i can actually stream it out straight away and then we thought oh wait if we are streaming you know isn't it better to actually stream as we read at you know every record and that's the reason why i called bad and good because if you stream you have to do it right you know you have to do one by one yeah so so what i i figured we can do is we we probably can show it here um through the the rest api explorer uh as long as i understand what the the header looks like there's no header you just have to pass like uh there's a query parameter called per underscore page and you can put like you know uh 70 000 for example and then there's one called page and you just put it one um yeah that's all you cool so this is for the the bad one and when we click send you'll notice here it's thinking it's thinking it's thinking right like this is how much time it's taking like if i was another uh service waiting for a response yeah yeah and it could time like you said it could give us your time time out on that too exactly so it took us you know 12 seconds to do that one right so that's what that does and this is essentially you know what it looks like um so i'm actually curious how servicenow is going to handle this uh streaming in their own platform plus page one and now when we click send so it's interesting it's still waiting as well so you know this is this is an example where like uh a ui thing similar to like postman needs to be able to support streaming um because if it doesn't it'll just look you know the same right like it it didn't notice anything different here um for it but another service that does support like checking for a status code of 200 right away um we'll be able to to get that performance um that it that it needs so yeah that's correct yeah cool yeah so i think like for me today like uh so there are of course there is performance but for me this wasn't the case right it was more that i needed i don't want you know i the client said we don't want to change the api we just want to change the end point so for me this was the only thing that i could do to remove that result from the from the payload yeah yeah it was my reason and again it's already paginated uh so there was you know there wouldn't be any need for any streaming but um because of that reason i think yeah i think it's definitely a nice like i don't want to say work around but it's nice that servicenow gives you the feature to control like what what you return in your your your body right and and if you essentially it's th those are the two use cases you want to do streaming or get some performance increase then you know streaming's your way or if you need to modify the body um to not like return the result or if you needed to paginate right like if you needed to build the pagination like servicenow doesn't you know have that out of the box so those are so three good use cases on why you would want to use their um this the streaming scripted rest apis yeah yeah cool anything else i mean if not like we could we can call this one a shorter one um i think this was like you know it was already pre-built right it was yeah yeah totally yeah we did we it is the magic of it yeah i'm actually working on this right now you know for a client so it's quite interesting awesome yeah yeah i think that was that was probably it awesome well if you guys like this content um feel free to subscribe or follow lucas on on linkedin or john and i um so we're and if you guys uh anyone in the audience does have other topics you want us to tackle or kind of walk you through like we're we're always open to doing so all right well thanks again everyone thank you guys thank you

View original source

https://www.youtube.com/watch?v=EoOJaUFc8eU