Everyone welcome back Johnny be here with slopes dot com and the time is now we are finally going to
actually do something chant related we are going to pull down the chat channels in this.
Or at least we're going to create the function that does that.
I said let's go ahead and get started.
Well we're going to do is we're going to create a new service called our message service and this is
the service that's going to be responsible for storing our messages and channels and also the functions
that will retrieve messages and channels as well as some other utilities that we'll be needing for it.
All right.
So let's get started.
I'm going to come over here to services right click and say new file is going to be a swift file and
say Next we're going to call this message service.
I'm going to create that and then just like our other services this is going to be a class called Message
Service like our other services.
This is also going to be a singleton.
So when I say static instance fool cool and message service it's Tenchi that.
And so what do we do we're gonna put it here.
Well we're going to have a whole bunch of channels stored in here as well as the messages for whichever
channel is selected.
So first we're going to need a couple of models right.
So let's go ahead and create those.
I just want to say new file.
This is a swift file.
And I say next and this is going to be a model called Channel.
And I'm going to create it.
And let's go ahead and take a peek at postman and see what kind of stuff is returned from a channel.
So this is the function or the API endpoint find all channels is add to the one slash channel and that
just returns all of the all of the channels that we get it's a GET request and so we can see here we
have an array of objects of Chase objects.
So we have one channel here and another channel here.
The first one is called general and it doesn't have a description.
The second channel is called the scription are inside the second channel is called random and its description
is This is the random channel.
OK.
So anyways so these are the properties of a channel that were going to be working with.
We're going to want to have the ID name and description.
All right.
So it's create a model on that.
And this is going to be a struct Superstruct it's going to be called Channel.
And like I said the properties that we want can be public private set VAR channel type toll and that's
going to be the type of string we're going to have public private set ups
are called Channel this corruption that's also going to be a string.
And Leslie just going to copy this.
So don't write that out copy and paste.
And this is going to be ID.
All right.
So a very simple model for us.
So that'll be nice and save that.
And then we can jump back into our message service and say our channels is equal to an array of channel
objects to instantiate that right there.
And yes now we have a place to store our channels.
Now we need a function to actually retrieve those.
All right and so this is the the postman API call and we are.
So this is what we get when we return it.
What else do we need we need an authorization HEADER AND WE NEED OUR you or else so it's just the base
you are L plus slash channel right.
So let's go ahead and create our you are l for that.
I'm going to come over to utility's here.
Go to Constans and let's create our New You are going to say let you all on that score get underscore
channels equal to the string interpolation of the base you are l and then just the channel and that'll
do it.
I just want to say that.
Copy the euro constant and jump back into our message service.
All right so now we are ready to create our function that is going to have our web request and go hit
our API and bring back all of our channels for us.
Right.
So going to say phunk find all channel can and this is just a get request so we don't have to actually
pass anything in the body or anything so we don't need anything to Pessin.
So all we need is our completion handler would say at this date being completion handler.
Okay.
So now it's going create our Alamo fire request to say Alamo fire DOD request and we need to actually
import those of course from the same port Alamo fire and import swiftie.
On there you go.
So we got it.
Alamo fire request.
And there we go.
And the you are all that we want to hit is your no underscore get channels.
The method is to get the parameters are nil.
The encoding is of type.
Jason do.
What is it.
G encoding that default and the headers is we do need a bearer headers.
Right.
Barer header.
And the response that we want we want it to be in the form of G songs so response Dusan and press return
and then return again and then renaming the data response to response.
And then just deleting that placeholder text.
All right so we we pinged the API.
We've sent it the information that we want and now we're going to receive it back.
And so just like we've done in the past we're going to see if response that resolved that error is equal
to nil then we're going to do something with it or else we're going to say completion is false and we're
going to debug the print.
We're going to debug print.
The problem is we're going to say respon stopped resolved that error as in the case that I'll just print
out the response.
If we were successful then what are we going to do.
Well we're going to do like we've done in the past we're going to parse out the Jason.
All right so we're going to create some object out of the data.
So first off let's get the data a guard let data equal response result that I'm type just response that
data you go.
Else return.
So now we're going to turn that data into some sort of chase on but coming back here to postpone it
we see that this is actually an array.
So we want to turn our Jason into an array object.
So using Swiftie juice that's super easy we just say if let Jason is equal to Jason and then we're going
to say data are going to pass in the data and then we're gonna say adopt a.
Okay.
And so that gives us an array of adjacent objects.
And now we can say for item in chase on sort of we're going to loop through each item in the sun array
and we're going to say no we're going to parse out the actual individual properties of the channel game
games we're going to say let name equal.
And that's going to be item and drill into it.
Look for the key of name and cast that as a string value.
Then we're going to say let's channel description a cool item and we're going to search for the key
of description and that's going to be cast to a string value and I'm going to say let the equal item
and we're going to drill into it and search for the key of underscore ID string value.
And if you're curious where I'm getting these.
Again just from right here.
So this is the Gees on response.
And so these are the keys that we need to be looking for.
All right.
And then with those with these three variables we can now instantiate and create a new channel object.
OK.
So when I say let channel equal and then using the default struct initializer we can just say channel
title is equal to name channel description is equal to channel description and the ID is equal to the
ID.
OK so each time we loop through one of the items in the cell array we're going to extract the properties
that we need.
And then we're going to initialize a new channel object from the struct initializer.
And then we're going to add this new channel to our channels array that we that we store inside of our
message service we're in say self-taught channels that append and the new element is this new channel
here.
So that's how we loop through those objects and add them to our channels array right here and that's
it we can say completion is true right here.
Okay.
So that's that's how we've been doing some parsing up till now.
Right.
And I like doing it this way but in swift for there's a new way and it's really nice in some ways.
So I definitely want to show you that we're probably going to continue doing it this way.
You can make the choice yourself going forward which way you want to do it.
But the new way is they have new decoded and protocols and it's super nice.
So I'm going to show you how to do that.
So what we're going to do is I'm going to go ahead and just comment out right here.
And to make it work well we have to do is we have to make our channel conform to the decoded bull protocol.
OK.
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
have to make our vegans find a way to make this visible while we still see it we have to make our model
mirror exactly what we see in the japes our response.
OK so just can put this over here on my other window.
So these names have to be the same so.
And in the same order.
So this needs to be underscore ID.
This needs to be name.
This needs to be the scription
right description.
And then the thing the other thing that it really like is that you have to have everything.
So before were just ignoring this underscore underscore V.
I'm not really sure what it is some sort of mongered database identifier or something but because it's
in the sun.
We also have to have it here in our model.
So I'm going to go ahead and add that the C control copy and paste.
And so then what you can do is oops this should be underscore underscore the right.
So then what we can do is we can come back into message service and we can actually do the parsing and
practically one line.
And so that's what's really nice about it.
So we're going to we're going to do it say I do.
And the reason we have to have a do and catch block is because we're going to be using the sun decoder
which can throw an error.
So we're going to say self dobbed channels is equal to try.
And so when you have a do you need to try.
Jason decoder dot de-code and systemone we want and the type is.
So we're going to say the type is channel.
So we made it an array because we know that it's an array.
So channel that self.
And then you just pass in the data right here and that is all of the JS on parsing that you need it
kind of just takes your channel model and it just crams everything that comes from the juice on it decodes
it and stuffs it right into whatever variables you have in the message service or in your in your channel
model as long as it matches correctly and then like I said because this is a try.
It has the potential to throw an heiress.
We need to handle that.
So when I say here catch that error and then we're just going to debug.
Print that error.
OK.
Has any.
And.
Yes.
That is.
That's it.
That's all you got to do.
So let's go ahead and test this.
So here.
Under it.
I'm going to say print and then I'm going to say self channels.
I'm going to save it and let's run this Frum's.
Let's just put it in the chad b c you did load.
Ok so I want to say a message service instance you know what actually we're going to need to.
I think we have a logon user so I'm going to be super careful here.
Message Service instance.
Find all channel and say success and we're not really doing anything with it so I'm just going to run
it.
We've got an error here says expected to decode a string but found a number instead.
That's my bad.
Here in the channel model if we look at post-bin this property here is actually in a number so let's
instead cast that to an int.
And if you make it optional with this Dakotah will if it's not there then it just puts Nyland there.
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
a optional.
All right.
And here we go.
So we can see here.
Yeah we got we can see some of the properties that we would expect in the channel name general description.
Welcome to the water cooler.
Yes so it is it is printing out the channel information so yeah that parsing is not parsing is working
and so is kind of just up to you how you want to do it.
So like I said the drawbacks for me are that you have to have everything in the model and you have to
have the naming convention the same as what the sun on returns.
I mean this is super super nice being able to just do that one line of that one line of code instead
of having to parse through lots of different properties and stuff but still in the end I actually prefer
the old way just because I can ignore some things in the in the model.
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
naming convention and also being able to ignore properties in the sun that we just all really care about.
It's want to save that and then come back to our message service.
And here I'm going to go ahead and command Z all the way back as well.
Yeah like I said it just wanted to show you that because you know this is a course on swift for it is
new.
So you guys definitely deserve to learn about it.
Just personally I still prefer doing it this way and I feel like it just gives it gives me more control
over what my models look like.
And also you know I'm not being forced to use everything that they use on spits back at us.
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
as well so I'm going to print out the let's say self dot channels and then I'll pick the all access
the zeroth item and say Dot
channel channel title.
Let's do that.
All right and we're loaded and sure enough there we go.
General is the name of the first channel or so how are we doing on time.
Looks like we're out of time here but there you go.
We have created the function to hit our API and bring back to us all of the channels that are that are
currently on the API.
All right so in the next lessons we're actually going to do something with this data.
We're going to work in our table view inside of the channel of the sea and we're going to start make
it so that we can actually visualize the channel data there.
All right.
So that's about it for now.
I'm going to go ahead and finish off by adding the progress that we have made and committing it.
And I will see you in the next one.