1 00:00:00,480 --> 00:00:03,080 Hello, and welcome back to the Pharo course. 2 00:00:03,240 --> 00:00:05,600 This segment covers two more aspects. 3 00:00:05,760 --> 00:00:07,360 I'll cover them in depth. 4 00:00:07,520 --> 00:00:11,280 You'll use them yourselves in the TinyBlog exercises. 5 00:00:11,520 --> 00:00:16,160 First, what web development can we do once we have described data? 6 00:00:16,320 --> 00:00:18,360 Second, how does Seaside use REST? 7 00:00:18,520 --> 00:00:22,080 Seaside is one of the frameworks for REST in Pharo. 8 00:00:22,280 --> 00:00:24,520 Let's start with the first subject. 9 00:00:24,680 --> 00:00:27,640 Magritte is a framework based on the hypothesis 10 00:00:27,800 --> 00:00:30,320 that if you describe your data once, 11 00:00:30,480 --> 00:00:33,680 it can generate several objects based on this data. 12 00:00:33,880 --> 00:00:36,760 Webforms and graphic interfaces, for example. 13 00:00:36,920 --> 00:00:39,360 In this course, I'll be focusing on 14 00:00:39,520 --> 00:00:42,800 showing you how to generate Seaside components 15 00:00:43,000 --> 00:00:46,160 so that you can code web applications faster. 16 00:00:46,360 --> 00:00:49,160 We'll take the class "Address" as an example. 17 00:00:49,320 --> 00:00:51,480 These are Swiss addresses. 18 00:00:51,920 --> 00:00:55,000 An address is defined as a street, a place, 19 00:00:56,680 --> 00:00:58,960 a postal code, and a canton. 20 00:00:59,320 --> 00:01:03,560 I have an instance which describes a particular street. 21 00:01:04,040 --> 00:01:07,000 So far, this is just standard programming. 22 00:01:07,400 --> 00:01:09,000 Now, what Magritte does 23 00:01:09,200 --> 00:01:11,720 is describe the fields in the class. 24 00:01:11,880 --> 00:01:16,240 It will describe a string in such a way as to define it as a street name. 25 00:01:16,560 --> 00:01:19,640 The postal code description is more interesting. 26 00:01:19,800 --> 00:01:21,920 First of all, we are going to say 27 00:01:22,080 --> 00:01:25,240 an address is not valid without a postal code. 28 00:01:25,400 --> 00:01:28,080 Addresses lacking postal codes are invalid. 29 00:01:28,480 --> 00:01:33,800 And all Swiss postal codes are figures between 1000 and 9999. 30 00:01:33,960 --> 00:01:37,160 This is specified in the NumberDescription. 31 00:01:37,400 --> 00:01:39,960 This information can be taken into account 32 00:01:40,120 --> 00:01:41,840 for automatic form validation. 33 00:01:42,000 --> 00:01:46,840 For Place, we also describe it as required, and as place. 34 00:01:47,000 --> 00:01:50,640 Moreover, in Switzerland, there are 26 cantons. 35 00:01:50,880 --> 00:01:54,800 The canton is part of a list, so we have SingleOptionDescription. 36 00:01:55,080 --> 00:01:58,960 It is required. We want it to be sorted, and the options listed. 37 00:01:59,120 --> 00:02:03,120 So the list of options is limited to these 26. 38 00:02:03,800 --> 00:02:04,920 Once that's done, 39 00:02:05,240 --> 00:02:07,200 what can we do with Magritte? 40 00:02:07,400 --> 00:02:10,880 Here is how the address is described in Pharo. 41 00:02:11,240 --> 00:02:16,320 We'll define a descriptionStreet as a StringDescription, 42 00:02:17,800 --> 00:02:20,200 with such and such a label and priority. 43 00:02:20,360 --> 00:02:23,880 For street numbers, I define a NumberDescription: 44 00:02:24,960 --> 00:02:27,640 I assign a different priority and label. 45 00:02:27,800 --> 00:02:32,200 I specify "Required" and give my minimum and maximum values. 46 00:02:32,520 --> 00:02:36,080 The very first thing I can do with this description 47 00:02:36,240 --> 00:02:39,640 is write a little tiny program to interpret the address 48 00:02:39,920 --> 00:02:41,200 and generate reports. 49 00:02:41,560 --> 00:02:44,480 I take an address, 50 00:02:44,800 --> 00:02:47,080 and run it through this program, 51 00:02:47,480 --> 00:02:49,480 which generates a little report: 52 00:02:50,160 --> 00:02:52,520 The name of the street, 53 00:02:55,400 --> 00:02:58,040 The place - Bern, and the canton, Bern. 54 00:02:58,200 --> 00:02:59,440 Postal code 3012. 55 00:02:59,640 --> 00:03:03,800 I can use the fact that my data has been described 56 00:03:04,440 --> 00:03:06,440 to generate reports. 57 00:03:06,920 --> 00:03:09,080 That was the simplest kind. 58 00:03:09,240 --> 00:03:12,480 But now we'll do more complex things with Seaside, 59 00:03:12,880 --> 00:03:15,160 like creating a visual component. 60 00:03:15,400 --> 00:03:16,640 Here, I am saying, 61 00:03:17,240 --> 00:03:20,400 "Address object, I want to see you as a component." 62 00:03:20,560 --> 00:03:22,600 That generates this part. 63 00:03:24,760 --> 00:03:29,600 I'll add a validation form, which gives me "Save" and "Cancel." 64 00:03:30,200 --> 00:03:33,160 And I'll write a "call" command so it displays. 65 00:03:33,320 --> 00:03:37,960 As you can see, I managed all that without even using the Seaside DSL. 66 00:03:38,320 --> 00:03:41,880 Described, my web components are generated automatically. 67 00:03:42,200 --> 00:03:43,600 That's powerful! 68 00:03:43,880 --> 00:03:46,560 Here is what the company Quuve does 69 00:03:46,840 --> 00:03:50,400 Quuve is an American portfolio management platform. 70 00:03:50,600 --> 00:03:54,080 The whole display was done with Seaside and Magritte. 71 00:03:54,400 --> 00:03:57,160 All the reports are generated automatically. 72 00:03:58,280 --> 00:04:01,320 That's a serious gain in productivity! 73 00:04:01,560 --> 00:04:05,800 The idea of this course is to give you some intuition 74 00:04:05,960 --> 00:04:08,960 of what can be done when you describe your data, 75 00:04:09,960 --> 00:04:13,280 especially with web component generation. 76 00:04:13,840 --> 00:04:15,560 You'll do that in TinyBlog. 77 00:04:15,760 --> 00:04:20,680 Now, I'll show you the potential in Seaside's REST layer. 78 00:04:21,080 --> 00:04:25,920 It enables client-server communication in the absence of visual components. 79 00:04:26,280 --> 00:04:29,760 REST is integrated into Seaside smoothly. 80 00:04:30,080 --> 00:04:32,440 Domain objects will be annotated. 81 00:04:32,600 --> 00:04:36,040 You'll have a natural conversion between URL parameters 82 00:04:36,280 --> 00:04:38,720 and SmallTalk or Pharo methods. 83 00:04:39,800 --> 00:04:43,760 To give you an idea of the complexity, 84 00:04:44,040 --> 00:04:47,880 we'll define what we call a "filter": 85 00:04:48,120 --> 00:04:51,240 TinyBlog Restful Filter. 86 00:04:51,400 --> 00:04:54,800 is a subclass of WARestful - it doesn't matter. 87 00:04:55,160 --> 00:05:00,040 The important part is the filter I add down here when I create my application. 88 00:05:00,480 --> 00:05:02,360 You'll see it in the exercise. 89 00:05:02,960 --> 00:05:06,560 Now, let's imagine that I want to get all the blogs 90 00:05:06,880 --> 00:05:09,840 that I have on my TinyBlog server. 91 00:05:11,120 --> 00:05:11,960 Here's how: 92 00:05:12,160 --> 00:05:15,680 First, I define a method 93 00:05:17,040 --> 00:05:20,320 in the RestfulFilter class, a method called listAll. 94 00:05:21,520 --> 00:05:25,280 It corresponds to the anchor I'm going to use: 95 00:05:26,280 --> 00:05:30,720 I tell it to use its "get" from the REST protocol 96 00:05:31,160 --> 00:05:33,200 to generate text in json form. 97 00:05:33,360 --> 00:05:37,760 I create a Pharo string stream 98 00:05:38,080 --> 00:05:41,600 I want the stream to contain all available blogs. 99 00:05:42,000 --> 00:05:45,280 I'll take each blog - and you see another iterator - 100 00:05:45,640 --> 00:05:48,400 Take each blog and convert it to Javascript, 101 00:05:48,800 --> 00:05:50,400 separated by commas. 102 00:05:51,360 --> 00:05:53,040 That gives me what I want. 103 00:05:53,200 --> 00:05:57,200 Now we want to do something a little more advanced. 104 00:05:58,000 --> 00:06:03,280 Say I want to find a blog by searching for a title match. 105 00:06:04,440 --> 00:06:06,760 Here, I can use "post," for example. 106 00:06:07,080 --> 00:06:11,560 I'll say I want this blog post, by giving the title next to it. 107 00:06:11,760 --> 00:06:16,160 I do that by defining a "post" method that takes a title as a parameter 108 00:06:16,720 --> 00:06:18,640 along with the "path" concept. 109 00:06:18,880 --> 00:06:24,360 I tell it, when the URL contains http..., post/ 110 00:06:25,200 --> 00:06:28,880 and something here, those characters are the title. 111 00:06:29,200 --> 00:06:33,280 That's what we see here, inside the wavy brackets. 112 00:06:35,520 --> 00:06:39,720 Likewise, I tell it, that will produce text in json form. 113 00:06:40,080 --> 00:06:44,080 Then what I do is call upon TinyBlog functionalities. 114 00:06:44,240 --> 00:06:48,520 I ask, do you have a post with a title specified as a parameter? 115 00:06:49,200 --> 00:06:51,640 Yes? If it's empty, I return an error. 116 00:06:51,800 --> 00:06:55,200 Otherwise, I put it in my stream and render my stream. 117 00:06:55,640 --> 00:06:57,800 There are lots of things we can do. 118 00:06:58,120 --> 00:07:01,120 If I want to do a search, it's the same thing. 119 00:07:05,000 --> 00:07:06,640 In my URL line, I type 120 00:07:06,920 --> 00:07:10,760 "search: title" with a value, it will be matched automatically. 121 00:07:10,920 --> 00:07:13,880 My title will appear here. 122 00:07:16,520 --> 00:07:20,160 That's how you work with REST in Seaside. 123 00:07:20,560 --> 00:07:24,560 You can have your graphic components in Seaside 124 00:07:24,720 --> 00:07:27,680 and a way to expose your domain functionalities 125 00:07:27,840 --> 00:07:28,880 with a REST API. 126 00:07:29,560 --> 00:07:32,440 I've only shown you "gets." 127 00:07:32,760 --> 00:07:36,840 But all the other REST functionalities are available: 128 00:07:37,240 --> 00:07:39,160 Delete, Post, Get, etc. 129 00:07:40,440 --> 00:07:43,720 I also showed you how Seaside, paired with Magritte, 130 00:07:43,880 --> 00:07:46,600 generates powerful forms, automatically. 131 00:07:47,040 --> 00:07:48,840 And REST is well integrated. 132 00:07:49,080 --> 00:07:53,000 Often, people use Teapot as a REST solution, as well. 133 00:07:53,160 --> 00:07:56,440 It's a Zinc overlayer for doing REST prototypes. 134 00:07:56,960 --> 00:08:01,200 It's a quick way to find out if their REST API is working. 135 00:08:01,680 --> 00:08:02,760 Have fun with it. 136 00:08:02,920 --> 00:08:06,640 There's documentation in the Web Enterprise Book.