aronbalog / Vox

Swift JSON:API client framework
http://jsonapi.org
MIT License
46 stars 19 forks source link

Write functions just on time #29

Open ZahraAsgharzadeh opened 4 years ago

ZahraAsgharzadeh commented 4 years ago

Hello Mr Balog. I’m using your Vox library , I have a question . I use it in whole of my project , I write repeating code every time and it’s not ok. Look at this attached swift file , this is my function that every time I use. Is there any way to write it once and call it every where I need and just change resource and method every time not to write all function , because I am doing same in on failure block every time . I’ll be really happy if you help me dear Aron Balog .

let client = JSONAPIClient.Alamofire(baseURL: baseURL)
let dataSource = DataSource<resourse>(strategy: .path("/path"), client: client)
do{
    try dataSource
        .fetch().include(["something"])
        .result({ (document: Document<[resourse]>) in
            let results = document.data

            // do some thing

        }) { (error) in
            // and do this every time
            if let error = error as? JSONAPIError {
                switch error {
                case .API( let errors):
                    ApiResponse(response: errors)
                default:()
                }
                return
            }else{
                ApiResponse(response: nil)
            }
    }
}catch let error_object{
    print(error_object)
}