![]() Note that it’s necessary to use a try because an exception can be thrown when running this function (this is why we are using a do/catch statement). In the line below, the return tuple of the () function is created. The URLSession, with all the information necessary to carry out the request, will return the API response. In other words, it’s the class that abstracts all communication between the app and the API. “The URLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs.” ![]() But wait a minute, what is URLSession? Again, let’s turn to Apple’s documentation: We’re close! For that, we will use the with our URLRequest that we just created. Now all you need to do is make the request. The httpBody is also configured following the same logic, in case there is any data to be sent to the endpoint. After that, the httpMethod and allHTTPHeaderFields are configured through, respectively, the method and header variables of the Endpoint passed by parameter. So, what the code does next is create an URLRequest from the instantiated URL. In addition, for HTTP and HTTPS requests, URLRequest includes the HTTP method ( GET, POST, and so on) and the HTTP headers.” “ URLRequest encapsulates two essential properties of a load request: the URL to load and the policies used to load it. But wait, what is this URLRequest? Apple documentation will help us: invalidURL if the guard let fails.Īfter that, we need to create a URLRequest for the endpoint consumption. Do you remember forming the URL through baseURL + path? Well, there it is! This is also where we see the first use of RequestError, issuing an. The method starts by creating the URL we’re going to use. This is where all this information will be used. As a parameter, it receives the Endpoint and the type of the model to decode the API response.Īs a return, we’ll have a Result that can be a success with the decoded response or an error with one of the cases created in RequestError.Īnd as you can see, it is accompanied by the keyword async, which represent that it is an asynchronous function that will run on a separate thread and can return the Result at any time.Īs previously stated, the Endpoint protocol contains all the information needed to consume it. HTTPClient is a protocol with a default implementation of the sendRequest function, responsible for making the request to the server. For this API, only the top five methods are included, but you can add more if it makes sense in your context. The type of the method variable is RequestMethod and represents the HTTP methods of the endpoint, which can be: GET, POST, PUT, DELETE, PATCH, HEAD, etc. Like the header variable, when necessary, the body is also a dictionary that must contain the body information that the endpoint documentation asks to be sent. This is usually where authentication is performed through Authorization, which you will see below. The header variable should return a dictionary with all the header information that the endpoint documentation asks for, when needed. For instance: suppose the endpoint URL is, then the path should be movie/top_rated. The path variable will be used as a complement to the baseURL to form the endpoint URL. If necessary, you can override this implementation. The Endpoint protocol has a default implementation of baseURL as it is normally just one for all endpoints therefore, it is not necessary to implement it all the time. At the bottom of the page, you will find your token in the API Read Access Token (v4 auth) section. In order to make requests, you’ll need an access token, which you can get by registering on TMDB’s page after following the steps below:ĭone. Using the API is free, but that doesn’t mean it’s completely open. Briefly, TMDB is a database that contains information on movies, TV shows, series, etc. To help us in this article, we will use TMDB’s API to make some requests, which you can see in the documentation here. In addition, it’s also important to have a good understanding of Protocol Oriented Programming (POP) and the async/await structure concurrency. If you haven’t mastered the API concept yet, I strongly recommend you read this article before continuing. This article will show you how to make a simple and generic network layer for your iOS application without having to write a lot of code every time you make a request, avoiding boilerplate. It’s in this interaction that the app comes to life. ![]() Receiving a response from the server is one of the coolest things when we are learning how to develop a frontend application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |