1
00:00:07,020 --> 00:00:08,390
What's up everybody.

2
00:00:08,410 --> 00:00:10,560
JOHNNY B here with slopes dot com.

3
00:00:10,610 --> 00:00:12,120
And welcome back to.

4
00:00:12,370 --> 00:00:13,200
Welcome back to class.

5
00:00:13,200 --> 00:00:18,870
We're continuing to work on our chat app and it's starting to shape up pretty well indeed.

6
00:00:18,970 --> 00:00:24,550
We're back in X code now we've spent the last few lessons talking about web requests and setting up

7
00:00:24,550 --> 00:00:30,730
our API eyes on Heroku and optionally Azi on a local host as well.

8
00:00:30,910 --> 00:00:39,490
So but now we're ready to jump back into X code and actually hook up with the API and start getting

9
00:00:39,490 --> 00:00:42,880
and sending data back and forth between the API and our client apps.

10
00:00:42,880 --> 00:00:44,580
So it's going to be some pretty cool stuff.

11
00:00:44,590 --> 00:00:46,000
So let's jump into it.

12
00:00:46,090 --> 00:00:48,970
This is where we left off in our app.

13
00:00:49,060 --> 00:00:58,210
You know we we've got the menu open menu working we can click on loggin and this isn't working yet but

14
00:00:58,210 --> 00:01:02,950
if we go to don't have an account to sign up here and this is where we're going to start working now

15
00:01:02,950 --> 00:01:10,470
because the how the API is set up when you don't have a user yet when you want to create a user there's

16
00:01:10,480 --> 00:01:17,410
three steps you got to go through socialist pull up postman here if you see over here in our in our

17
00:01:17,470 --> 00:01:22,350
requests from the collection that you imported the postman you'll see we have three numbers here.

18
00:01:22,360 --> 00:01:23,970
We got one two and three.

19
00:01:23,980 --> 00:01:28,870
The first is two off is to register a new user.

20
00:01:28,900 --> 00:01:29,460
OK.

21
00:01:29,710 --> 00:01:38,310
And so what that does is it sends to the API an email and password and registers a user for that account.

22
00:01:38,320 --> 00:01:42,400
The next step is to log in that user.

23
00:01:42,730 --> 00:01:44,500
And when you log in the user.

24
00:01:44,830 --> 00:01:51,490
This is where we get back the token that we talked about in Lesson 6 about web requests an API is and

25
00:01:51,490 --> 00:01:58,870
this token is necessary to do any of the further requests that we want to do like sending a message

26
00:01:58,930 --> 00:02:03,070
retrieving chat messages you know adding channels.

27
00:02:03,070 --> 00:02:09,340
Anything on the API that is locked down meaning you know the API has lots of functions for doing different

28
00:02:09,340 --> 00:02:12,250
things and some of them are locked down.

29
00:02:12,280 --> 00:02:19,210
So you have to have that API her sorry you have to have that off tokin key that is inside of the head

30
00:02:19,210 --> 00:02:21,790
or to access those functions.

31
00:02:21,790 --> 00:02:22,330
Right.

32
00:02:22,690 --> 00:02:28,480
So first we're going to register a user they're going to log that user in and then we're going to create

33
00:02:28,570 --> 00:02:30,060
a new user.

34
00:02:30,220 --> 00:02:35,080
All right so registering a user and creating a user or a little bit different when you register it it

35
00:02:35,080 --> 00:02:39,830
just creates an email and password user.

36
00:02:39,940 --> 00:02:44,560
When we do the AD user what we're going to be doing is we're going to be adding things like the avatar

37
00:02:44,560 --> 00:02:49,090
name the Avatar background color and some other information that we need for it.

38
00:02:49,090 --> 00:02:49,730
All right.

39
00:02:49,990 --> 00:02:52,300
So let's get started in this video.

40
00:02:52,310 --> 00:02:56,730
All we're going to do is get the registering the user set up.

41
00:02:56,740 --> 00:02:57,350
OK.

42
00:02:57,700 --> 00:03:00,400
So let's let's take a look at just this.

43
00:03:00,430 --> 00:03:02,800
Register user call OK.

44
00:03:02,830 --> 00:03:06,000
So right now it's set up to be on our local host.

45
00:03:06,010 --> 00:03:13,510
So let's go ahead and fire our local host up to an open terminal and open up a new tab.

46
00:03:13,900 --> 00:03:20,240
And if you remember from our last video to get the local hosts up and running we also have to have Mongo

47
00:03:20,270 --> 00:03:24,550
d the the database running's we're going to say Mongo DE

48
00:03:27,670 --> 00:03:31,960
and wait for that to get up and running you can see here it says that waiting for connections on port

49
00:03:32,050 --> 00:03:33,570
2 7 0 1 7.

50
00:03:33,700 --> 00:03:40,660
And that's just what we want to see them open up a new tab and we need to change directory into our

51
00:03:41,190 --> 00:03:42,850
Power Chat API folders.

52
00:03:42,840 --> 00:03:49,810
I'm going to say CD and that's on our desktop so I'd say CD to desktop and then CD Mac chat API.

53
00:03:50,020 --> 00:03:50,500
OK.

54
00:03:50,770 --> 00:03:59,540
So once we're in here after you've done your NPM install all we got to do is do any AM run dev.

55
00:03:59,950 --> 00:04:03,390
All right so that is up and running says database connection ready.

56
00:04:03,670 --> 00:04:13,900
So now we can actually send this register call to our local hosted API and I can say something like

57
00:04:14,290 --> 00:04:19,960
Jay at test her register dot com.

58
00:04:20,020 --> 00:04:28,050
So we talked about web requests in the past and as part of the content that you can send with an HDTV

59
00:04:28,060 --> 00:04:30,780
request is a header and a body.

60
00:04:30,790 --> 00:04:35,360
So like I said some of them are locked down this API request is not.

61
00:04:35,410 --> 00:04:39,370
So you can see here that we don't have an authorization header or anything.

62
00:04:39,370 --> 00:04:44,050
All we're doing is saying what kind of the content type is but then here in the body we're actually

63
00:04:44,050 --> 00:04:54,280
sending Jaison in the form of an email with a email string value and a key of password and a string

64
00:04:54,850 --> 00:04:56,710
value for that key password.

65
00:04:56,950 --> 00:05:04,750
And if we say send here what we're doing is we're we're sending this message the post request to our

66
00:05:05,020 --> 00:05:10,310
local LEA hosted API at the path of V-1 account register.

67
00:05:10,390 --> 00:05:10,850
OK.

68
00:05:11,020 --> 00:05:13,920
And so if I say send.

69
00:05:14,330 --> 00:05:15,300
It's going to send it to it.

70
00:05:15,320 --> 00:05:20,660
And you can see here it says that we successfully created a new account.

71
00:05:20,660 --> 00:05:26,130
And if we open up one of our other tools that we downloaded in the last couple of videos robo Mungo

72
00:05:26,650 --> 00:05:32,270
if we come over here and we look at our chat happy I am and we come down here to accounts and double

73
00:05:32,270 --> 00:05:35,320
click if I scroll down here to the bottom.

74
00:05:35,630 --> 00:05:40,400
There we go we can actually see that we just registered user.

75
00:05:40,400 --> 00:05:44,760
All right so Jay at test register dot com.

76
00:05:44,810 --> 00:05:51,320
So I'm I'm showing you all this in postman so that you can get an idea of what kind of stuff we're going

77
00:05:51,320 --> 00:05:58,050
to be passing back and forth to the API and what the kind of responses are in the next videos that we'll

78
00:05:58,050 --> 00:06:00,580
be looking at the off and create user.

79
00:06:00,830 --> 00:06:04,550
But for now we're just going to stick with this registry user so we're know that we're going to be needing

80
00:06:04,550 --> 00:06:11,160
to send something to a an API with it with a Ural and a post.

81
00:06:11,180 --> 00:06:17,240
We know that we're going to need to be sending somebody and we know that we're going to need a simple

82
00:06:17,240 --> 00:06:18,850
header in the type of application.

83
00:06:18,880 --> 00:06:24,190
Jason So let's jump into X code and get started on that.

84
00:06:24,230 --> 00:06:30,140
So first thing I think we need to create some a service so all this stuff is going to be handled inside

85
00:06:30,140 --> 00:06:32,330
of a file called Office service.

86
00:06:32,330 --> 00:06:40,250
So let's create a new group that will click right click on SMAC here and say new group and that new

87
00:06:40,250 --> 00:06:42,050
group is going to be called

88
00:06:44,720 --> 00:06:46,150
services.

89
00:06:46,320 --> 00:06:46,810
OK.

90
00:06:47,150 --> 00:06:54,500
We're going to create a new file here inside of it and this can just be a swift file.

91
00:06:54,500 --> 00:06:54,810
All right.

92
00:06:54,810 --> 00:07:00,560
Call it off service ok and create.

93
00:07:01,400 --> 00:07:10,730
OK so this service is going to handle all of our log in create user register user functions as well

94
00:07:10,730 --> 00:07:15,250
as some some other variables that we're going to want to code up.

95
00:07:15,290 --> 00:07:15,800
OK.

96
00:07:16,070 --> 00:07:17,360
So let's go ahead and create it.

97
00:07:17,370 --> 00:07:21,500
So we're going to take class off service.

98
00:07:22,550 --> 00:07:22,930
OK.

99
00:07:22,940 --> 00:07:24,830
This is going to be a singleton.

100
00:07:25,040 --> 00:07:31,530
Meaning that this is going to be accessible globally and there can only be one instance of it at a times

101
00:07:31,630 --> 00:07:40,670
when say static let instance cool off service and we're just going to initialize that and then we're

102
00:07:40,670 --> 00:07:49,040
going to create a few variables here that we are going to store in the user defaults meaning that those

103
00:07:49,040 --> 00:07:53,550
are going to be persisted even after the user closes the app.

104
00:07:53,570 --> 00:07:58,460
And these are going to be things like whether they whether the user has logged in so that we don't have

105
00:07:58,460 --> 00:08:02,630
to enter in the username and password every single time we open the app.

106
00:08:02,630 --> 00:08:08,090
The other thing is going to be the office token and the last thing is the user e-mail so that when we

107
00:08:08,330 --> 00:08:12,040
reopen the app we can get all of the info for for the user.

108
00:08:12,110 --> 00:08:12,690
OK.

109
00:08:13,010 --> 00:08:14,330
So go ahead and create those.

110
00:08:14,330 --> 00:08:22,850
Like I said we're going to be using the user defaults and we say let defaults equal user defaults dot

111
00:08:23,240 --> 00:08:25,120
standard game.

112
00:08:25,130 --> 00:08:30,560
So for click on here on use default it says an interface to the user's default database where you store

113
00:08:30,560 --> 00:08:35,900
key value pairs persistently across invocations of your app on a given device.

114
00:08:35,900 --> 00:08:41,870
So basically this is the most simple way of saving data and your app and you don't want to use it for

115
00:08:41,870 --> 00:08:47,570
things like images and any like heavy duty data.

116
00:08:47,570 --> 00:08:48,000
All right.

117
00:08:48,110 --> 00:08:53,900
So just use the user defaults for simple things like strings and booleans and that kind of stuff for

118
00:08:53,900 --> 00:08:54,470
it.

119
00:08:54,620 --> 00:08:58,070
It's also not super secure so you don't want to store passwords and stuff.

120
00:08:58,470 --> 00:09:05,730
So it's just out we're going to say a VAR is logged in and this is going to be a big deal.

121
00:09:06,710 --> 00:09:12,860
And like I said this is just a bully and that will help us to be able to do checks on how we want the

122
00:09:12,860 --> 00:09:13,700
UI to display.

123
00:09:13,700 --> 00:09:20,060
So for instance if we're logged in then we're going to show in the menu that logon button is actually

124
00:09:20,060 --> 00:09:21,280
going to show the users name.

125
00:09:21,290 --> 00:09:23,580
But if we're not logged in then it's going to say log.

126
00:09:23,650 --> 00:09:24,150
OK.

127
00:09:24,560 --> 00:09:29,330
And we're to create some getters and setters for this so we're going to get.

128
00:09:29,960 --> 00:09:35,750
And so what we want to do is when we when we try to access this we're going to look into the user defaults

129
00:09:35,780 --> 00:09:39,470
and see if see if this value exists.

130
00:09:39,470 --> 00:09:52,460
So I say return defaults dot bhool for key and the key is going to be a constant that we haven't created

131
00:09:52,460 --> 00:10:00,020
yet but it's going to be called whip's logged in key.

132
00:10:00,290 --> 00:10:03,330
OK so let's go and I was going to create that right now.

133
00:10:03,500 --> 00:10:11,010
So let's jump into Constans and create a new header here called user defaults.

134
00:10:11,210 --> 00:10:16,760
And we're going to have we're going to have three use of default constants here so we're going to add

135
00:10:16,760 --> 00:10:18,120
all of them right now.

136
00:10:18,140 --> 00:10:32,410
First we're going to say let tokin underscore key equal tokin we're going to have the logged in key

137
00:10:33,050 --> 00:10:37,170
and that's just going to be logged in.

138
00:10:37,850 --> 00:10:46,670
And lastly we're going to have user e-mail and that's going to be equal to user e-mail.

139
00:10:46,770 --> 00:10:50,450
I was going to say that and jump back into service.

140
00:10:50,690 --> 00:10:57,500
OK so now when we try to access all service ADOT instance that is locked in we're going to look into

141
00:10:57,500 --> 00:11:00,690
our user defaults and see if that exists.

142
00:11:00,770 --> 00:11:01,360
Okay.

143
00:11:01,670 --> 00:11:07,040
And then if we want to set it we need a set or services set and we are going to say

144
00:11:12,820 --> 00:11:22,780
defaults got set and the one that we want is that value for key in case we're going to see new value

145
00:11:23,020 --> 00:11:27,130
for key and that same key logged in key.

146
00:11:27,580 --> 00:11:29,250
Perfect.

147
00:11:29,260 --> 00:11:32,770
So it's going to create a little bit of space here.

148
00:11:33,340 --> 00:11:40,110
All right so the next one is for the tokens would say bar off token and this is going to be a string

149
00:11:41,380 --> 00:11:45,080
and we're going to create a couple of getters and setters again from the they get.

150
00:11:45,400 --> 00:11:57,750
And this is going to be return defaults to value for key string and Kim and so the key for the token

151
00:11:57,750 --> 00:12:00,340
is token key.

152
00:12:00,690 --> 00:12:06,940
And then we have to cast it as a string because you will see here if we click right here on the first

153
00:12:07,110 --> 00:12:10,580
value this returns an optional any.

154
00:12:10,670 --> 00:12:13,720
OK so we got to cast it as a string.

155
00:12:14,190 --> 00:12:21,120
And when we said it we are going to say defaults that set.

156
00:12:21,330 --> 00:12:29,010
And what we want here is the value and the value is new value meaning whatever it is that we set this

157
00:12:29,040 --> 00:12:31,780
equal to when we're calling it up and I need to fix this.

158
00:12:31,820 --> 00:12:33,690
That should be a token.

159
00:12:33,870 --> 00:12:37,710
And the key is again token key.

160
00:12:37,800 --> 00:12:45,790
And lastly we have our user emails and we save our user e-mail and this is also a string.

161
00:12:46,290 --> 00:12:50,730
And you know I can actually just copy these getters and setters because this is going to be the same

162
00:12:50,730 --> 00:12:56,210
thing except for the key which is user email.

163
00:12:56,400 --> 00:12:57,060
Perfect.

164
00:12:57,060 --> 00:13:03,160
So now we have just created these three variables that we'll be able to access anywhere.

165
00:13:03,210 --> 00:13:09,330
And also be reading and writing to and from our user defaults so these these these variables will be

166
00:13:09,330 --> 00:13:11,910
persisted across.

167
00:13:12,480 --> 00:13:13,170
App launches.

168
00:13:13,170 --> 00:13:13,540
All right.

169
00:13:13,620 --> 00:13:14,670
So that's perfect.

170
00:13:17,040 --> 00:13:21,770
All right so now we are actually ready to create our registered user function.

171
00:13:21,780 --> 00:13:23,540
So how do we do that in X code.

172
00:13:23,610 --> 00:13:29,790
You know here in Postman it seems pretty simple all we got to do is press the send button and it does

173
00:13:29,790 --> 00:13:30,700
it for us.

174
00:13:30,870 --> 00:13:37,530
But we got a code that all out here in X code ourselves so we're going to got to do is create an actual

175
00:13:38,880 --> 00:13:47,010
web request specify the headers specify the body specify how we want to see that response come back

176
00:13:47,040 --> 00:13:47,780
and everything.

177
00:13:48,000 --> 00:13:52,550
And what we're going to do is we're going to use something a library called Allum of fire.

178
00:13:52,740 --> 00:13:58,920
And what this does what Alma fire does is it's a it's a library built on top of Apple's you r l session

179
00:14:00,520 --> 00:14:04,190
framework that makes making web requests a lot easier.

180
00:14:04,230 --> 00:14:11,360
OK so here we're going to import Alamo fire CBSA import Alamo fire.

181
00:14:11,510 --> 00:14:14,520
It's super easy to use.

182
00:14:14,610 --> 00:14:23,880
Let me open up Safari and show you where to find some more documentation if you just Google Alamo fire

183
00:14:24,330 --> 00:14:28,400
and come up here to the get hub Alamo fire page.

184
00:14:30,400 --> 00:14:33,850
Then you can see a lot of great information.

185
00:14:33,940 --> 00:14:43,090
Alamo fire is one of the best documented and maintain libraries out there so highly highly recommended.

186
00:14:43,240 --> 00:14:43,930
See here.

187
00:14:44,110 --> 00:14:47,050
So here's where usage making a request.

188
00:14:47,050 --> 00:14:47,890
So it's super simple.

189
00:14:47,890 --> 00:14:51,070
All you have to do is say you call Alamo fire.

190
00:14:51,070 --> 00:14:56,640
Then you say Doc request and then you pass in the your L that we're trying to hit.

191
00:14:56,650 --> 00:15:03,520
So in our case it would be something like this local host account register and then you specify how

192
00:15:03,520 --> 00:15:06,010
you want the response to be.

193
00:15:06,010 --> 00:15:12,790
Most the time we want it to be a song but in the case of the register function what we actually want

194
00:15:13,430 --> 00:15:16,830
what we actually want is response string.

195
00:15:17,100 --> 00:15:22,180
Yes so the response string handler uses the response string serialized to convert the data returned

196
00:15:22,180 --> 00:15:23,740
by the server into a string.

197
00:15:23,860 --> 00:15:24,270
OK.

198
00:15:24,460 --> 00:15:30,460
So the register user api function is just a little bit unique in that way and that the response that

199
00:15:30,460 --> 00:15:32,760
we get back is actually a string.

200
00:15:33,370 --> 00:15:41,380
So it's pretty simple so let's go ahead and create a function to do that register and say func register

201
00:15:42,100 --> 00:15:43,250
user.

202
00:15:43,450 --> 00:15:50,020
And I remember from our postman some of the things that we're going to need to be able to pass to the

203
00:15:50,020 --> 00:15:50,540
API.

204
00:15:50,540 --> 00:15:52,510
It an e-mail and a password.

205
00:15:52,710 --> 00:15:53,230
OK.

206
00:15:53,470 --> 00:16:01,910
So we're going to say e-mail which is a type of string and pass word which is also a string.

207
00:16:02,400 --> 00:16:02,990
OK.

208
00:16:03,320 --> 00:16:10,330
But we need one more thing here because web requests are asynchronous meaning that we send off the web

209
00:16:10,330 --> 00:16:15,850
request and then we don't know when the response is going to come back do we.

210
00:16:15,850 --> 00:16:22,150
And so as a result we can't say register user and then immediately try to do something with what we

211
00:16:22,150 --> 00:16:23,470
expect to come back.

212
00:16:23,710 --> 00:16:31,090
OK so what we need to do is have a way of knowing once it's finished and we do this with something called

213
00:16:31,090 --> 00:16:32,850
the completion handler.

214
00:16:33,730 --> 00:16:38,770
So if we come back over here into Constans we're going to create a new constant.

215
00:16:38,890 --> 00:16:47,880
And this is going to be called type alias and the name is completion handler and the type is just going

216
00:16:47,890 --> 00:16:49,590
to write this and then we'll talk about it.

217
00:16:49,900 --> 00:16:59,840
OK so those underscore success bool and then dash and arrow and then open close parentheses.

218
00:16:59,880 --> 00:17:03,140
I said Don't freak out if you don't understand what's going on right here.

219
00:17:03,210 --> 00:17:05,410
And let's go through it a little bit at a time.

220
00:17:05,620 --> 00:17:12,330
So first off what does a type Illyas type alias is simply renaming a type.

221
00:17:12,670 --> 00:17:20,450
I can do this I could say type alias and I'm going to say Johnny and type expression is string.

222
00:17:20,650 --> 00:17:22,010
Then I could say something like.

223
00:17:22,070 --> 00:17:29,740
Let name of type Jhonny equal.

224
00:17:30,340 --> 00:17:38,620
And so you'll see here what we've done is simply remapped the type of string to this type called Johnny.

225
00:17:38,650 --> 00:17:45,280
So that's what the type alias's all we're doing is saying completion handler is of this type.

226
00:17:45,490 --> 00:17:46,660
And what does this type.

227
00:17:46,840 --> 00:17:49,100
Well this is a closure.

228
00:17:49,120 --> 00:17:53,140
OK we're creating a very simple custom closure.

229
00:17:53,230 --> 00:18:00,170
And what a closure is is it's a first class function that can be passed around in code.

230
00:18:00,580 --> 00:18:06,750
So basically how we're going to be using it is we're going to send off a web request.

231
00:18:06,850 --> 00:18:07,370
OK.

232
00:18:07,780 --> 00:18:16,260
And then once that web request is done we are going to say completed and pass into that closure.

233
00:18:16,270 --> 00:18:17,340
True or false.

234
00:18:17,560 --> 00:18:25,070
And then we can do a check on that later on to see whether the request was successful or not.

235
00:18:25,310 --> 00:18:25,760
OK.

236
00:18:25,870 --> 00:18:27,780
So it's a little bit complicated.

237
00:18:28,180 --> 00:18:31,830
And so this is a very simple example of a closure called the completion handler.

238
00:18:31,990 --> 00:18:38,530
A little bit later on we're going to do a little bit more complex work with closures and I'm hoping

239
00:18:38,530 --> 00:18:45,490
that at that point you'll have used the simple one a little bit more and that once we get into the other

240
00:18:45,790 --> 00:18:48,170
uses of it it will click into place a little bit better.

241
00:18:48,200 --> 00:18:48,560
Right.

242
00:18:48,700 --> 00:18:54,850
So don't worry if you don't fully understand this completion handler right now it'll it'll become more

243
00:18:54,850 --> 00:18:55,950
clear as we use it.

244
00:18:55,960 --> 00:18:56,740
And as we go on.

245
00:18:56,740 --> 00:18:57,330
OK.

246
00:18:57,670 --> 00:19:00,150
So here we go we got a completion Hendler here.

247
00:19:00,370 --> 00:19:05,640
And so jump back to our office service start swift and right here at the end.

248
00:19:05,830 --> 00:19:08,620
We're going to say completion

249
00:19:11,730 --> 00:19:16,990
at this being which is just the key word that we need for our completion.

250
00:19:17,190 --> 00:19:25,340
And then we're going to say completion handler and you'll see here that is passing in a bull and returning

251
00:19:25,980 --> 00:19:26,450
nothing.

252
00:19:26,520 --> 00:19:27,300
OK.

253
00:19:27,600 --> 00:19:31,290
So first thing we're going to do is we're going to set the e-mail to be all lowercase.

254
00:19:31,290 --> 00:19:33,880
Just to make things easier.

255
00:19:33,930 --> 00:19:41,500
When I say lower case e-mail you go to the email that is passed in hand just say lowercase.

256
00:19:41,770 --> 00:19:45,290
OK then we're going to need a header.

257
00:19:45,390 --> 00:19:45,870
All right.

258
00:19:46,050 --> 00:19:51,930
So remember back in less than six we talked about web requests with headers and headers are kind of

259
00:19:51,930 --> 00:19:57,960
like kind of like an attachment to the HTP request where you can specify the type of content you can

260
00:19:57,960 --> 00:20:02,000
add to authorization headers API keys and other things like that.

261
00:20:02,060 --> 00:20:02,600
OK.

262
00:20:02,790 --> 00:20:09,610
And so what that looks like here in X code is we're going to create a jigsaw Jason object.

263
00:20:09,690 --> 00:20:22,260
OK and we do that by saying Let header equal square bracket and then we put in a key and a value in

264
00:20:22,290 --> 00:20:29,190
the type of strings during the same head or ball and content type

265
00:20:31,900 --> 00:20:47,660
Colin Lissa's application slash Jason and character set is equal to T F dash 8.

266
00:20:47,990 --> 00:20:48,660
OK.

267
00:20:48,870 --> 00:20:53,290
And you build this if this these errors go away.

268
00:20:53,760 --> 00:20:55,010
OK so looks good.

269
00:20:55,170 --> 00:21:01,410
And you might remember this from our postman if we come up here and look at these hetter you see something

270
00:21:01,410 --> 00:21:02,000
similar here.

271
00:21:02,010 --> 00:21:02,670
Content-Type.

272
00:21:02,670 --> 00:21:03,990
Application J song.

273
00:21:04,230 --> 00:21:08,870
And so we're just kind of replicating the what we have here in postman in here.

274
00:21:08,870 --> 00:21:15,090
Here in ex-coach crafting out our very own web request from X code.

275
00:21:15,150 --> 00:21:16,470
All right so let's keep going.

276
00:21:16,470 --> 00:21:20,680
The next thing that we're going to need is a body in case of that body.

277
00:21:21,090 --> 00:21:27,070
And the same thing we're going to create a J on object and we're going to say this one is of type string

278
00:21:27,070 --> 00:21:35,430
with kand and the light is equal to again opening close square brackets.

279
00:21:35,430 --> 00:21:37,490
I'm going to say are two key value pairs.

280
00:21:37,500 --> 00:21:39,900
If we look in postman What do we need.

281
00:21:39,920 --> 00:21:41,300
We look at our body here.

282
00:21:41,430 --> 00:21:44,920
We're going to need an email and a string and a password and a string.

283
00:21:44,940 --> 00:21:45,390
OK.

284
00:21:45,600 --> 00:21:46,770
So let's go ahead and do that.

285
00:21:46,770 --> 00:21:55,890
So this is going to be e-mail and the email is lowercase e-mail and then you do a comma to denote that

286
00:21:55,890 --> 00:21:58,530
we have multiple key value pairs.

287
00:21:58,770 --> 00:22:01,970
And the next one is going to be password.

288
00:22:02,530 --> 00:22:03,600
OK.

289
00:22:04,080 --> 00:22:07,710
And that is going to be the password that we pass in.

290
00:22:07,760 --> 00:22:07,980
All right.

291
00:22:07,980 --> 00:22:14,880
And it's extremely important that what you have here and here matches up exactly with what you have

292
00:22:14,880 --> 00:22:15,780
here and here.

293
00:22:15,870 --> 00:22:16,380
OK.

294
00:22:16,650 --> 00:22:21,030
So now we have defined what our header is what our body is.

295
00:22:21,060 --> 00:22:25,180
And so now we're actually ready to create the web request.

296
00:22:25,410 --> 00:22:25,700
OK.

297
00:22:25,740 --> 00:22:34,010
And so like we were looking in on their hub page all we got to do is say Hello Mo fire dot request.

298
00:22:34,080 --> 00:22:40,890
So we create the request and what we want is this one right here because we're going to be doing a little

299
00:22:40,890 --> 00:22:44,760
bit more complicated stuff than just simply making a web request.

300
00:22:44,750 --> 00:22:52,470
We need to be able to send a web request with a specific type of method whether it's get post you know

301
00:22:52,470 --> 00:22:53,070
et cetera.

302
00:22:53,280 --> 00:22:59,970
We need to be able to pass in parameters which is our body here and we need to be able to specify the

303
00:23:00,150 --> 00:23:06,050
type of encoding and we need to be able to say Pass headers as well.

304
00:23:06,210 --> 00:23:06,760
All right.

305
00:23:06,930 --> 00:23:13,320
So that might seem a little bit daunting at first but I promise as we keep using this you're going to

306
00:23:13,320 --> 00:23:14,760
get the hang of it real quick.

307
00:23:15,110 --> 00:23:15,290
OK.

308
00:23:15,300 --> 00:23:21,690
So we see here that we need it you are well and we haven't actually defined that yet so let's come over

309
00:23:21,690 --> 00:23:28,790
here to Constance as well as open up Safari and just run over to our Heroku page.

310
00:23:28,800 --> 00:23:29,420
OK.

311
00:23:29,650 --> 00:23:34,420
All right so here in her Roku go ahead and click on your chat to be app.

312
00:23:34,500 --> 00:23:40,860
Come over here to settings and we are going to grab this.

313
00:23:41,100 --> 00:23:48,650
You are right here and you can click on it if you want and you'll see that wait for it to come back

314
00:23:48,650 --> 00:23:50,660
on a line.

315
00:23:50,720 --> 00:23:51,580
All right there we go.

316
00:23:51,590 --> 00:23:53,430
This is Chaddy P.I. is alive.

317
00:23:53,480 --> 00:23:53,750
All right.

318
00:23:53,760 --> 00:23:55,070
So good.

319
00:23:55,100 --> 00:24:02,630
So if we come over here to a postman what you see here is the local host 2005 for our online Heroku

320
00:24:02,630 --> 00:24:06,600
version that is this guy right here.

321
00:24:06,860 --> 00:24:14,060
OK so jump back to X code with that you are Ehle copy and we are going to set up our Ural Constance

322
00:24:19,110 --> 00:24:23,030
and the first one is our base Yoro because it's a base.

323
00:24:23,040 --> 00:24:31,410
You are all equal to piano that you are all that we just copied plus the one at the end for version

324
00:24:31,410 --> 00:24:32,760
1.

325
00:24:32,820 --> 00:24:40,300
Then if we look in Postman the what comes after that his account slash register.

326
00:24:40,450 --> 00:24:40,730
OK.

327
00:24:40,740 --> 00:24:53,190
So are you are all or register is going to be equal to base hopes and need to do the string interpolation

328
00:24:53,190 --> 00:24:53,830
here.

329
00:24:54,090 --> 00:25:02,270
So that's going to be bass you are l Plus account slash register tacked onto the end.

330
00:25:02,360 --> 00:25:09,410
So when I say that and jam packed into our office service here and we're going to pass in the U r l

331
00:25:09,420 --> 00:25:14,800
n that u r l he is u r l underscore register.

332
00:25:14,970 --> 00:25:22,410
The method is of course Dr. Post and which you can again see here in post-Mandela post.

333
00:25:22,620 --> 00:25:27,170
The parameters is the body soon to say body.

334
00:25:27,180 --> 00:25:30,970
The encoding is just on.

335
00:25:31,180 --> 00:25:39,600
Let's see here Jewson encoding the default and the headers is of course our header that we created right

336
00:25:39,600 --> 00:25:40,430
above.

337
00:25:40,670 --> 00:25:42,630
And then we're going to say Dot.

338
00:25:42,720 --> 00:25:45,850
And this is where we specify our response.

339
00:25:46,020 --> 00:25:51,030
And you see that there's a whole bunch you can do response data where the response just comes as data

340
00:25:51,360 --> 00:25:57,030
as the sun for this particular case we're going to do a response string.

341
00:25:57,030 --> 00:25:59,200
Most of the time you'll be working with response Jason.

342
00:25:59,220 --> 00:26:05,180
But in this particular case for the register API call it a response string.

343
00:26:05,250 --> 00:26:05,620
Ok.

344
00:26:05,780 --> 00:26:11,700
Press that and then press return again so that we get the completion handler.

345
00:26:11,700 --> 00:26:17,370
And when I say here with this selected here I'm going to say response so I'm just renaming what we get

346
00:26:17,370 --> 00:26:22,330
past to use as response and hearing code.

347
00:26:22,440 --> 00:26:30,230
We're just going to say if response does result that error is equal to nil.

348
00:26:30,240 --> 00:26:34,380
So if everything went hunky dory then we're going to see a completion.

349
00:26:34,410 --> 00:26:35,790
Tis true.

350
00:26:35,910 --> 00:26:43,110
All right and so this completion is right here the custom completion handler that we that we coded so

351
00:26:43,110 --> 00:26:48,680
that we'll know when the web request is completed and when it's successful.

352
00:26:48,840 --> 00:26:58,680
So if everything didn't go well else then we're going to say completion is false and we're going to

353
00:26:58,680 --> 00:26:59,400
go ahead and do it.

354
00:26:59,400 --> 00:27:02,210
Debug print to see see what went wrong.

355
00:27:02,340 --> 00:27:03,560
Debug print.

356
00:27:03,670 --> 00:27:04,460
And we're going to.

357
00:27:04,470 --> 00:27:10,360
Response result that error as any.

358
00:27:10,890 --> 00:27:13,820
So that's just going to print out what went wrong for us.

359
00:27:13,830 --> 00:27:19,500
I just going to save that and you know we are running really long on this whole mess and so I'm afraid

360
00:27:19,500 --> 00:27:22,460
I'm going to have to cut it short right here.

361
00:27:22,740 --> 00:27:28,950
But so far in this lesson what we've done is we've created our three office service variable's is logged

362
00:27:28,950 --> 00:27:30,740
in off token user e-mail.

363
00:27:30,810 --> 00:27:36,450
And then we have created our register user function and the next lesson and we're going to actually

364
00:27:36,450 --> 00:27:36,980
use it.

365
00:27:37,020 --> 00:27:37,530
OK.

366
00:27:37,680 --> 00:27:41,930
So just to finish off real quick I'm going to go ahead and commit our changes.

367
00:27:42,210 --> 00:27:51,630
So I'm back here at PWT to make sure I'm in the right directory get status and then get add that get

368
00:27:51,810 --> 00:27:53,370
commit dash.

369
00:27:53,580 --> 00:27:58,000
And this is lesson nine.

370
00:27:58,530 --> 00:28:03,080
All right guys I will see you in the next one when we are going to work more with the API.
