Monday, September 26, 2022
HomeiOS Developmentios - How to merge many API call into one in swift

ios – How to merge many API call into one in swift

[ad_1]

func getCar(session: String, CarHandler: @escaping (Result<CarResponseModel, Error>) -> Void) {
    let url = MAIN_URL + "/user/car"
    
    AF.request(url, method: .post, encoding: JSONEncoding.default, headers: ["Content-Type":"application/json; charset=utf-8", "Accept":"application/json", "session":session])
                .validate(statusCode: 200..<300)
                .validate(contentType: ["application/json"])
                .responseDecodable(of: CarResponseModel.self) { (response) in
                switch response.result {
                case .success(let response):
                    CarHandler(.success(response))
                case let .failure(error):
                    CarHandler(error.localizedDescription)
                }
            }
}

func getHouse(session: String, HouseHandler: @escaping (Result<HouseResponseModel, Error>) -> Void) {
    let url = MAIN_URL + "/user/house"
    
    let parameters = ["name": "houseName"]
    
    AF.request(url, method: .get, parameters: parameters, encoding: URLEncoding.queryString, headers: ["Content-Type":"application/json; charset=utf-8", "Accept":"application/json", "session":session])
                .validate(statusCode: 200..<300)
                .validate(contentType: ["application/json"])
                .responseDecodable(of: HouseResponseModel.self) { (response) in
                switch response.result {
                case .success(let response):
                    HouseHandler(.success(response))
                case let .failure(error):
                    HouseHandler(.failure(error))
                }
            }
}

Question

Use two functions to get car and house information. I wonder if it’s possible to put this together.

Reason

The idea that if I receive information using only one communication function, I can handle the error at once.

My idea

The parameters or get, post information for handing over to the server are made by another function using communication and handed over to one communication function

Desired result

func getData(request, AllHandler: @escaping (Result<AnyModelStruct, Error>) -> Void) {
    AF.request(request)
                .validate(statusCode: 200..<300)
                .validate(contentType: ["application/json"])
                .responseDecodable(of: AnyModelStruct.self) { (response) in
                switch response.result {
                case .success(let response):
                    AllHandler(.success(response))
                case let .failure(error):
                    AllHandler(.failure(error))
                }
            }
}

Problem

func getData(request, AllHandler: @escaping (Result<HouseResponseModel, Error>) -> Void) {
    AF.request(request)
                .validate(statusCode: 200..<300)
                .validate(contentType: ["application/json"])
                .responseDecodable(of: HouseResponseModel.self) { (response) in
                switch response.result {
                case .success(let response):
                    AllHandler(.success(response))
                case let .failure(error):
                    AllHandler(.failure(error))
                }
            }
}

let url = URL(string: "http://www.stackoverflow.com")!
var request = URLRequest(url: url)
request.httpMethod = "POST"

I know that url, method, parameters, encoding, header information can be generated and handed over as a request,

I don’t know how to give and receive a model…

Model information is required for escape and response decoding. Is there a way to hand over the model information here? Or is it possible to set the Any type?

Others

Lastly, I would like to know a tip on how to proceed when I perform many API requests in other projects, if the method I’m trying now is not a popular method.

[ad_2]

Source link

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments