1
00:00:07,540 --> 00:00:14,350
Everyone welcome back Johnny be here with slopes dot com and the time is now we are finally going to

2
00:00:14,380 --> 00:00:21,990
actually do something chant related we are going to pull down the chat channels in this.

3
00:00:22,330 --> 00:00:25,280
Or at least we're going to create the function that does that.

4
00:00:25,310 --> 00:00:27,000
I said let's go ahead and get started.

5
00:00:27,200 --> 00:00:32,860
Well we're going to do is we're going to create a new service called our message service and this is

6
00:00:32,860 --> 00:00:41,140
the service that's going to be responsible for storing our messages and channels and also the functions

7
00:00:41,140 --> 00:00:48,290
that will retrieve messages and channels as well as some other utilities that we'll be needing for it.

8
00:00:48,290 --> 00:00:48,730
All right.

9
00:00:48,910 --> 00:00:49,750
So let's get started.

10
00:00:49,750 --> 00:00:56,110
I'm going to come over here to services right click and say new file is going to be a swift file and

11
00:00:56,110 --> 00:01:01,040
say Next we're going to call this message service.

12
00:01:01,240 --> 00:01:08,470
I'm going to create that and then just like our other services this is going to be a class called Message

13
00:01:08,830 --> 00:01:12,970
Service like our other services.

14
00:01:12,970 --> 00:01:14,540
This is also going to be a singleton.

15
00:01:14,540 --> 00:01:24,500
So when I say static instance fool cool and message service it's Tenchi that.

16
00:01:25,000 --> 00:01:27,510
And so what do we do we're gonna put it here.

17
00:01:27,520 --> 00:01:32,830
Well we're going to have a whole bunch of channels stored in here as well as the messages for whichever

18
00:01:32,830 --> 00:01:34,600
channel is selected.

19
00:01:34,600 --> 00:01:36,830
So first we're going to need a couple of models right.

20
00:01:36,970 --> 00:01:39,320
So let's go ahead and create those.

21
00:01:39,360 --> 00:01:41,570
I just want to say new file.

22
00:01:41,800 --> 00:01:43,340
This is a swift file.

23
00:01:43,390 --> 00:01:48,940
And I say next and this is going to be a model called Channel.

24
00:01:49,520 --> 00:01:51,510
And I'm going to create it.

25
00:01:52,180 --> 00:02:00,350
And let's go ahead and take a peek at postman and see what kind of stuff is returned from a channel.

26
00:02:00,550 --> 00:02:07,480
So this is the function or the API endpoint find all channels is add to the one slash channel and that

27
00:02:07,480 --> 00:02:14,320
just returns all of the all of the channels that we get it's a GET request and so we can see here we

28
00:02:14,320 --> 00:02:18,010
have an array of objects of Chase objects.

29
00:02:18,100 --> 00:02:20,990
So we have one channel here and another channel here.

30
00:02:21,040 --> 00:02:24,760
The first one is called general and it doesn't have a description.

31
00:02:24,790 --> 00:02:32,200
The second channel is called the scription are inside the second channel is called random and its description

32
00:02:32,200 --> 00:02:33,770
is This is the random channel.

33
00:02:33,800 --> 00:02:34,200
OK.

34
00:02:34,340 --> 00:02:39,730
So anyways so these are the properties of a channel that were going to be working with.

35
00:02:39,970 --> 00:02:43,920
We're going to want to have the ID name and description.

36
00:02:43,930 --> 00:02:44,160
All right.

37
00:02:44,170 --> 00:02:46,330
So it's create a model on that.

38
00:02:46,330 --> 00:02:53,670
And this is going to be a struct Superstruct it's going to be called Channel.

39
00:02:54,100 --> 00:03:06,040
And like I said the properties that we want can be public private set VAR channel type toll and that's

40
00:03:06,040 --> 00:03:13,860
going to be the type of string we're going to have public private set ups

41
00:03:16,630 --> 00:03:23,810
are called Channel this corruption that's also going to be a string.

42
00:03:24,230 --> 00:03:25,640
And Leslie just going to copy this.

43
00:03:25,690 --> 00:03:29,820
So don't write that out copy and paste.

44
00:03:30,130 --> 00:03:33,110
And this is going to be ID.

45
00:03:33,400 --> 00:03:34,330
All right.

46
00:03:34,330 --> 00:03:37,710
So a very simple model for us.

47
00:03:37,770 --> 00:03:39,770
So that'll be nice and save that.

48
00:03:40,600 --> 00:03:52,060
And then we can jump back into our message service and say our channels is equal to an array of channel

49
00:03:52,690 --> 00:03:56,810
objects to instantiate that right there.

50
00:03:57,070 --> 00:04:01,630
And yes now we have a place to store our channels.

51
00:04:01,630 --> 00:04:05,640
Now we need a function to actually retrieve those.

52
00:04:05,650 --> 00:04:10,460
All right and so this is the the postman API call and we are.

53
00:04:10,480 --> 00:04:13,440
So this is what we get when we return it.

54
00:04:13,450 --> 00:04:19,900
What else do we need we need an authorization HEADER AND WE NEED OUR you or else so it's just the base

55
00:04:19,930 --> 00:04:22,560
you are L plus slash channel right.

56
00:04:22,570 --> 00:04:25,430
So let's go ahead and create our you are l for that.

57
00:04:25,430 --> 00:04:27,520
I'm going to come over to utility's here.

58
00:04:27,510 --> 00:04:36,310
Go to Constans and let's create our New You are going to say let you all on that score get underscore

59
00:04:36,310 --> 00:04:48,220
channels equal to the string interpolation of the base you are l and then just the channel and that'll

60
00:04:48,220 --> 00:04:49,780
do it.

61
00:04:49,780 --> 00:04:51,120
I just want to say that.

62
00:04:51,310 --> 00:04:57,940
Copy the euro constant and jump back into our message service.

63
00:04:57,940 --> 00:05:05,980
All right so now we are ready to create our function that is going to have our web request and go hit

64
00:05:05,980 --> 00:05:09,510
our API and bring back all of our channels for us.

65
00:05:09,530 --> 00:05:09,790
Right.

66
00:05:09,880 --> 00:05:18,790
So going to say phunk find all channel can and this is just a get request so we don't have to actually

67
00:05:18,790 --> 00:05:22,430
pass anything in the body or anything so we don't need anything to Pessin.

68
00:05:22,450 --> 00:05:30,510
So all we need is our completion handler would say at this date being completion handler.

69
00:05:30,570 --> 00:05:31,340
Okay.

70
00:05:31,660 --> 00:05:40,990
So now it's going create our Alamo fire request to say Alamo fire DOD request and we need to actually

71
00:05:40,990 --> 00:05:46,670
import those of course from the same port Alamo fire and import swiftie.

72
00:05:46,730 --> 00:05:48,290
On there you go.

73
00:05:48,340 --> 00:05:49,550
So we got it.

74
00:05:49,560 --> 00:05:51,310
Alamo fire request.

75
00:05:51,380 --> 00:05:52,390
And there we go.

76
00:05:52,630 --> 00:05:59,360
And the you are all that we want to hit is your no underscore get channels.

77
00:05:59,530 --> 00:06:04,090
The method is to get the parameters are nil.

78
00:06:04,480 --> 00:06:07,680
The encoding is of type.

79
00:06:08,680 --> 00:06:10,860
Jason do.

80
00:06:10,870 --> 00:06:12,250
What is it.

81
00:06:12,660 --> 00:06:19,700
G encoding that default and the headers is we do need a bearer headers.

82
00:06:19,730 --> 00:06:20,060
Right.

83
00:06:20,140 --> 00:06:21,480
Barer header.

84
00:06:21,700 --> 00:06:28,840
And the response that we want we want it to be in the form of G songs so response Dusan and press return

85
00:06:28,870 --> 00:06:35,330
and then return again and then renaming the data response to response.

86
00:06:35,740 --> 00:06:40,120
And then just deleting that placeholder text.

87
00:06:40,150 --> 00:06:43,530
All right so we we pinged the API.

88
00:06:43,720 --> 00:06:48,330
We've sent it the information that we want and now we're going to receive it back.

89
00:06:48,490 --> 00:06:55,960
And so just like we've done in the past we're going to see if response that resolved that error is equal

90
00:06:55,960 --> 00:07:07,060
to nil then we're going to do something with it or else we're going to say completion is false and we're

91
00:07:07,060 --> 00:07:09,190
going to debug the print.

92
00:07:09,190 --> 00:07:10,000
We're going to debug print.

93
00:07:10,000 --> 00:07:21,160
The problem is we're going to say respon stopped resolved that error as in the case that I'll just print

94
00:07:21,160 --> 00:07:22,660
out the response.

95
00:07:22,930 --> 00:07:25,440
If we were successful then what are we going to do.

96
00:07:25,570 --> 00:07:30,300
Well we're going to do like we've done in the past we're going to parse out the Jason.

97
00:07:30,340 --> 00:07:33,490
All right so we're going to create some object out of the data.

98
00:07:33,490 --> 00:07:43,480
So first off let's get the data a guard let data equal response result that I'm type just response that

99
00:07:43,780 --> 00:07:45,650
data you go.

100
00:07:46,180 --> 00:07:48,860
Else return.

101
00:07:49,510 --> 00:07:55,090
So now we're going to turn that data into some sort of chase on but coming back here to postpone it

102
00:07:55,090 --> 00:07:56,950
we see that this is actually an array.

103
00:07:57,190 --> 00:08:00,720
So we want to turn our Jason into an array object.

104
00:08:00,910 --> 00:08:11,280
So using Swiftie juice that's super easy we just say if let Jason is equal to Jason and then we're going

105
00:08:11,280 --> 00:08:18,490
to say data are going to pass in the data and then we're gonna say adopt a.

106
00:08:18,760 --> 00:08:19,730
Okay.

107
00:08:20,580 --> 00:08:23,250
And so that gives us an array of adjacent objects.

108
00:08:23,280 --> 00:08:30,790
And now we can say for item in chase on sort of we're going to loop through each item in the sun array

109
00:08:31,290 --> 00:08:37,440
and we're going to say no we're going to parse out the actual individual properties of the channel game

110
00:08:37,450 --> 00:08:40,790
games we're going to say let name equal.

111
00:08:41,340 --> 00:08:44,480
And that's going to be item and drill into it.

112
00:08:44,490 --> 00:08:51,040
Look for the key of name and cast that as a string value.

113
00:08:51,150 --> 00:09:01,980
Then we're going to say let's channel description a cool item and we're going to search for the key

114
00:09:02,070 --> 00:09:11,970
of description and that's going to be cast to a string value and I'm going to say let the equal item

115
00:09:12,780 --> 00:09:21,850
and we're going to drill into it and search for the key of underscore ID string value.

116
00:09:22,110 --> 00:09:23,520
And if you're curious where I'm getting these.

117
00:09:23,520 --> 00:09:25,030
Again just from right here.

118
00:09:25,050 --> 00:09:26,620
So this is the Gees on response.

119
00:09:26,630 --> 00:09:31,580
And so these are the keys that we need to be looking for.

120
00:09:31,580 --> 00:09:31,910
All right.

121
00:09:31,920 --> 00:09:42,120
And then with those with these three variables we can now instantiate and create a new channel object.

122
00:09:42,120 --> 00:09:42,610
OK.

123
00:09:42,830 --> 00:09:54,720
So when I say let channel equal and then using the default struct initializer we can just say channel

124
00:09:54,720 --> 00:10:03,390
title is equal to name channel description is equal to channel description and the ID is equal to the

125
00:10:03,480 --> 00:10:04,400
ID.

126
00:10:04,650 --> 00:10:10,680
OK so each time we loop through one of the items in the cell array we're going to extract the properties

127
00:10:10,680 --> 00:10:11,470
that we need.

128
00:10:11,580 --> 00:10:16,290
And then we're going to initialize a new channel object from the struct initializer.

129
00:10:16,380 --> 00:10:22,680
And then we're going to add this new channel to our channels array that we that we store inside of our

130
00:10:22,680 --> 00:10:30,990
message service we're in say self-taught channels that append and the new element is this new channel

131
00:10:30,990 --> 00:10:31,650
here.

132
00:10:31,710 --> 00:10:37,590
So that's how we loop through those objects and add them to our channels array right here and that's

133
00:10:37,590 --> 00:10:42,100
it we can say completion is true right here.

134
00:10:42,510 --> 00:10:44,190
Okay.

135
00:10:45,360 --> 00:10:50,160
So that's that's how we've been doing some parsing up till now.

136
00:10:50,160 --> 00:10:51,180
Right.

137
00:10:51,880 --> 00:11:01,490
And I like doing it this way but in swift for there's a new way and it's really nice in some ways.

138
00:11:01,560 --> 00:11:06,060
So I definitely want to show you that we're probably going to continue doing it this way.

139
00:11:06,060 --> 00:11:10,500
You can make the choice yourself going forward which way you want to do it.

140
00:11:10,510 --> 00:11:18,490
But the new way is they have new decoded and protocols and it's super nice.

141
00:11:18,690 --> 00:11:20,760
So I'm going to show you how to do that.

142
00:11:20,760 --> 00:11:24,470
So what we're going to do is I'm going to go ahead and just comment out right here.

143
00:11:28,790 --> 00:11:37,120
And to make it work well we have to do is we have to make our channel conform to the decoded bull protocol.

144
00:11:37,370 --> 00:11:38,100
OK.

145
00:11:38,420 --> 00:11:45,650
And then the other thing that we have to do and this is the reason that I kind of don't like it is we

146
00:11:45,650 --> 00:11:55,130
have to make our vegans find a way to make this visible while we still see it we have to make our model

147
00:11:55,430 --> 00:12:00,270
mirror exactly what we see in the japes our response.

148
00:12:00,270 --> 00:12:03,360
OK so just can put this over here on my other window.

149
00:12:03,440 --> 00:12:05,770
So these names have to be the same so.

150
00:12:05,810 --> 00:12:06,740
And in the same order.

151
00:12:06,740 --> 00:12:09,670
So this needs to be underscore ID.

152
00:12:09,940 --> 00:12:12,210
This needs to be name.

153
00:12:12,590 --> 00:12:16,460
This needs to be the scription

154
00:12:20,270 --> 00:12:21,560
right description.

155
00:12:21,560 --> 00:12:25,870
And then the thing the other thing that it really like is that you have to have everything.

156
00:12:26,000 --> 00:12:29,950
So before were just ignoring this underscore underscore V.

157
00:12:30,230 --> 00:12:35,780
I'm not really sure what it is some sort of mongered database identifier or something but because it's

158
00:12:35,780 --> 00:12:36,700
in the sun.

159
00:12:36,740 --> 00:12:39,270
We also have to have it here in our model.

160
00:12:39,410 --> 00:12:45,480
So I'm going to go ahead and add that the C control copy and paste.

161
00:12:45,980 --> 00:12:54,510
And so then what you can do is oops this should be underscore underscore the right.

162
00:12:54,520 --> 00:13:01,090
So then what we can do is we can come back into message service and we can actually do the parsing and

163
00:13:01,100 --> 00:13:02,630
practically one line.

164
00:13:02,630 --> 00:13:04,300
And so that's what's really nice about it.

165
00:13:04,610 --> 00:13:07,770
So we're going to we're going to do it say I do.

166
00:13:08,780 --> 00:13:14,420
And the reason we have to have a do and catch block is because we're going to be using the sun decoder

167
00:13:14,420 --> 00:13:15,910
which can throw an error.

168
00:13:15,950 --> 00:13:25,380
So we're going to say self dobbed channels is equal to try.

169
00:13:25,590 --> 00:13:28,130
And so when you have a do you need to try.

170
00:13:28,680 --> 00:13:39,160
Jason decoder dot de-code and systemone we want and the type is.

171
00:13:39,790 --> 00:13:43,730
So we're going to say the type is channel.

172
00:13:43,960 --> 00:13:46,510
So we made it an array because we know that it's an array.

173
00:13:46,510 --> 00:13:47,940
So channel that self.

174
00:13:48,160 --> 00:13:57,130
And then you just pass in the data right here and that is all of the JS on parsing that you need it

175
00:13:57,160 --> 00:14:07,000
kind of just takes your channel model and it just crams everything that comes from the juice on it decodes

176
00:14:07,000 --> 00:14:13,330
it and stuffs it right into whatever variables you have in the message service or in your in your channel

177
00:14:13,330 --> 00:14:18,540
model as long as it matches correctly and then like I said because this is a try.

178
00:14:18,580 --> 00:14:20,340
It has the potential to throw an heiress.

179
00:14:20,350 --> 00:14:21,670
We need to handle that.

180
00:14:21,670 --> 00:14:28,350
So when I say here catch that error and then we're just going to debug.

181
00:14:28,360 --> 00:14:30,740
Print that error.

182
00:14:31,030 --> 00:14:31,590
OK.

183
00:14:31,840 --> 00:14:33,980
Has any.

184
00:14:34,630 --> 00:14:35,270
And.

185
00:14:35,290 --> 00:14:35,660
Yes.

186
00:14:35,680 --> 00:14:37,160
That is.

187
00:14:37,630 --> 00:14:38,060
That's it.

188
00:14:38,110 --> 00:14:39,540
That's all you got to do.

189
00:14:39,550 --> 00:14:41,750
So let's go ahead and test this.

190
00:14:42,220 --> 00:14:42,960
So here.

191
00:14:42,960 --> 00:14:43,850
Under it.

192
00:14:43,870 --> 00:14:49,780
I'm going to say print and then I'm going to say self channels.

193
00:14:50,110 --> 00:14:53,020
I'm going to save it and let's run this Frum's.

194
00:14:53,230 --> 00:14:58,130
Let's just put it in the chad b c you did load.

195
00:14:58,380 --> 00:15:07,700
Ok so I want to say a message service instance you know what actually we're going to need to.

196
00:15:08,080 --> 00:15:12,330
I think we have a logon user so I'm going to be super careful here.

197
00:15:12,400 --> 00:15:14,700
Message Service instance.

198
00:15:15,290 --> 00:15:22,600
Find all channel and say success and we're not really doing anything with it so I'm just going to run

199
00:15:22,600 --> 00:15:23,570
it.

200
00:15:23,750 --> 00:15:29,170
We've got an error here says expected to decode a string but found a number instead.

201
00:15:29,200 --> 00:15:30,070
That's my bad.

202
00:15:30,070 --> 00:15:39,100
Here in the channel model if we look at post-bin this property here is actually in a number so let's

203
00:15:39,190 --> 00:15:41,400
instead cast that to an int.

204
00:15:41,620 --> 00:15:46,790
And if you make it optional with this Dakotah will if it's not there then it just puts Nyland there.

205
00:15:46,810 --> 00:15:51,050
So I don't know if that property is always going to be this or I'm going to make that an end or I mean

206
00:15:51,110 --> 00:15:53,920
a optional.

207
00:15:54,430 --> 00:15:54,870
All right.

208
00:15:54,880 --> 00:15:56,150
And here we go.

209
00:15:56,290 --> 00:15:58,730
So we can see here.

210
00:15:58,870 --> 00:16:03,970
Yeah we got we can see some of the properties that we would expect in the channel name general description.

211
00:16:03,970 --> 00:16:06,720
Welcome to the water cooler.

212
00:16:06,900 --> 00:16:14,380
Yes so it is it is printing out the channel information so yeah that parsing is not parsing is working

213
00:16:15,070 --> 00:16:18,620
and so is kind of just up to you how you want to do it.

214
00:16:19,150 --> 00:16:26,470
So like I said the drawbacks for me are that you have to have everything in the model and you have to

215
00:16:26,470 --> 00:16:31,890
have the naming convention the same as what the sun on returns.

216
00:16:32,700 --> 00:16:41,020
I mean this is super super nice being able to just do that one line of that one line of code instead

217
00:16:41,020 --> 00:16:50,710
of having to parse through lots of different properties and stuff but still in the end I actually prefer

218
00:16:50,830 --> 00:16:56,980
the old way just because I can ignore some things in the in the model.

219
00:16:57,040 --> 00:17:04,150
So I'm just going to command Z here in the channel all the way back to how we had it with our with our

220
00:17:04,150 --> 00:17:10,180
naming convention and also being able to ignore properties in the sun that we just all really care about.

221
00:17:10,190 --> 00:17:13,490
It's want to save that and then come back to our message service.

222
00:17:13,780 --> 00:17:20,600
And here I'm going to go ahead and command Z all the way back as well.

223
00:17:21,050 --> 00:17:26,530
Yeah like I said it just wanted to show you that because you know this is a course on swift for it is

224
00:17:26,530 --> 00:17:27,530
new.

225
00:17:27,550 --> 00:17:31,370
So you guys definitely deserve to learn about it.

226
00:17:31,440 --> 00:17:38,350
Just personally I still prefer doing it this way and I feel like it just gives it gives me more control

227
00:17:38,350 --> 00:17:40,450
over what my models look like.

228
00:17:40,450 --> 00:17:47,030
And also you know I'm not being forced to use everything that they use on spits back at us.

229
00:17:47,440 --> 00:17:54,490
But yes so up to you but once again here let's go ahead and let's make sure it's working with this way

230
00:17:54,490 --> 00:18:03,990
as well so I'm going to print out the let's say self dot channels and then I'll pick the all access

231
00:18:04,000 --> 00:18:06,760
the zeroth item and say Dot

232
00:18:09,380 --> 00:18:10,950
channel channel title.

233
00:18:11,260 --> 00:18:12,800
Let's do that.

234
00:18:13,310 --> 00:18:16,000
All right and we're loaded and sure enough there we go.

235
00:18:16,110 --> 00:18:21,980
General is the name of the first channel or so how are we doing on time.

236
00:18:21,980 --> 00:18:25,180
Looks like we're out of time here but there you go.

237
00:18:25,180 --> 00:18:33,860
We have created the function to hit our API and bring back to us all of the channels that are that are

238
00:18:33,860 --> 00:18:35,220
currently on the API.

239
00:18:35,420 --> 00:18:39,830
All right so in the next lessons we're actually going to do something with this data.

240
00:18:39,950 --> 00:18:47,780
We're going to work in our table view inside of the channel of the sea and we're going to start make

241
00:18:47,780 --> 00:18:50,510
it so that we can actually visualize the channel data there.

242
00:18:50,510 --> 00:18:51,180
All right.

243
00:18:51,440 --> 00:18:54,130
So that's about it for now.

244
00:18:54,140 --> 00:18:59,130
I'm going to go ahead and finish off by adding the progress that we have made and committing it.

245
00:18:59,300 --> 00:19:00,680
And I will see you in the next one.
