{"id":15787184,"url":"https://github.com/fmo91/konex","last_synced_at":"2025-10-13T11:37:21.922Z","repository":{"id":56918011,"uuid":"80465615","full_name":"fmo91/Konex","owner":"fmo91","description":"The modular Swift networking library ☁️","archived":false,"fork":false,"pushed_at":"2017-02-09T15:06:36.000Z","size":764,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-15T21:09:43.958Z","etag":null,"topics":["ios","networking","swift"],"latest_commit_sha":null,"homepage":"https://fmo91.github.io/Konex/","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/fmo91.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-30T21:29:31.000Z","updated_at":"2025-01-17T20:06:14.000Z","dependencies_parsed_at":"2022-08-21T03:50:57.128Z","dependency_job_id":null,"html_url":"https://github.com/fmo91/Konex","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/fmo91/Konex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FKonex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FKonex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FKonex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FKonex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fmo91","download_url":"https://codeload.github.com/fmo91/Konex/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FKonex/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279014803,"owners_count":26085594,"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","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["ios","networking","swift"],"created_at":"2024-10-04T21:06:10.002Z","updated_at":"2025-10-13T11:37:21.883Z","avatar_url":"https://github.com/fmo91.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Konex\n\n[![CI Status](http://img.shields.io/travis/fmo91/Konex.svg?style=flat)](https://travis-ci.org/fmo91/Konex)\n[![Version](https://img.shields.io/cocoapods/v/Konex.svg?style=flat)](http://cocoapods.org/pods/Konex)\n[![License](https://img.shields.io/cocoapods/l/Konex.svg?style=flat)](http://cocoapods.org/pods/Konex)\n[![Platform](https://img.shields.io/cocoapods/p/Konex.svg?style=flat)](http://cocoapods.org/pods/Konex)\n[![codebeat badge](https://codebeat.co/badges/70b7f330-d723-4e0c-8a68-7268be2b464d)](https://codebeat.co/projects/github-com-fmo91-konex)\n\n![GitHub Logo](/assets/portrait-01.jpg)\n\n## Introduction\n\nKonex is a lightweight protocol-oriented networking library written in swift that can be easily extended or modified. It enforces a networking layer organization by forces to implement each request in a separate object. \nKonex can optionally parse responses to json objects.\n\n## Features\n\n- [x] Totally modular\n- [x] Protocol oriented \n- [x] Highly extensible\n- [x] Easy to begin with\n- [x] Already parsed responses\n- [x] Lightweight\n- [x] Enforces architectural best practices\n- [ ] Much more coming soon... \n\n## Brief practical example\n\nAt the core of the Konex library there is the `KonexRequest` protocol. So, the first you have to do is implement that protocol in a struct or a class.\n\n```swift\nstruct GetAllPostsRequest: KonexRequest {\n\tlet path = \"https://jsonplaceholder.typicode.com/posts/\"\n    let method = .post\n}\n```\n\nWe need a Post class that can be written like this:\n\n```swift\nimport ObjectMapper\n\nstruct Post: KonexJSONDecodable {\n    var id: Int?\n    var title: String?\n    \n    init() {}\n    \n    static func instantiate(withJSON json: [String : Any]) -\u003e Post? {\n        var post = Post()\n        \n        post.id = json[\"id\"] as? Int\n        post.title = json[\"title\"] as? String\n        \n        return post\n    }\n}\n```\n\nIt's important for our purposes that the Post class implements `KonexJSONDecodable` protocol.\nOnce you have the model and the request modelled, we are in conditions of dispatching that request and getting the response.\n\nThe class that is responsible for dispatching requests is `KonexClient`. It can be used as is, so you can instantiate it and start using in wherever you want.\n\n```swift\nlet client = KonexClient()\n\nlet request = GetAllPostsRequest()\n\nclient.requestArray(of: Post.self,\n    request: request,\n    onSuccess: { (posts: [Post]) in\n        // You can do whatever you want with your posts!\n    },\n    onError: { (error: Error) in\n        // You should handle this...\n    }\n)\n```\n\nAnd that's all! You can have base requests, if you like object oriented programming. This and more is going to be extended in the following sections.\n\n## Creating requests\n\nYou can model your requests in either a protocol-oriented fashion, or in a more object oriented one.\n\n**Protocol oriented request**: Konex provides a `KonexRequest` protocol that is required for your Request to implement in order to be dispatched by a `KonexClient`.\n`KonexRequest` is a very important part of the KonexLibrary. A `KonexRequest` defines the following properties that you can implement in your requests:\n\n* **path**: A string that represents the request URL. **This is the only required property**.\n* **method**: An enum value that represents the request HTTP method. It is of `Konex.HTTPMethod` type. Defaults to `.get`\n* **parameters**: A JSON object that will be added to the request URL in case of `.get` request or to the http body in any other case. Defaults to `nil`\n* **headers**: The request headers. Defaults to `nil`\n\nIn addition, `KonexRequest` also lets you add Konex extension components that are exclusive to your request. Konex extension components will be explained later in this guide, but for the moment, there are them:\n\n* **requestPlugins**:  It's an array of `KonexPlugin` objects. Defaults to [].\n* **requestResponseProcessors**: It's an array of `KonexResponseProcessor` objects. Defaults to [].\n* **requestResponseValidators**: It's an array of `KonexResponseValidator` objects. Defaults to [].\n\n**Object oriented request**: In addition to the protocol oriented way, Konex allows you to model your requests in a more object oriented way. Konex defines a `KonexBasicRequest` class, that implements `KonexRequest` protocol and is totally open to subclass. **It can't be used as is. It's kind of an abstract class. MUST SUBCLASS**.\n\n```swift\nopen class KonexBasicRequest: KonexRequest {\n    open var requestPlugins: [KonexPlugin] { return [] }\n    open var requestResponseProcessors: [KonexResponseProcessor] { return [] } \n    open var requestResponseValidators: [KonexResponseValidator] { return [] }\n    \n    open var path: String { return \"\" }\n    open var method: Konex.HTTPMethod { return .get }\n    open var parameters: [String : Any]? { return [:] }\n    open var headers: [String : String]? { return [:] }\n    \n    public init() {}\n}\n```\n\nThat simple. You can subclass it and define your Base Request or something like that. You can also have an AuthenticatedRequest or something like that. You are free to create your requests hierarchy as you want.\n\n## Dispatching requests\n\nTo dispatch `KonexRequest` objects, you need a `KonexClient`. `KonexClient` relies on URLSession to dispatch requests. So the first step is creating a `KonexClient`. This can be done using its initializer:\n\n```swift\nlet client = KonexClient()\n```\n\nThis initalizes its `URLSession` attribute member to `URLSession.default`. If you want, you can inject another `URLSession` using this initializer:\n\n```swift\nlet client = KonexClient(urlSession: anotherSession)\n```\n\nOnce you have a `KonexClient`, you can use it to dispatch `KonexRequest` objects.\n\n`KonexClient` defines three methods to do so:\n\n```swift\nopen func request(\n\trequest: KonexRequest, \n    plugins localPlugins: [KonexPlugin] = [], \n    responseProcessors localResponseProcessors: [KonexResponseProcessor] = [], \n    responseValidators localResponseValidators: [KonexResponseValidator] = [], \n    onSuccess: @escaping (Any) -\u003e Void, \n    onError: @escaping (Error) -\u003e Void) -\u003e URLSessionDataTask?\n```\n\n`request` method defines the core logic to perform requests. It dispatches your requests and you pass two closures to it, one for the success case, and another one for the error case.\n\n`KonexClient` can also parse the responses so you can get the final version of the data. There are two methods. `requestObject` and `requestArray`, both of those are similar.\n\n```swift\nopen func requestObject\u003cT:Mappable\u003e(ofType type: T.Type, \n\trequest: KonexRequest, \n    plugins localPlugins: [KonexPlugin] = [], \n    responseProcessors localResponseProcessors: [KonexResponseProcessor] = [],\n    responseValidators localResponseValidators: [KonexResponseValidator] = [],\n    onSuccess: @escaping (T) -\u003e Void, \n    onError: @escaping (Error) -\u003e Void) -\u003e URLSessionDataTask?\n\nopen func requestArray\u003cT: Mappable\u003e(of type: T.Type, \n\trequest: KonexRequest, \n    plugins localPlugins: [KonexPlugin] = [], \n    responseProcessors localResponseProcessors: [KonexResponseProcessor] = [],\n    responseValidators localResponseValidators: [KonexResponseValidator] = [], \n    onSuccess: @escaping ([T]) -\u003e Void, \n    onError: @escaping (Error) -\u003e Void) -\u003e URLSessionDataTask?\n```\n\nFinally, `KonexClient` is an open class, and so its member methods, so you can customize it as your will.\n\n## Extending Konex\n\n`Konex` defines three protocols that you can use in order to extend your requests dispatching logic. \n\n* **KonexPlugin**: It defines two methods: `didSendRequest` and `didReceiveResponse`. An example of this could be a Network logger, or a Network indicator handler.\n* **KonexResponseProcessor**: Defines `func process(response: Any) -\u003e Any`, that allows you to create functional pipes to process the response that comes after dispatching a request.\n* **KonexResponseValidator**: Defines `func validate(response: Any) throws`\n\nKonex components can be added at three different levels:\n\n* **The client level**: `KonexClient` exposes properties called `plugins`, `responseProcessors` and `responseValidators` where you can append your extension components.\n* **The request level**: `KonexRequest` defines three properties, `requestPlugins`, `requestResponseProcessors` and  `requestResponseValidators`.\n* **The method level**: `KonexClient` methods accepts extension components within their arguments\n\n## Example\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Requirements\n\n## Installation\n\nKonex is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'Konex', :git =\u003e 'https://github.com/fmo91/konex'\n```\n\n## Author\n\nfmo91, ortizfernandomartin@gmail.com\n\n## License\n\nKonex is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmo91%2Fkonex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffmo91%2Fkonex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmo91%2Fkonex/lists"}