{"id":15787186,"url":"https://github.com/fmo91/conn","last_synced_at":"2025-04-01T14:32:07.588Z","repository":{"id":56906486,"uuid":"134765396","full_name":"fmo91/Conn","owner":"fmo91","description":"Minimal yet modular networking layer for Swift.","archived":false,"fork":false,"pushed_at":"2018-07-16T19:19:51.000Z","size":37,"stargazers_count":23,"open_issues_count":0,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-11T21:45:22.909Z","etag":null,"topics":["ios","lightweight","networking","protocol-oriented-programming","swift"],"latest_commit_sha":null,"homepage":null,"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":"2018-05-24T20:29:19.000Z","updated_at":"2022-10-09T17:59:59.000Z","dependencies_parsed_at":"2022-08-20T19:20:21.392Z","dependency_job_id":null,"html_url":"https://github.com/fmo91/Conn","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FConn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FConn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FConn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fmo91%2FConn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fmo91","download_url":"https://codeload.github.com/fmo91/Conn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246604612,"owners_count":20804100,"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":["ios","lightweight","networking","protocol-oriented-programming","swift"],"created_at":"2024-10-04T21:06:12.631Z","updated_at":"2025-04-01T14:32:07.292Z","avatar_url":"https://github.com/fmo91.png","language":"Swift","readme":"# Conn\n\n[![CI Status](https://img.shields.io/travis/fmo91/Conn.svg?style=flat)](https://travis-ci.org/fmo91/Conn)\n[![Version](https://img.shields.io/cocoapods/v/Conn.svg?style=flat)](https://cocoapods.org/pods/Conn)\n[![License](https://img.shields.io/cocoapods/l/Conn.svg?style=flat)](https://cocoapods.org/pods/Conn)\n[![Platform](https://img.shields.io/cocoapods/p/Conn.svg?style=flat)](https://cocoapods.org/pods/Conn)\n\n## Introduction\n\nWhen writing networking layers, it´s common to end with a lot of boilerplate or repeated code.\n\nThere are currently some libraries that one can use in order to improve code readability and to avoid writing boilerplate, but they are usually so bloated that one doesn´t use the half of what that library offers.\n\nOn the other hand, there are some other libraries that are lightweight, but they often fall short when you need more advanced functionality.\n\nConn is the library that resolves this issue. It is very lightweight (118 lines counting empty lines until now), but it is still highly modular while it doesn't require the developer to write any boilerplate.\n\nYou can read more about the reasoning behind this library in my [article in Medium](https://medium.com/@ortizfernandomartin/minimal-networking-layer-from-scratch-in-swift-4-a151af786dc5).\n\n## Features\n\n- Declarative style\n- Very lightweight\n- Parses JSON to models\n- Replaceable network dispatcher\n- Minimal boilerplate required\n\n## Usage\n\nThe first thing you have to do is describing your requests as structs, classes or enums conforming to the `RequestType` protocol. For instance:\n\n```swift\nstruct GetAllUsers: RequestType {\n    typealias ResponseType = [User]\n    var data: RequestData {\n        return RequestData(path: \"https://jsonplaceholder.typicode.com/users\")\n    }\n}\n```\n\n`ResponseType` is an `associatedtype` that declares the format of the response. In this case, we expect to have an array of `User` as a response. The type associated to the request must implement `Codable` in order to be parsed. Take `User` as an example:\n\n```swift\nstruct User: Codable {\n    let id: Int\n    let username: String\n}\n```\n\n`RequestData` is a plain struct that defines the format of the request. In this case we are only defining a path (url), but `RequestData` also supports defining a http method, headers and params (body).\n\nThis is all you have to do. Then, to execute the request, `RequestType` has a method called `execute()` that actually dispatches the request:\n\n```swift\nGetAllUsers().execute(\n    onSuccess: { (users: [User]) in\n        // Do something with users\n    },\n    onError: { (error: Error) in\n        // Do something with error\n    }\n)\n```\n\nPlease note that users are already parsed as an array of `User`.\n\n## Advanced Usage\n\nIf you need to do something more complex, use other networking library like Alamofire, or anything else, the `execute` method in `RequestType` accepts an optional argument named `dispatcher` of type `NetworkDispatcher`.\n\n`NetworkDispatcher` is a protocol that only requires implementing a single method called `dispatch`:\n\n```swift\npublic protocol NetworkDispatcher {\n    func dispatch(request: RequestData, onSuccess: @escaping (Data) -\u003e Void, onError: @escaping (Error) -\u003e Void)\n}\n```\n\nBy default, `dispatch` uses `URLSessionNetworkDispatcher` that dispatches the request using `URLSession`. But you can define your own `NetworkDispatcher` as you want.\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\nConn is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'Conn'\n```\n\n## Author\n\nfmo91, ortizfernandomartin@gmail.com\n\n## License\n\nConn is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmo91%2Fconn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffmo91%2Fconn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffmo91%2Fconn/lists"}