10.8. Fetching Data with AlamoFire: 'details' endpoint

http://apis.sakibnm.space:8888/contacts/text/details?name=Mark W

Let's get the details of a selected user when the user taps on a cell on the table view.

We already know that when the user taps on a cell of the table view, we can handle the interaction in the adopted method regarding didSelectRowAt.

So let's write this line of code getContactDetails(name: self.contactNames[indexPath.row]) inside didSelectRowAt.

extension ViewController: UITableViewDelegate, UITableViewDataSource{
    //codes omitted...
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        getContactDetails(name: self.contactNames[indexPath.row])
    }
}

Here we are saying if the user selects a row, we will call the getContactDetails(name:) with the contact name getting displayed on that cell.

Let's define the getContactDetails() method:

//MARK: get details of a contact...
    func getContactDetails(name: String){
        let parameters = ["name":name]
        if let url = URL(string: APIConfigs.baseURL+"details"){
            AF.request(url, method:.get,
                       parameters: ["name":name],
                       encoding: URLEncoding.queryString)
                .responseString(completionHandler: { response in
                
                //MARK: retrieving the status code...
                let status = response.response?.statusCode
                
                switch response.result{
                case .success(let data):
                    //MARK: there was no network error...
                    
                    //MARK: status code is Optional, so unwrapping it...
                    if let uwStatusCode = status{
                        switch uwStatusCode{
                            case 200...299:
                            //MARK: the request was valid 200-level...
                                //MARK: show alert with details...
                                self.showDetailsInAlert(data: data)
                                break
                    
                            case 400...499:
                            //MARK: the request was not valid 400-level...
                                print(data)
                                break
                    
                            default:
                            //MARK: probably a 500-level error...
                                print(data)
                                break
                    
                        }
                    }
                    break
                    
                case .failure(let error):
                    //MARK: there was a network error...
                    print(error)
                    break
                }
            })
        }
    }

Here in the above code:

  • We set the method of the request to GET.

  • If you remember from the Postman test, we added the name parameter with the API call for the details API endpoint. Here, we are doing it using the parameters attribute to the Alamofire request (at line 6).

  • The response closure for this case is pretty straightforward too. If there is no network error and the status code is a 200-level code, we call the method showDetailsInAlert(data: data) to show an alert with the received data (line 23).

Now let's write the code to display the alert in method showDetailsInAlert():

In the above code, we split the data string into multiple parts to separate the name, email, and phone. Then we remove the whitespaces from each part's beginning and end, and display the alert using them.

Let's run the app now.

So, we are done writing codes for three API calls. Can you do the 'delete'?

Last updated

Was this helpful?