1 00:00:04,960 --> 00:00:19,999 [Music] 2 00:00:21,279 --> 00:00:26,359 the ding is my favorite bit welcome back 3 00:00:24,199 --> 00:00:28,320 to devops uh hopefully you are in the 4 00:00:26,359 --> 00:00:30,840 right room we are here for a lazy 5 00:00:28,320 --> 00:00:33,640 Person's Guide to building rest CL cents 6 00:00:30,840 --> 00:00:35,800 uh we have Ash Beck presenting for us 7 00:00:33,640 --> 00:00:38,239 Ash is a cyber security engineer with a 8 00:00:35,800 --> 00:00:40,360 background in medical device testing uh 9 00:00:38,239 --> 00:00:42,320 chronically in incapable of saying no to 10 00:00:40,360 --> 00:00:44,200 interesting projects or weird technical 11 00:00:42,320 --> 00:00:47,039 problems they are also allergic to 12 00:00:44,200 --> 00:00:48,480 writing scripts more than 300 lines long 13 00:00:47,039 --> 00:00:50,199 involved in a number of Pride networks 14 00:00:48,480 --> 00:00:52,399 they're passionate about expanding queer 15 00:00:50,199 --> 00:00:54,079 representation in cyber security and 16 00:00:52,399 --> 00:00:55,920 Engineering they still haven't figured 17 00:00:54,079 --> 00:00:57,700 out what Dev seops is and at this point 18 00:00:55,920 --> 00:01:00,160 I'm too afraid to ask 19 00:00:57,700 --> 00:01:03,160 [Music] 20 00:01:00,160 --> 00:01:05,760 welcome Ash take it away hello oh cool 21 00:01:03,160 --> 00:01:05,760 I'm coming through the 22 00:01:08,159 --> 00:01:13,640 speakers hi everyone I'm Ash um welcome 23 00:01:11,280 --> 00:01:16,200 to my talk setting some context uh rest 24 00:01:13,640 --> 00:01:18,520 apis are pretty ubiquitous really useful 25 00:01:16,200 --> 00:01:23,520 way to send and receive data um writing 26 00:01:18,520 --> 00:01:26,200 them's super boring um so I set out to 27 00:01:23,520 --> 00:01:28,720 make my sty make my life much harder one 28 00:01:26,200 --> 00:01:31,000 day and find a way to make the actual 29 00:01:28,720 --> 00:01:33,159 writing of them a bit interesting 30 00:01:31,000 --> 00:01:35,840 um walk you through my weird problem 31 00:01:33,159 --> 00:01:37,640 solving Journey um yeah this talk kind 32 00:01:35,840 --> 00:01:39,159 of came about from last year's Pon which 33 00:01:37,640 --> 00:01:41,079 I semi snuck into because there was a 34 00:01:39,159 --> 00:01:42,720 really boring corporate cybercity 35 00:01:41,079 --> 00:01:45,320 conference that work had sent me to Next 36 00:01:42,720 --> 00:01:46,439 Door um hated it and bumped into a 37 00:01:45,320 --> 00:01:48,759 friend and they were like ah that might 38 00:01:46,439 --> 00:01:50,680 be interesting so um yeah little bit 39 00:01:48,759 --> 00:01:51,880 about me um I pronoun a day them I 40 00:01:50,680 --> 00:01:53,320 didn't spend thousands of dollars 41 00:01:51,880 --> 00:01:56,560 avoiding the horror of assigned gender 42 00:01:53,320 --> 00:01:58,039 to be gendered so please respect that um 43 00:01:56,560 --> 00:02:00,439 things I do for fun rock climbing 44 00:01:58,039 --> 00:02:02,200 photography cooking sewing writing 45 00:02:00,439 --> 00:02:04,240 uh as I said in my bio I was originally 46 00:02:02,200 --> 00:02:05,799 a biomedical engineer um but these days 47 00:02:04,240 --> 00:02:07,399 I work as a security engineer for people 48 00:02:05,799 --> 00:02:09,360 that don't know cyber security makes me 49 00:02:07,399 --> 00:02:12,879 a bit of a cyber plumber um or I really 50 00:02:09,360 --> 00:02:15,239 overpaid sis admin um yeah so um 51 00:02:12,879 --> 00:02:17,480 disclaimer I'm not a software engineer 52 00:02:15,239 --> 00:02:19,120 um my only formal qualification involved 53 00:02:17,480 --> 00:02:21,599 putting pingpong balls on UTS and making 54 00:02:19,120 --> 00:02:23,680 them walk around a room um so I only 55 00:02:21,599 --> 00:02:25,200 learned what like uh software design 56 00:02:23,680 --> 00:02:26,720 patterns were a month ago so if you have 57 00:02:25,200 --> 00:02:28,480 any feedback on any of the codeing this 58 00:02:26,720 --> 00:02:30,160 talk uh please let me know I really want 59 00:02:28,480 --> 00:02:31,480 to be better at my job um my colleagues 60 00:02:30,160 --> 00:02:33,680 would also probably really like me to be 61 00:02:31,480 --> 00:02:35,840 better at my job um anyone who wants to 62 00:02:33,680 --> 00:02:38,480 hear the UT story uh buy me a coffee 63 00:02:35,840 --> 00:02:39,760 sometime it's kind of fun um and a 64 00:02:38,480 --> 00:02:41,280 little bit of an overview going to touch 65 00:02:39,760 --> 00:02:43,879 on rest clients what are they how do I 66 00:02:41,280 --> 00:02:46,360 interact with them um very brief 67 00:02:43,879 --> 00:02:47,800 introduction to what Dundas are and then 68 00:02:46,360 --> 00:02:50,599 the meat and potatoes is kind of going 69 00:02:47,800 --> 00:02:53,200 to be like how I solved this or how I 70 00:02:50,599 --> 00:02:56,720 made my life more difficult um in order 71 00:02:53,200 --> 00:02:58,360 to make it less boring so rest clients 72 00:02:56,720 --> 00:03:00,319 or representational state transfer it's 73 00:02:58,360 --> 00:03:01,519 a bit of a mouthful um actually 74 00:03:00,319 --> 00:03:03,000 originally started as a software 75 00:03:01,519 --> 00:03:07,360 architectural style in the late '90s 76 00:03:03,000 --> 00:03:09,080 early 2000s I think um interestingly 77 00:03:07,360 --> 00:03:11,560 most rest apis do not conform to the 78 00:03:09,080 --> 00:03:14,519 software architectural style um which is 79 00:03:11,560 --> 00:03:16,319 kind of odd um so these days rest is 80 00:03:14,519 --> 00:03:17,840 commonly used as a bit of a Catal term 81 00:03:16,319 --> 00:03:20,319 for any kind of system that you're 82 00:03:17,840 --> 00:03:22,159 grabbing data from a a web server and 83 00:03:20,319 --> 00:03:23,799 bringing it to your clients and so for 84 00:03:22,159 --> 00:03:27,280 the purpose of this talk we'll be 85 00:03:23,799 --> 00:03:29,760 talking about just the API um for the 86 00:03:27,280 --> 00:03:31,239 example I'll be using virus total uh 87 00:03:29,760 --> 00:03:33,120 partly because I'm a security engineer 88 00:03:31,239 --> 00:03:34,640 and there is no security operations team 89 00:03:33,120 --> 00:03:37,280 on Earth that probably doesn't use it in 90 00:03:34,640 --> 00:03:39,000 some capacity and secondly because it's 91 00:03:37,280 --> 00:03:41,319 really well documented you can go online 92 00:03:39,000 --> 00:03:43,080 check out the API doco uh there's also a 93 00:03:41,319 --> 00:03:46,439 free tier you can sign up for an API key 94 00:03:43,080 --> 00:03:48,159 if you'd like um yeah so arrest client 95 00:03:46,439 --> 00:03:50,120 how does it work your client will 96 00:03:48,159 --> 00:03:53,799 usually want some data from a server and 97 00:03:50,120 --> 00:03:55,879 so it will go sender https not HTTP 98 00:03:53,799 --> 00:03:57,200 please don't use that um request to the 99 00:03:55,879 --> 00:03:59,439 server usually with some kind of 100 00:03:57,200 --> 00:04:00,439 authorization authentication um header 101 00:03:59,439 --> 00:04:02,959 to a 102 00:04:00,439 --> 00:04:05,519 commonly an API key and then server will 103 00:04:02,959 --> 00:04:08,000 vomit up a blob of data it can be any 104 00:04:05,519 --> 00:04:09,360 kind of format um virus total us as 105 00:04:08,000 --> 00:04:12,319 Jason so that's what we'll be using for 106 00:04:09,360 --> 00:04:13,640 this talk and then a very very very 107 00:04:12,319 --> 00:04:15,079 brief introduction into Dundas you 108 00:04:13,640 --> 00:04:17,160 probably most of you are probably aware 109 00:04:15,079 --> 00:04:18,639 of what they are um when I was writing 110 00:04:17,160 --> 00:04:21,199 this talk I learned that their official 111 00:04:18,639 --> 00:04:23,280 term is actually a system defined name 112 00:04:21,199 --> 00:04:26,560 um yeah I'd only ever heard as done as a 113 00:04:23,280 --> 00:04:28,520 magic methods for years uh two trailing 114 00:04:26,560 --> 00:04:30,600 and leading underscores hence double 115 00:04:28,520 --> 00:04:33,160 underscore Dunder feels a bit 116 00:04:30,600 --> 00:04:36,039 self-explanatory um defined within the 117 00:04:33,160 --> 00:04:37,759 interpreter but they allow for custom 118 00:04:36,039 --> 00:04:39,080 classes to define specific behavior I 119 00:04:37,759 --> 00:04:41,600 think most people's introduction is 120 00:04:39,080 --> 00:04:42,960 probably going to be the init function 121 00:04:41,600 --> 00:04:45,880 which is used to define how new 122 00:04:42,960 --> 00:04:48,919 instances of a class are initialized um 123 00:04:45,880 --> 00:04:51,160 yeah so diving in we get into our 124 00:04:48,919 --> 00:04:53,919 problem case so you got to write a bunch 125 00:04:51,160 --> 00:04:55,960 of clients say you've got well put 126 00:04:53,919 --> 00:04:58,039 yourself in my shoes you've been hired 127 00:04:55,960 --> 00:04:59,440 into a new role uh you've got to write a 128 00:04:58,039 --> 00:05:01,919 bunch of software you're not familiar 129 00:04:59,440 --> 00:05:03,320 with Python and you've got a an 130 00:05:01,919 --> 00:05:08,199 application that you need to bring a 131 00:05:03,320 --> 00:05:09,840 bunch of data into Yesterday um yeah and 132 00:05:08,199 --> 00:05:11,919 so you're going how can I write a lot L 133 00:05:09,840 --> 00:05:13,199 code and get this done really fast uh it 134 00:05:11,919 --> 00:05:16,520 probably didn't end up making my life 135 00:05:13,199 --> 00:05:18,240 much faster but hey let's see so as a 136 00:05:16,520 --> 00:05:20,080 reference point this is a regular 137 00:05:18,240 --> 00:05:22,280 request in Python you fire up your 138 00:05:20,080 --> 00:05:24,120 request Library you import some or you 139 00:05:22,280 --> 00:05:26,000 import your libraries you pull an API 140 00:05:24,120 --> 00:05:29,080 key and you send a request this is kind 141 00:05:26,000 --> 00:05:31,199 of what we want to be automating right 142 00:05:29,080 --> 00:05:33,120 so you get a response that looks like 143 00:05:31,199 --> 00:05:34,880 this again a Json blob you're probably 144 00:05:33,120 --> 00:05:37,000 all fairly familiar with what they look 145 00:05:34,880 --> 00:05:40,319 like um not going to go into the details 146 00:05:37,000 --> 00:05:42,039 of Jason but yeah so you go I don't want 147 00:05:40,319 --> 00:05:43,440 to have to write that code over and over 148 00:05:42,039 --> 00:05:45,080 and over again for every request I make 149 00:05:43,440 --> 00:05:46,759 so maybe you go wow this abstraction 150 00:05:45,080 --> 00:05:48,199 thing is really great I'm going to write 151 00:05:46,759 --> 00:05:49,560 a function and then I only have to write 152 00:05:48,199 --> 00:05:51,479 that code once and then I can call that 153 00:05:49,560 --> 00:05:54,560 function as many times as I need look at 154 00:05:51,479 --> 00:05:56,600 that I can get all those IPS 155 00:05:54,560 --> 00:05:59,319 wonderful still kind of annoying so 156 00:05:56,600 --> 00:06:01,280 maybe you go why don't I create a class 157 00:05:59,319 --> 00:06:04,160 python class system is amazing you want 158 00:06:01,280 --> 00:06:06,360 to group common Behavior also we can do 159 00:06:04,160 --> 00:06:07,440 SUB classes I'm a big fan of the request 160 00:06:06,360 --> 00:06:09,120 session it brings a lot of the 161 00:06:07,440 --> 00:06:10,800 functionality that we want to be using 162 00:06:09,120 --> 00:06:12,880 into this you don't have to write the 163 00:06:10,800 --> 00:06:14,240 actual request itself the people that 164 00:06:12,880 --> 00:06:16,639 wrote the request Library are bloody 165 00:06:14,240 --> 00:06:19,479 brilliant they're much smarter than me 166 00:06:16,639 --> 00:06:22,000 but at the same time or I should say you 167 00:06:19,479 --> 00:06:24,759 want to call this so pretty standard 168 00:06:22,000 --> 00:06:26,240 process um get an instance of your class 169 00:06:24,759 --> 00:06:29,160 you call the methods on the class you 170 00:06:26,240 --> 00:06:31,360 get your data groundbreaking stuff um 171 00:06:29,160 --> 00:06:33,599 bit of an egg first person to come to me 172 00:06:31,360 --> 00:06:36,080 with what that hash is I'll buy a drink 173 00:06:33,599 --> 00:06:39,280 um but you're still having to write a 174 00:06:36,080 --> 00:06:42,919 method for each piece of functionality 175 00:06:39,280 --> 00:06:45,400 so each endpoint on the server that you 176 00:06:42,919 --> 00:06:48,479 want you're writing a method for I'm 177 00:06:45,400 --> 00:06:50,479 really lazy I hate writing more code 178 00:06:48,479 --> 00:06:52,319 than I have to I hate doing more work 179 00:06:50,479 --> 00:06:54,759 than I have to and especially if it's 180 00:06:52,319 --> 00:06:58,319 boilerplate and I'm not solving any 181 00:06:54,759 --> 00:07:00,400 problems that feel like fun so how can 182 00:06:58,319 --> 00:07:02,800 we make this big more 183 00:07:00,400 --> 00:07:04,199 interesting and more specifically can I 184 00:07:02,800 --> 00:07:06,240 create methods and attributes without 185 00:07:04,199 --> 00:07:07,599 having to write them all um that's a 186 00:07:06,240 --> 00:07:09,080 really good question let's see what 187 00:07:07,599 --> 00:07:12,160 happens so let's strip those methods out 188 00:07:09,080 --> 00:07:14,840 of our class so we've just got our nit 189 00:07:12,160 --> 00:07:17,560 function um and then let's see what 190 00:07:14,840 --> 00:07:19,000 happens when we try this little part in 191 00:07:17,560 --> 00:07:21,720 the main 192 00:07:19,000 --> 00:07:23,919 section surprise surprise we throw an 193 00:07:21,720 --> 00:07:26,520 error um 194 00:07:23,919 --> 00:07:29,199 great um this case it's an attribute 195 00:07:26,520 --> 00:07:30,440 error that's really good to know and 196 00:07:29,199 --> 00:07:32,400 because we're throwing an error we can 197 00:07:30,440 --> 00:07:34,680 say The Interpreter is doing something 198 00:07:32,400 --> 00:07:36,680 to catch that so if it wasn't catching 199 00:07:34,680 --> 00:07:38,280 the eror you'd throw a seg faal um which 200 00:07:36,680 --> 00:07:40,360 is not ideal trying to access something 201 00:07:38,280 --> 00:07:42,960 in memory that doesn't exist so we're 202 00:07:40,360 --> 00:07:45,080 seeing an attribute error and the object 203 00:07:42,960 --> 00:07:47,400 has no attribute popular threat 204 00:07:45,080 --> 00:07:50,520 categories so that kind of begs the 205 00:07:47,400 --> 00:07:55,879 question how does python access 206 00:07:50,520 --> 00:07:58,080 attributes and methods um yeah 207 00:07:55,879 --> 00:08:00,039 and one of the wonderful things about 208 00:07:58,080 --> 00:08:01,560 python is it's super well documented I 209 00:08:00,039 --> 00:08:03,400 really really love the reference 210 00:08:01,560 --> 00:08:06,639 documentation um so you go to the 211 00:08:03,400 --> 00:08:07,960 documentation specifically chapter 3 um 212 00:08:06,639 --> 00:08:09,520 which is probably what I'll be well no 213 00:08:07,960 --> 00:08:11,159 definitely what I'll be referring to 214 00:08:09,520 --> 00:08:13,000 it's really really good and you go to 215 00:08:11,159 --> 00:08:15,199 the attribute access section and there 216 00:08:13,000 --> 00:08:18,960 are five five this is kind of four 217 00:08:15,199 --> 00:08:20,960 fingers cuz climbing um methods that 218 00:08:18,960 --> 00:08:22,039 define attribute access they get add get 219 00:08:20,960 --> 00:08:25,360 attribute set 220 00:08:22,039 --> 00:08:26,280 add Del add and de um seems like the 221 00:08:25,360 --> 00:08:27,800 first three are going to be the most 222 00:08:26,280 --> 00:08:29,120 useful for us we've only read the 223 00:08:27,800 --> 00:08:30,879 documentation saying that those methods 224 00:08:29,120 --> 00:08:33,440 exist haven't actually read what they do 225 00:08:30,879 --> 00:08:35,360 we don't need to do that right so best 226 00:08:33,440 --> 00:08:39,360 way to find out is to write code and see 227 00:08:35,360 --> 00:08:41,159 what they do um so we override or 228 00:08:39,360 --> 00:08:42,719 actually overload I learned last week 229 00:08:41,159 --> 00:08:46,040 that overloading and overriding actually 230 00:08:42,719 --> 00:08:49,200 two different things um so interesting 231 00:08:46,040 --> 00:08:51,000 little factoid so we overload our three 232 00:08:49,200 --> 00:08:52,800 functions we overload get add get 233 00:08:51,000 --> 00:08:55,000 attribute and set add you can probably 234 00:08:52,800 --> 00:08:57,240 see here that I'm using the super class 235 00:08:55,000 --> 00:08:58,160 um turns out if you don't do that you 236 00:08:57,240 --> 00:09:00,040 break 237 00:08:58,160 --> 00:09:01,440 instantiation um 238 00:09:00,040 --> 00:09:02,800 yeah so that was one thing that I 239 00:09:01,440 --> 00:09:04,640 learned when doing this that if you 240 00:09:02,800 --> 00:09:07,240 overload the wrong method you can kind 241 00:09:04,640 --> 00:09:10,440 of break a bunch of things so avoiding 242 00:09:07,240 --> 00:09:12,880 that where possible so cool this is what 243 00:09:10,440 --> 00:09:14,399 our class looks like and let's go back 244 00:09:12,880 --> 00:09:17,800 to what we were doing before and let's 245 00:09:14,399 --> 00:09:20,720 try and access these attributes that 246 00:09:17,800 --> 00:09:22,640 don't exist so previously we threw 247 00:09:20,720 --> 00:09:25,839 attribute errors let's see what happens 248 00:09:22,640 --> 00:09:27,720 now oh we get a bunch of logs I'm a 249 00:09:25,839 --> 00:09:30,279 security person I'm a huge nerd I 250 00:09:27,720 --> 00:09:33,519 [ __ ] love logs you guys probably 251 00:09:30,279 --> 00:09:35,120 don't and so I do have a handy dandy 252 00:09:33,519 --> 00:09:36,880 kind of janky flowchart to show what's 253 00:09:35,120 --> 00:09:39,079 going on here but before I get to that 254 00:09:36,880 --> 00:09:40,880 you can see that those functions are 255 00:09:39,079 --> 00:09:43,519 being called and they seem to be being 256 00:09:40,880 --> 00:09:45,920 called in a specific order so we get to 257 00:09:43,519 --> 00:09:47,800 our kind of janky flowchart and this is 258 00:09:45,920 --> 00:09:50,600 what it looks like whenever you attempt 259 00:09:47,800 --> 00:09:52,680 attribute access the method get 260 00:09:50,600 --> 00:09:55,360 attribute is called first execution 261 00:09:52,680 --> 00:09:58,440 order matters um because you can break 262 00:09:55,360 --> 00:10:00,200 it otherwise and if the attribute exists 263 00:09:58,440 --> 00:10:03,920 it'll return the attribute 264 00:10:00,200 --> 00:10:05,519 if not it will attempt to call get add 265 00:10:03,920 --> 00:10:09,160 if you haven't defined get add it will 266 00:10:05,519 --> 00:10:11,399 throw an attribute error if you do it 267 00:10:09,160 --> 00:10:13,360 will call get adder and whatever the 268 00:10:11,399 --> 00:10:16,480 functionality you've popped in there if 269 00:10:13,360 --> 00:10:18,040 not again it'll raise an attribute error 270 00:10:16,480 --> 00:10:21,040 so if we go back to some of the code 271 00:10:18,040 --> 00:10:21,040 that we had written just 272 00:10:21,240 --> 00:10:26,560 before where was I you can see that I've 273 00:10:24,160 --> 00:10:29,640 called set adder in there so we're 274 00:10:26,560 --> 00:10:31,880 actually setting that attribute and 275 00:10:29,640 --> 00:10:33,040 value so we've just done name name here 276 00:10:31,880 --> 00:10:35,560 for the example but doesn't really 277 00:10:33,040 --> 00:10:40,079 matter what you put in there where 278 00:10:35,560 --> 00:10:43,519 we get so what is get at and how does it 279 00:10:40,079 --> 00:10:45,040 work specifically again documentation 280 00:10:43,519 --> 00:10:48,240 it's beautiful you should read more of 281 00:10:45,040 --> 00:10:49,680 it you should write a lot of it um 282 00:10:48,240 --> 00:10:52,880 called when the default attribute access 283 00:10:49,680 --> 00:10:54,920 fails with an attribute error the object 284 00:10:52,880 --> 00:10:59,320 class itself does not provide this 285 00:10:54,920 --> 00:11:00,240 method cool what's an object class um I 286 00:10:59,320 --> 00:11:02,360 again this was something that I learned 287 00:11:00,240 --> 00:11:04,120 when I was writing the talk object class 288 00:11:02,360 --> 00:11:05,600 is the ultimate base class of all other 289 00:11:04,120 --> 00:11:07,279 classes it has methods that are common 290 00:11:05,600 --> 00:11:11,560 to all instances of python what does 291 00:11:07,279 --> 00:11:13,519 that mean so it means that every object 292 00:11:11,560 --> 00:11:15,519 in Python has the methods that are 293 00:11:13,519 --> 00:11:18,200 attached to the base class there are 294 00:11:15,519 --> 00:11:20,040 other Dundas that exist but ones that 295 00:11:18,200 --> 00:11:22,279 are not attached to the ultimate base 296 00:11:20,040 --> 00:11:25,120 class and not don't Define default 297 00:11:22,279 --> 00:11:27,480 behavior for objects so because get Adda 298 00:11:25,120 --> 00:11:28,839 is not on the base class that means that 299 00:11:27,480 --> 00:11:32,120 we can do whatever the hell we want with 300 00:11:28,839 --> 00:11:34,720 it theoretically without breaking things 301 00:11:32,120 --> 00:11:37,360 so no predefined behavior for get Adder 302 00:11:34,720 --> 00:11:41,000 we have control over what it does in 303 00:11:37,360 --> 00:11:44,680 this case we used set add um to set our 304 00:11:41,000 --> 00:11:46,800 attributes so and that's how we got that 305 00:11:44,680 --> 00:11:49,480 those logs working and that's kind of 306 00:11:46,800 --> 00:11:51,760 fun we can start playing with things 307 00:11:49,480 --> 00:11:53,839 we're setting our attributes but how do 308 00:11:51,760 --> 00:11:57,240 we get them to give me 309 00:11:53,839 --> 00:11:59,880 data that's a good question let's try 310 00:11:57,240 --> 00:12:02,240 calling it now it's not an attribute 311 00:11:59,880 --> 00:12:05,519 it's a method technically still objects 312 00:12:02,240 --> 00:12:10,560 though um so let's try this and see what 313 00:12:05,519 --> 00:12:12,200 happens cool another error great uh I 314 00:12:10,560 --> 00:12:13,639 love errors they give us a bunch of 315 00:12:12,200 --> 00:12:15,600 information they tell us what's going on 316 00:12:13,639 --> 00:12:17,680 underneath in this case we're getting a 317 00:12:15,600 --> 00:12:20,360 type error so non- type object is not 318 00:12:17,680 --> 00:12:23,480 callable yeah cool what does that mean 319 00:12:20,360 --> 00:12:25,240 um let's go to the documentation so I go 320 00:12:23,480 --> 00:12:26,720 to I don't even go to the documentation 321 00:12:25,240 --> 00:12:29,560 I go to Google and I type in how do I 322 00:12:26,720 --> 00:12:31,480 make a python object callable um I fight 323 00:12:29,560 --> 00:12:32,720 for my life with the generic AI response 324 00:12:31,480 --> 00:12:34,760 realize that I burnt through several 325 00:12:32,720 --> 00:12:36,000 bottles of water feel a little bit bad 326 00:12:34,760 --> 00:12:39,160 click on the first link and this is what 327 00:12:36,000 --> 00:12:41,079 I get so instances of an arbitrary class 328 00:12:39,160 --> 00:12:42,600 can be made callable by defining a call 329 00:12:41,079 --> 00:12:47,120 method in their 330 00:12:42,600 --> 00:12:49,160 class cool what's the call method called 331 00:12:47,120 --> 00:12:52,440 when an instance is called kind of bit 332 00:12:49,160 --> 00:12:55,240 circular there but again the object 333 00:12:52,440 --> 00:12:57,199 class does not provide this method great 334 00:12:55,240 --> 00:12:58,800 so that means we can probably continue 335 00:12:57,199 --> 00:13:00,720 playing around with this and hopefully 336 00:12:58,800 --> 00:13:03,320 again not break 337 00:13:00,720 --> 00:13:05,560 anything fingers crossed um I haven't 338 00:13:03,320 --> 00:13:06,800 broken anything with this uh if anyone 339 00:13:05,560 --> 00:13:08,320 has please let me know that would be 340 00:13:06,800 --> 00:13:10,279 really interesting to know how you broke 341 00:13:08,320 --> 00:13:12,079 it um I really like breaking things I 342 00:13:10,279 --> 00:13:15,600 think that's why I ended up in cyber 343 00:13:12,079 --> 00:13:18,959 security um where were we so let's write 344 00:13:15,600 --> 00:13:21,079 some more code let's pop this or create 345 00:13:18,959 --> 00:13:22,279 a call method see what happens in this 346 00:13:21,079 --> 00:13:24,560 case we'll do something really simple 347 00:13:22,279 --> 00:13:28,000 we'll just send a log every time it 348 00:13:24,560 --> 00:13:29,959 calls itself we'll throw a log when a um 349 00:13:28,000 --> 00:13:31,760 endpoint is created notes I've created a 350 00:13:29,959 --> 00:13:33,199 new class again this is just kind of 351 00:13:31,760 --> 00:13:35,000 grouping some common Behavior together 352 00:13:33,199 --> 00:13:37,680 to make things a little bit easier so 353 00:13:35,000 --> 00:13:40,880 any of the endpoints related Behavior 354 00:13:37,680 --> 00:13:43,440 formatting the URLs grabbing the data is 355 00:13:40,880 --> 00:13:46,560 going to pop in there our virus total 356 00:13:43,440 --> 00:13:48,160 will still handle our requests so note 357 00:13:46,560 --> 00:13:50,720 we've changed get out so we're setting 358 00:13:48,160 --> 00:13:52,680 the attribute but we're also returning 359 00:13:50,720 --> 00:13:54,199 it um there is a slight error in this 360 00:13:52,680 --> 00:13:57,560 this will actually create two versions 361 00:13:54,199 --> 00:13:59,800 of it so uh I have fixed this in my 362 00:13:57,560 --> 00:14:03,759 GitHub repo since I actually pop this 363 00:13:59,800 --> 00:14:05,320 slide in but anyway um yeah so we're 364 00:14:03,759 --> 00:14:06,920 returning it returning it is really 365 00:14:05,320 --> 00:14:09,240 important if you don't return it you'll 366 00:14:06,920 --> 00:14:11,360 throw another error um again something 367 00:14:09,240 --> 00:14:12,600 that I leared as I was messing around 368 00:14:11,360 --> 00:14:14,759 with this otherwise you would have to 369 00:14:12,600 --> 00:14:17,120 create the attribute and then call it in 370 00:14:14,759 --> 00:14:20,279 another line but if we do this we can do 371 00:14:17,120 --> 00:14:22,600 it all in a single line and less code is 372 00:14:20,279 --> 00:14:26,680 great 373 00:14:22,600 --> 00:14:28,160 so this didn't work before um at this 374 00:14:26,680 --> 00:14:29,360 point in the talk you're probably going 375 00:14:28,160 --> 00:14:30,600 well it didn't work before you've done 376 00:14:29,360 --> 00:14:33,079 the example so it will probably work 377 00:14:30,600 --> 00:14:34,240 this time and yes you'd be right um I'm 378 00:14:33,079 --> 00:14:35,279 not going to show you more logs you're 379 00:14:34,240 --> 00:14:36,480 probably a bit sick of them you're going 380 00:14:35,279 --> 00:14:38,480 to have to trust me that this actually 381 00:14:36,480 --> 00:14:41,120 works um or you could run it see if it 382 00:14:38,480 --> 00:14:43,839 does find out if it doesn't come be like 383 00:14:41,120 --> 00:14:48,440 well actually Ash you just told me some 384 00:14:43,839 --> 00:14:51,240 [ __ ] um yeah so we get to that point 385 00:14:48,440 --> 00:14:54,320 and we go okay it's calling oh actually 386 00:14:51,240 --> 00:14:55,440 I did show logs should have uh walked 387 00:14:54,320 --> 00:14:58,079 through my talk a few more times to 388 00:14:55,440 --> 00:15:00,639 remember what slides are in it um so 389 00:14:58,079 --> 00:15:02,680 it's working I believe the proverb is we 390 00:15:00,639 --> 00:15:04,680 are cooking with gas um some interesting 391 00:15:02,680 --> 00:15:07,639 things to note you can see that the 392 00:15:04,680 --> 00:15:09,519 virus total object class is common for 393 00:15:07,639 --> 00:15:10,880 all of them um I suppose that's 394 00:15:09,519 --> 00:15:12,639 interesting to me some people might go 395 00:15:10,880 --> 00:15:14,000 that seems really boring um so you can 396 00:15:12,639 --> 00:15:16,600 see it's all pointing to the same object 397 00:15:14,000 --> 00:15:19,800 at the same memory address 398 00:15:16,600 --> 00:15:22,399 great nice if you want to be a bit more 399 00:15:19,800 --> 00:15:26,519 efficient so let's start making some 400 00:15:22,399 --> 00:15:29,279 requests we have created attributes out 401 00:15:26,519 --> 00:15:33,160 of thin air and well I suppose creative 402 00:15:29,279 --> 00:15:36,959 methods out of thin air that's wonderful 403 00:15:33,160 --> 00:15:38,600 so let's modify our call method let's 404 00:15:36,959 --> 00:15:40,959 just throw a really simple request in 405 00:15:38,600 --> 00:15:42,920 there um note that I'm formatting the 406 00:15:40,959 --> 00:15:44,440 URL I'm really familiar with the virus 407 00:15:42,920 --> 00:15:46,279 total documentation so you are going to 408 00:15:44,440 --> 00:15:48,319 have to trust me that this tends to be 409 00:15:46,279 --> 00:15:49,759 how you access an endpoint on virus 410 00:15:48,319 --> 00:15:52,800 total you can look at the documentation 411 00:15:49,759 --> 00:15:54,560 itself other apis may not necessarily 412 00:15:52,800 --> 00:15:56,519 follow that format and there is some 413 00:15:54,560 --> 00:15:59,560 jiggery pokery if you're doing this for 414 00:15:56,519 --> 00:16:01,040 a bunch of them in that your class does 415 00:15:59,560 --> 00:16:04,360 need to accommodate for that or you need 416 00:16:01,040 --> 00:16:08,440 to create other versions of it for each 417 00:16:04,360 --> 00:16:10,920 API yeah so we do that and then we can 418 00:16:08,440 --> 00:16:13,160 do things like this there's no method IP 419 00:16:10,920 --> 00:16:15,600 addresses there's no method domains uh 420 00:16:13,160 --> 00:16:17,519 json. dumps is an actual method it would 421 00:16:15,600 --> 00:16:18,800 print a nicely formatted Json blob I'm 422 00:16:17,519 --> 00:16:20,560 not going to show you my console for 423 00:16:18,800 --> 00:16:22,360 this again you've probably seen enough 424 00:16:20,560 --> 00:16:24,360 Json blobs to last a lifetime if you're 425 00:16:22,360 --> 00:16:26,839 already here at the 426 00:16:24,360 --> 00:16:30,600 conference 427 00:16:26,839 --> 00:16:32,880 yeah so we can can expand on it this is 428 00:16:30,600 --> 00:16:35,600 great note in the previous one we were 429 00:16:32,880 --> 00:16:37,319 defaulting to a or we were only doing 430 00:16:35,600 --> 00:16:39,399 get requests in this one we will default 431 00:16:37,319 --> 00:16:41,199 to a get request you can do throw some 432 00:16:39,399 --> 00:16:44,480 quags in there attach it as a method 433 00:16:41,199 --> 00:16:46,560 equals whatever um we can modify our URL 434 00:16:44,480 --> 00:16:50,040 based on the uh based on the data that 435 00:16:46,560 --> 00:16:50,839 we're feeding it I really like quags um 436 00:16:50,040 --> 00:16:53,480 it's 437 00:16:50,839 --> 00:16:55,440 great problem with this youa might not 438 00:16:53,480 --> 00:16:58,319 like it 439 00:16:55,440 --> 00:17:00,519 um doesn't make code super readable but 440 00:16:58,319 --> 00:17:02,880 you get it out really quickly so I'm not 441 00:17:00,519 --> 00:17:05,480 sure whether this is super suitable for 442 00:17:02,880 --> 00:17:06,919 production um cuz you know you'd write 443 00:17:05,480 --> 00:17:08,640 this you'd prototype it you would 444 00:17:06,919 --> 00:17:10,079 definitely come back to refactor it to 445 00:17:08,640 --> 00:17:11,439 make sure that the documentation and the 446 00:17:10,079 --> 00:17:12,959 code was really good so that other 447 00:17:11,439 --> 00:17:14,640 people would know what to do next time 448 00:17:12,959 --> 00:17:17,559 that that would definitely happen 449 00:17:14,640 --> 00:17:19,039 right um yeah so this gives us the 450 00:17:17,559 --> 00:17:23,160 ability to kind of throw a different 451 00:17:19,039 --> 00:17:23,160 different methods at it that we might 452 00:17:23,760 --> 00:17:30,000 want yeah so essentially we get to this 453 00:17:27,799 --> 00:17:32,080 so I know that virus total search 454 00:17:30,000 --> 00:17:34,000 endpoint basically allows you to throw 455 00:17:32,080 --> 00:17:37,000 any data that you want at it and it will 456 00:17:34,000 --> 00:17:38,840 just run a very generic query but what 457 00:17:37,000 --> 00:17:42,039 we would get to here is that python code 458 00:17:38,840 --> 00:17:45,080 up the top would be equivalent of that 459 00:17:42,039 --> 00:17:47,320 curl request down on the bottom um which 460 00:17:45,080 --> 00:17:48,480 is great we can throw a bunch of stuff 461 00:17:47,320 --> 00:17:50,919 we don't have to be writing a whole 462 00:17:48,480 --> 00:17:52,720 bunch of methods it ends up being that 463 00:17:50,919 --> 00:17:56,200 that class in endpoint class is probably 464 00:17:52,720 --> 00:17:57,799 about 80 to 100 lines of code and you've 465 00:17:56,200 --> 00:18:00,320 got something working and then you can 466 00:17:57,799 --> 00:18:01,919 feed that through to your your uh or 467 00:18:00,320 --> 00:18:04,600 hopefully to your web application in my 468 00:18:01,919 --> 00:18:07,600 case um 469 00:18:04,600 --> 00:18:09,240 yeah we want to expand on it what 470 00:18:07,600 --> 00:18:11,919 further functionality do we want our 471 00:18:09,240 --> 00:18:14,480 client to have well obviously this is 472 00:18:11,919 --> 00:18:16,120 going to depend on your use case um in 473 00:18:14,480 --> 00:18:17,919 mine want it to be able to handle 474 00:18:16,120 --> 00:18:20,120 different request types cool we've kind 475 00:18:17,919 --> 00:18:22,240 of done that probably want it to handle 476 00:18:20,120 --> 00:18:24,400 data in the URI in the request body if 477 00:18:22,240 --> 00:18:26,159 it's a get request it's probably just 478 00:18:24,400 --> 00:18:28,799 request parameters in the URI if it's a 479 00:18:26,159 --> 00:18:30,360 post or a delete or a put you've 480 00:18:28,799 --> 00:18:32,480 probably got some kind of body that 481 00:18:30,360 --> 00:18:34,799 you're sending data to you probably want 482 00:18:32,480 --> 00:18:36,360 to check that the data that you're 483 00:18:34,799 --> 00:18:37,919 sending and the method that you're using 484 00:18:36,360 --> 00:18:41,120 on the Endo is valid you probably want 485 00:18:37,919 --> 00:18:43,960 to handle the response and if you've got 486 00:18:41,120 --> 00:18:46,080 other people that may be using this code 487 00:18:43,960 --> 00:18:49,360 maybe you want to turn it into a library 488 00:18:46,080 --> 00:18:50,880 um and maybe make their lives not too 489 00:18:49,360 --> 00:18:52,320 painful maybe you want to wrap those 490 00:18:50,880 --> 00:18:53,919 known end points into some nicely 491 00:18:52,320 --> 00:18:56,240 defined methods which we were kind of 492 00:18:53,919 --> 00:18:57,559 hoping to avoid initially but we got our 493 00:18:56,240 --> 00:18:59,240 prototype out we got some kind of 494 00:18:57,559 --> 00:19:02,159 working thing we can show our that look 495 00:18:59,240 --> 00:19:04,000 yeah it actually works um and then we 496 00:19:02,159 --> 00:19:06,480 want to make their lives easier so let's 497 00:19:04,000 --> 00:19:09,159 have a look at what that might look like 498 00:19:06,480 --> 00:19:10,960 again this is an example of how I did it 499 00:19:09,159 --> 00:19:11,799 if you have other ideas I'll leave that 500 00:19:10,960 --> 00:19:14,960 as 501 00:19:11,799 --> 00:19:16,880 a an exercise for the reader I guess um 502 00:19:14,960 --> 00:19:19,280 so one way to offload it so we modify 503 00:19:16,880 --> 00:19:21,400 our call maybe we validate the method 504 00:19:19,280 --> 00:19:23,919 maybe we check that there's something 505 00:19:21,400 --> 00:19:25,799 required we're checking our augs maybe 506 00:19:23,919 --> 00:19:29,080 we have some kind of response Handler 507 00:19:25,799 --> 00:19:30,919 what might those look like um I'll touch 508 00:19:29,080 --> 00:19:32,720 on how I did that for this specific API 509 00:19:30,919 --> 00:19:34,640 so we've modified our mpoint class we've 510 00:19:32,720 --> 00:19:36,320 got some methods and required it's 511 00:19:34,640 --> 00:19:37,880 probably not the best naming convention 512 00:19:36,320 --> 00:19:40,240 but please forgive me for 513 00:19:37,880 --> 00:19:42,360 this yeah when we instantiate it it 514 00:19:40,240 --> 00:19:44,760 might look like this so you go oh wow 515 00:19:42,360 --> 00:19:47,600 that's a list in a dictionary great um 516 00:19:44,760 --> 00:19:49,919 what am I doing with those so we want to 517 00:19:47,600 --> 00:19:51,360 validate our method we just check to see 518 00:19:49,919 --> 00:19:52,679 if the method is in the list that we've 519 00:19:51,360 --> 00:19:55,280 attached to the endpoint and it's really 520 00:19:52,679 --> 00:19:57,360 groundbreaking stuff here right um we 521 00:19:55,280 --> 00:19:58,480 might do the same for our dictionary um 522 00:19:57,360 --> 00:20:01,480 so I am speeding through these really 523 00:19:58,480 --> 00:20:04,240 fast last time apologize for that um 524 00:20:01,480 --> 00:20:06,559 again we just make sure that whatever 525 00:20:04,240 --> 00:20:09,960 parameter is attached to the diction or 526 00:20:06,559 --> 00:20:12,280 to the method entry in the dictionary um 527 00:20:09,960 --> 00:20:15,400 I would love to see alternative ways to 528 00:20:12,280 --> 00:20:17,200 do this um just out of curiosity if 529 00:20:15,400 --> 00:20:22,000 you've got recommendations or better 530 00:20:17,200 --> 00:20:24,600 things for me um yeah so we want to wrap 531 00:20:22,000 --> 00:20:25,919 it again really complex stuff going on 532 00:20:24,600 --> 00:20:28,799 here I love when the doc strings are 533 00:20:25,919 --> 00:20:31,799 longer than the method itself um we're 534 00:20:28,799 --> 00:20:34,159 literally just returning uh our 535 00:20:31,799 --> 00:20:36,400 predefined method or our non- predefined 536 00:20:34,159 --> 00:20:37,880 method um there's also an error here if 537 00:20:36,400 --> 00:20:39,840 you noticed that I was returning the 538 00:20:37,880 --> 00:20:41,280 Json blob in the previous one this is 539 00:20:39,840 --> 00:20:42,760 also returning the Json blob with the 540 00:20:41,280 --> 00:20:46,240 Json blob it would probably throw an 541 00:20:42,760 --> 00:20:49,679 error um I fixed this in my actual repo 542 00:20:46,240 --> 00:20:51,320 but not for this particular example um 543 00:20:49,679 --> 00:20:54,799 and then we might have a response 544 00:20:51,320 --> 00:20:57,840 Handler I really love Python 3.10 and 545 00:20:54,799 --> 00:20:59,679 the fact that you can do um patent 546 00:20:57,840 --> 00:21:02,440 matching like this it's genuinely my 547 00:20:59,679 --> 00:21:04,200 favorite thing I I you have to bring me 548 00:21:02,440 --> 00:21:06,400 screaming and crying into a version less 549 00:21:04,200 --> 00:21:06,400 than 550 00:21:06,440 --> 00:21:10,559 3.10 really annoying if you've written 551 00:21:08,799 --> 00:21:12,279 this code on your own machine and then 552 00:21:10,559 --> 00:21:13,640 you try and deploy and your boss goes 553 00:21:12,279 --> 00:21:15,200 actually it's a red hat server and the 554 00:21:13,640 --> 00:21:17,760 only version of python it has is like 555 00:21:15,200 --> 00:21:19,039 3.6 and you have to refactor it all uh 556 00:21:17,760 --> 00:21:23,520 definitely not something that's happened 557 00:21:19,039 --> 00:21:24,799 to me at like Friday at 5:30 uh um which 558 00:21:23,520 --> 00:21:29,440 was not my fault I didn't choose to 559 00:21:24,799 --> 00:21:33,360 deploy it then um I just uh yeah I just 560 00:21:29,440 --> 00:21:35,360 request um yeah so where do we go from 561 00:21:33,360 --> 00:21:37,320 here well you write more clients you 562 00:21:35,360 --> 00:21:38,840 play with these methods um I would 563 00:21:37,320 --> 00:21:41,600 really love to know other ways to use 564 00:21:38,840 --> 00:21:43,240 this I'm sure people have done similar 565 00:21:41,600 --> 00:21:45,400 things to do this that aren't 566 00:21:43,240 --> 00:21:47,360 necessarily related to rest clients 567 00:21:45,400 --> 00:21:50,279 there's almost certainly a lot more use 568 00:21:47,360 --> 00:21:52,159 cases um I'd be really shocked if I had 569 00:21:50,279 --> 00:21:55,039 come up with the only 570 00:21:52,159 --> 00:21:56,679 one yeah so I spread through that very 571 00:21:55,039 --> 00:21:58,000 quickly because I'm quite nervous and I 572 00:21:56,679 --> 00:22:01,440 speak really really quickly when I'm 573 00:21:58,000 --> 00:22:03,919 nervous so thank you for coming um my 574 00:22:01,440 --> 00:22:06,039 GitHub my blue sky and my Neo cities 575 00:22:03,919 --> 00:22:09,200 links are up there um if you ever want 576 00:22:06,039 --> 00:22:10,720 to read or look at my terrible code or 577 00:22:09,200 --> 00:22:12,080 my terrible posting or my terrible 578 00:22:10,720 --> 00:22:14,159 writing um they're not actually that 579 00:22:12,080 --> 00:22:18,640 terrible I hope people have complimented 580 00:22:14,159 --> 00:22:18,640 them um sometimes yeah thank you for 581 00:22:22,039 --> 00:22:27,600 coming oh a mug you question I'm happy 582 00:22:26,039 --> 00:22:29,440 to take questions if people have them I 583 00:22:27,600 --> 00:22:31,200 can't necessarily guarant able to answer 584 00:22:29,440 --> 00:22:33,159 them we we have a couple of minutes for 585 00:22:31,200 --> 00:22:35,559 questions because thank you for catching 586 00:22:33,159 --> 00:22:37,559 us up because this is great no one ever 587 00:22:35,559 --> 00:22:38,960 goes quickly and makes us catch up time 588 00:22:37,559 --> 00:22:41,120 like it's develops how are you making 589 00:22:38,960 --> 00:22:43,400 things better yeah I'm nervous and I 590 00:22:41,120 --> 00:22:45,120 speak really Qui this is tremendous uh I 591 00:22:43,400 --> 00:22:46,400 will do a runner around the room to uh 592 00:22:45,120 --> 00:22:50,039 please use the microphone when you ask 593 00:22:46,400 --> 00:22:53,000 some questions we have one over 594 00:22:50,039 --> 00:22:56,799 here good day just wondering if you've 595 00:22:53,000 --> 00:22:59,200 looked at open API at all and schemas 596 00:22:56,799 --> 00:23:00,760 yes so uh virus total has a nice open 597 00:22:59,200 --> 00:23:02,360 API specification I do know that there 598 00:23:00,760 --> 00:23:05,360 are tools where if you feed it an open 599 00:23:02,360 --> 00:23:07,840 API spec it will spit out a client for 600 00:23:05,360 --> 00:23:11,120 you um I have had issues with those 601 00:23:07,840 --> 00:23:15,360 before um in Python and in Rust they 602 00:23:11,120 --> 00:23:17,600 really don't work well in Rust um yeah 603 00:23:15,360 --> 00:23:20,520 um and I wasn't really thinking about 604 00:23:17,600 --> 00:23:22,279 the end schema um most of what my work 605 00:23:20,520 --> 00:23:24,880 involves is turning Jason blobs into 606 00:23:22,279 --> 00:23:26,720 slightly different Json blobs um having 607 00:23:24,880 --> 00:23:28,760 to yeah having to write out a full 608 00:23:26,720 --> 00:23:31,200 schema or have one pregenerated for me 609 00:23:28,760 --> 00:23:36,480 is usually a waste of time um cuz I 610 00:23:31,200 --> 00:23:36,480 don't yeah usually a Time limitation but 611 00:23:38,960 --> 00:23:42,400 yes any more 612 00:23:43,000 --> 00:23:50,480 questions cool well please join me in 613 00:23:46,000 --> 00:23:50,480 thanking Ash for a tremendous talk