{"id":19416719,"url":"https://github.com/panevnyk/restapimanager","last_synced_at":"2025-07-03T00:04:59.256Z","repository":{"id":62452835,"uuid":"123245569","full_name":"Panevnyk/RestApiManager","owner":"Panevnyk","description":"URLRequests in one line of code","archived":false,"fork":false,"pushed_at":"2022-02-12T17:09:48.000Z","size":141,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-03T00:04:09.675Z","etag":null,"topics":["apimanager","ios","swift","urlsession"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Panevnyk.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-28T07:18:42.000Z","updated_at":"2022-03-17T03:11:26.000Z","dependencies_parsed_at":"2022-11-01T23:46:06.803Z","dependency_job_id":null,"html_url":"https://github.com/Panevnyk/RestApiManager","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/Panevnyk/RestApiManager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Panevnyk%2FRestApiManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Panevnyk%2FRestApiManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Panevnyk%2FRestApiManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Panevnyk%2FRestApiManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Panevnyk","download_url":"https://codeload.github.com/Panevnyk/RestApiManager/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Panevnyk%2FRestApiManager/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263234941,"owners_count":23434918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apimanager","ios","swift","urlsession"],"created_at":"2024-11-10T13:04:19.759Z","updated_at":"2025-07-03T00:04:59.172Z","avatar_url":"https://github.com/Panevnyk.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":" \u003cp align=\"center\"\u003e  \n \u003cimg src=\"https://github.com/Panevnyk/RestApiManager/blob/master/Images/satellite-dish.png?raw=true\" width=\"180\"\u003e \n \u003c/p\u003e\n \n # RestApiManager\n - *Decouple URLSession logic from endpoints*\n - *Automapping of response objects*\n - *Responce types: Object, Array, String, CustomResponseSerializer*\n - *Easy work with multipart requests*\n - *Cool request, response printing in console*\n - *Auto show load indicator*\n - *Auto show error alert*\n - *Possibility to set a unique Error Type for each request (ResultWithET\u003cT, ET\u003e, where T - Response type, ET - Error Type)*\n \n ## CocoaPods\n \n Add the following line to your Podfile:\n \n ```rb\n pod 'RestApiManager'\n ```\n \n ## Usage\n \n #### Instance\n \n For use RestApiManager, you can simply create URLSessionRAMDIContainer, and inject it to RestApiManager instance.\n In URLSessionRAMDIContainer you can customize (errorType, urlSession, jsonDecoder, restApiAlert, restApiActivityIndicator, printRequestInfo) properties.\n \n ```swift\n let urlSessionRAMDIContainer = URLSessionRAMDIContainer(errorType: ExampleRestApiError.self, printRequestInfo: true)\n let restApiManager: RestApiManager = URLSessionRestApiManager(urlSessionRAMDIContainer: urlSessionRAMDIContainer)\n ```\n \n #### Create method\n \n For create API method you should realize RestApiMethod and ParametersProtocol protocols.\n \n ```swift\n enum QuestionsRestApiMethods: RestApiMethod {\n    // Method\n    case getQuestions(QuestionsRestApiParameters)\n \n    // URL\n    private static let getQuestionsUrl = \"questions\"\n \n    // RestApiData\n    var data: RestApiData {\n        switch self {\n        case .getQuestions(let parameters):\n            return RestApiData(url: RestApiConstants.baseURL + QuestionsRestApiMethods.getQuestionsUrl,\n                               httpMethod: .get,\n                               parameters: parameters,\n                               keyPath: RestApiConstants.items)\n        }\n    }\n }\n ```\n \n ```swift\n struct QuestionsRestApiParameters: ParametersProtocol {\n    let order = \"desc\"\n    let sort = \"votes\"\n    let site = \"stackoverflow\"\n \n    var parametersValue: Parameters {\n        let parameters: [String: Any] = [\n            RestApiConstants.order: order,\n            RestApiConstants.sort: sort,\n            RestApiConstants.site: site\n        ]\n \n        return parameters\n    }\n }\n ```\n \n #### Call\n \n For create API call, you should create RestApiMethod instance. And send request using some of existing \"call\" method of RestApiManager.\n \n Create RestApiMethod instance\n \n ```swift\n private let questionsParameters = QuestionsRestApiParameters()\n private let getQuestionsMethod: QuestionsRestApiMethods = .getQuestions(questionsParameters)\n ```\n \n Array response call\n \n ```swift   \n func simpleCall() {\n    restApiManager.call(method: getQuestionsMethod) { (result: Result\u003c[StackoverflowItemModel]\u003e) in\n        switch result {\n        case .success(let obj):\n            // some action with success result\n        case .failure(let error):\n            // some action with failure result\n        }\n    }\n }\n ```\n \n Call with autoshow indicator and errorAlert\n \n ```swift\n func simpleCallWithIndicatorAndErrorAlert() {\n    restApiManager.call(method: getQuestionsMethod, \n                        indicator: true,\n                        errorAlert: true) \n    { (result: Result\u003c[StackoverflowItemModel]\u003e) in\n        switch result {\n        case .success(let obj):\n            // some action with success result\n        case .failure(let error):\n            // some action with failure result\n        }\n    }\n }\n ```\n \n #### Error handling\n \n For error handling you can use our DefaultRestApiError.\n Or create your own error handling realization. For this, you should implement RestApiError protocol in your class, and inject it in URLSessionRAMDIContainer.\n \n In \"handle\" method of RestApiError protocol you should realize error parsing method that will return your optional class instance. If you return nil this means that you request will return success response.\n \n ```swift\n final class ExampleRestApiError: RestApiError {\n    // MARK: - Properties\n    var code: Int\n    var details: String\n \n    // MARK: - Inits\n    convenience init() {\n        self.init(code: 0)\n    }\n \n    convenience init(error: Error) {\n        let nsError = error as NSError\n        self.init(code: nsError.code, details: nsError.localizedDescription)\n    }\n \n    convenience init(code: Int) {\n        self.init(code: code, details: \"\")\n    }\n \n    init(code: Int, details: String) {\n        self.code = code\n        self.details = details\n    }\n \n    // MARK: - Handle\n \n    /// method for handle error by response, if parse to Error success, method will be return Error\n    ///\n    /// - Parameters:\n    ///   - error: Error?\n    ///   - urlResponse: URLResponse?\n    ///   - data: Data? for parsing to Error\n    /// - Returns: ExampleRestApiError\n    static func handle(error: Error?, urlResponse: URLResponse?, data: Data?) -\u003e ExampleRestApiError? {\n        if let error = error {\n            return ExampleRestApiError(error: error)\n        } else if let data = data {\n            guard !data.isEmpty else {\n                return nil\n            }\n \n            do {\n                if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject],\n                    let code = json[RestApiConstants.code] as? Int,\n                    let message = json[RestApiConstants.message] as? String {\n            \n                    let error = ExampleRestApiError(code: code, details: message)   \n                        return error    \n                } else {\n                    return nil\n                }\n            } catch let error as NSError {\n                return ExampleRestApiError(error: error)\n            }\n        } else {\n            return ExampleRestApiError.unknown\n        }\n    }\n }\n ```\n \n ```swift\n let urlSessionRAMDIContainer = URLSessionRAMDIContainer(errorType: ExampleRestApiError.self)\n let restApiManager: RestApiManager = URLSessionRestApiManager(urlSessionRAMDIContainer: urlSessionRAMDIContainer)\n ```\n \n #### Autoshow error alert\n \n For autoshow error alert you should set in \"call\" method \"indicator\" property as \"true\".\n You can use our DefaultRestApiAlert class for show alert.\n Or create your own alert realization. For this, you should implement RestApiAlert protocol in your class, and inject it in URLSessionRAMDIContainer.\n \n\n ```swift\n public class ExampleRestApiAlert: RestApiAlert {\n    /// init\n    public init() {}\n \n     /// show\n     ///\n     /// - Parameter error: RestApiError\n     public func show(error: RestApiError) {\n        show(title: \"Error\", message: error.details, completion: nil)\n     }\n \n     /// show\n     ///\n     /// - Parameters:\n     ///   - title: title of UIAlertController\n     ///   - message: message of UIAlertController\n     ///   - completion: completion of UIAlertController\n     public func show(title: String, message: String, completion: (() -\u003e Void)?) {\n     \n         guard let rootViewController = UIApplication.presentationViewController else {\n            return\n         }\n         \n         let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)\n         let okAction = UIAlertAction(title: \"Ok\", style: .default, handler: { [unowned self] _ in\n            completion?()\n         })\n         alert.addAction(okAction)\n         rootViewController.present(alert, animated: true, completion: nil)\n     }\n }\n ```\n \n #### Autoshow load indicator\n \n For autoshow load indicator you should set in \"call\" method \"indicator\" property as \"true\".\n You can use our DefaultRestApiActivityIndicator class for show indicator.\n Or create your own indicator view realization. For this, you should implement RestApiActivityIndicator protocol in your class, and inject it in URLSessionRAMDIContainer.\n \n ```swift\n func show()\n func show(onView view: UIView)\n func hide()\n ```\n \n ## Example target\n \n You can check more functionality of RestApiManager in [Example](Example) target.\n \n ## License\n \n RestApiManager is released under an MIT license. See [License](LICENSE.txt) for more information.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpanevnyk%2Frestapimanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpanevnyk%2Frestapimanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpanevnyk%2Frestapimanager/lists"}