Sobes.tech
Middle

What is your understanding of REST architecture and how have you applied it in your projects?

sobes.tech AI

Answer from AI

REST is not an architecture but a set of design principles for distributed systems. The main principles:

  • Client-Server: Separation of concerns. The client requests resources, and the server provides them.
  • Stateless: The server does not store information about the client's state between requests. Each request from the client contains all the information needed for processing.
  • Cacheable: Clients and intermediate nodes can cache server responses. The server should indicate whether responses are cacheable.
  • Layered System: The client does not necessarily interact directly with the final server; it can interact with intermediate layers (e.g., load balancers, proxies).
  • Code on Demand: The server can provide executable code to the client (e.g., JavaScript). This is rarely used in iOS development.
  • Uniform Interface: The most important principle. It defines the structure and format of interactions:
    • Resource Identification in Requests: Resources are identified by unique URIs.
    • Manipulation of Resources Through Representations: The client manipulates resources by sending their representations (e.g., JSON, XML) to the server.
    • Self-descriptive Messages: Each message contains enough information for processing.
    • Hypermedia as the Engine of Application State (HATEOAS): The server provides links to other available actions or resources in the response body. It allows the client to navigate between application states via hypermedia.

In iOS projects, I applied RESTful architecture using the following approaches:

  1. Working with API: Most interactions with the backend were carried out through RESTful APIs, providing access to data and functionality via HTTP methods (GET, POST, PUT, DELETE).
  2. Using frameworks: I actively used frameworks such as URLSession (native) or third-party ones like Alamofire for executing HTTP requests and handling responses.
    // Example of a request using URLSession
    let url = URL(string: "https://api.example.com/users/1")!
    let task = URLSession.shared.dataTask(with: url) { data, response, error in
        guard let data = data, error == nil else {
            print("Error: \(error?.localizedDescription ?? "Unknown error")")
            return
        }
    
        // Assuming the response is in JSON format
        if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
            print(json)
        }
    }
    task.resume()
    
  3. Data handling: JSON responses were parsed using JSONDecoder or other methods.
    // Example of decoding a JSON response
    struct User: Decodable {
        let id: Int
        let name: String
    }
    
    func parseUserData(data: Data) {
        let decoder = JSONDecoder()
        if let user = try? decoder.decode(User.self, from: data) {
            print("User ID: \(user.id), Name: \(user.name)")
        } else {
            print("Failed to decode JSON")
        }
    }
    
  4. Data modeling: Created Swift models representing resources received from the server.
  5. Architectural patterns: Integrated REST API interactions depending on the architectural pattern used (MVC, MVVM, MVP, VIPER). For example, in MVVM, network logic is often in the Services or Repositories layer.
  6. Caching: Used built-in URLSession caching mechanisms or implemented custom data caching logic at the application level to improve performance and offline functionality.
  7. Error handling: Implemented detailed error handling for API responses (e.g., 401 Unauthorized, 404 Not Found), informing the user or taking appropriate actions.

The principles of Uniform Interface and Statelessness were key in designing the interaction between the client-side iOS app and the backend, making the API predictable and scalable.