{"id":18374357,"url":"https://github.com/mejiagarcia/simple-networking","last_synced_at":"2025-04-06T19:32:37.869Z","repository":{"id":56922117,"uuid":"225501732","full_name":"mejiagarcia/simple-networking","owner":"mejiagarcia","description":"Full Codable support Networking library.","archived":false,"fork":false,"pushed_at":"2024-04-26T08:53:30.000Z","size":1227,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T09:51:59.264Z","etag":null,"topics":["cocoapods","networking","swift"],"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/mejiagarcia.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-03T01:18:48.000Z","updated_at":"2024-04-26T08:53:33.000Z","dependencies_parsed_at":"2024-11-06T00:14:20.457Z","dependency_job_id":"5ce302b2-5c76-4313-b0c4-529b9d3b9df2","html_url":"https://github.com/mejiagarcia/simple-networking","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mejiagarcia%2Fsimple-networking","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mejiagarcia%2Fsimple-networking/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mejiagarcia%2Fsimple-networking/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mejiagarcia%2Fsimple-networking/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mejiagarcia","download_url":"https://codeload.github.com/mejiagarcia/simple-networking/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247539443,"owners_count":20955306,"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":["cocoapods","networking","swift"],"created_at":"2024-11-06T00:14:17.540Z","updated_at":"2025-04-06T19:32:32.857Z","avatar_url":"https://github.com/mejiagarcia.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hey folks, thanks for checking this repo! So, I made this library for an online iOS course I taught back in 2019, I do not maintain this library, nor recommend using it. These days all you need is URLSession and a fancy await/async usage. Happy coding!\n\n# Simple-Networking\n\n[![CI Status](https://travis-ci.org/mejiagarcia/simple-networking.svg?branch=master)](https://travis-ci.org/mejiagarcia/simple-networking)\n[![Version](https://img.shields.io/cocoapods/v/Simple-Networking.svg?style=flat)](https://cocoapods.org/pods/Simple-Networking)\n[![License](https://img.shields.io/cocoapods/l/Simple-Networking.svg?style=flat)](https://cocoapods.org/pods/Simple-Networking)\n[![Platform](https://img.shields.io/cocoapods/p/Simple-Networking.svg?style=flat)](https://cocoapods.org/pods/Simple-Networking)\n\nForget about all serialization logic, mappers and old networking stuff, Simple-Networking is build on top of **URLSession** and **Codable** protocol to make your life easier. \n\n-  [Example](#example)\n-  [Installation](#installation)\n-  [Perform GET/POST request](#usage)\n-  [Headers](#headers)\n-  [Debug Mode](#debug)\n-  [SSL Pinning](#ssl-pinning)\n-  [Documentation](#documentation)\n\n## Example\nTo run the example project, clone the repo, open `SimpleNetworking.xcworkspace` and open the Example target.\n\n## Installation\n\n### Cocoapods\n\nSimpleNetworking is available through [CocoaPods](https://cocoapods.org). To install it, simply add the following line to your Podfile:\n\n```ruby\n\npod 'Simple-Networking'\n\n```\n\n### Swift Package Manager\n\nSimpleNetworking is available through [Swift Package Manager](https://swift.org/package-manager/). To install\nit, simply add the following line to your `Package.swift` or your xcode project configuration.\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/mejiagarcia/simple-networking.git\", .upToNextMajor(from: \"0.3.9\"))\n]\n```\n\n## Usage\n```swift\nimport Simple_Networking\n ```\n\n### Perform a GET request with a Codable result.\n\n**Given the next model:**\n\n1. Success case model.\n```swift\nstruct  User: Codable {\n\tlet title: String\n\tlet userId: Int\n}\n ```\n\nPerform your request expecting the given model as the result: \n\n```swift\n// 1. Prepare your endpoint.\nlet endpoint = \"https://jsonplaceholder.typicode.com/todos/1\"\n\n// 2. Put your Codable type in the result block, in this example, your type is the *User* model.\nSN.get(endpoint: endpoint) { [weak self] (response: SNResult\u003cUser\u003e) in\n\tswitch response {\n\tcase .error(let error):\n\t\t// 3. Hanlde the possible error.\n\t\tprint(error.localizedDescription)\n\t\t\n\tcase .success(let response):\n\t\t// 4. Enjoy your codable result.\n\t\tself?.nameLabel.text = response.title // \u003c- Response is your User model.\n\t}\n}\n ```\n \n### Perform a GET request with a Codable result and a Codable error.\n\n**Given the next two models:**\n\n1. Success case model.\n```swift\nstruct User: Codable {\n\tlet title: String\n\tlet userId: Int\n}\n ```\n \n2. Error case model\n```swift\nstruct ServerError: Codable {\n\tlet error: String\n\tlet serverError: String\n}\n ```\n\nPerform your request to get the model number 1 in success case or model number 2 in error case:\n\n```swift\n// 1. Prepare your endpoint (this particular one has a 404 response).\nlet endpoint = \"http://www.mocky.io/v2/5de68cd33700005a000924a4\"\n\n// 2. Put your Codable type in the result block.\nSN.get(endpoint: endpoint) { [weak self] (response: SNResultWithEntity\u003cUser, ServerError\u003e) in \n\tswitch response {\n\t// Regular error\n\tcase .error(let error):\n\t\tprint(error.localizedDescription)\n\t\t\n\t// Error parsed to your error entity\n\tcase .errorResult(let entity):\n\t\tprint(entity.error) // \u003c- Entity is ServerError\n\t\tprint(entity.serverError)\n\t\n\t// Regular success\n\tcase .success(let response):\n\t\tprint(response.title) // \u003c- response is User\n\t}\n}\n ```\n\n### Perform a POST request with a Codable result and a Codable request.\n\n**Given the next model:**\n\n1. Success case and request model.\n```swift\nstruct  User: Codable {\n\tlet title: String\n\tlet userId: Int\n}\n ```\n\nPerform your request expecting the given model as the result and sending your model as the request body: \n\n```swift\n// 1. Prepare your endpoint.\nlet endpoint = \"https://jsonplaceholder.typicode.com/posts\"\n\n// 2. Prepare your request (Codable model)\nlet request = User(title: \"test title\", userId: 99999)\n\n// 2. Make the request\nSN.post(endpoint: endpoint, model: request) { [weak self] (response: SNResult\u003cUser\u003e) in\n\tswitch response {\n\tcase .error(let error):\n\t\t// 3. Hanlde the possible error.\n\t\tprint(error.localizedDescription)\n\t\t\n\tcase .success(let response):\n\t\t// 4. Enjoy\n\t\tself?.humanNameLabel.text = response.title\n\t}\n}\n ```\n\n## Headers\nBy default, SimpleNetworking uses `application/json` as the `Content-Type` of every request. You can change this or use your own headers:\n\n```swift\nSimpleNetworking.defaultHeaders = [\n\t\"Content-Type\": \"application/json\",\n\t// Your headers\n]\n```\n\n### Authentication\nWe added a helper method to help you add an authentication header (e.g. a JWT Token):\n\n```swift\nSimpleNetworking.setAuthenticationHeader(prefix: \"Bearer\", token: \"TOKEN\")\n```\n\nCalling this method will result in:\n\n```Authentication: Bearer TOKEN```\n\n### Debug\nWe provide a debug mode in order to see in detail all your requests through the Pod in Xcode console, this is how it works:\n\n1. Check only your API responses:\n```swift\nSimpleNetworking.debugMode = .onlyResponses\n```\n\n\u003cimg src=\"./Screenshots/response_example.png\" width=\"500px\"/\u003e\n\n2. Check only your API requests:\n```swift\nSimpleNetworking.debugMode = .onlyRequests\n```\n\n\u003cimg src=\"./Screenshots/request_example.png\" width=\"500px\"/\u003e\n\n3. Check all your requests and responses from your API:\n```swift\nSimpleNetworking.debugMode = .all\n```\n\nBy default debug mode is `disabled`.\n\n## SSL-Pinning\nWe support certificate pinning (available in v0.3.5), we are still working in Public Key pinning support (PR's are welcome). In order to setup your certificate you have to call `setupSSLPinnig` method:\n\n```swift\nif let certPath = Bundle.main.path(forResource: \"my_cert\", ofType: \".der\") {\n\tSimpleNetworking.setupSSLPinnig(certificateFullPath: certPath)\n}\n```\n\nThis method will compare the remote certificate against your local certificate. The remote certificate is obtained from the Host you are targeting in the request.\n\n## Documentation\n*SNResult*\n```swift\npublic enum SNResult\u003cT: Codable\u003e {\n\tcase success(response: T)\n\tcase error(error: SNErrors)\n}\n```\n\n*SNResultWithEntity*\n```swift\npublic  enum  SNResultWithEntity\u003cT: Codable, Y: Codable\u003e {\n\tcase success(response: T)\n\tcase error(error: SNErrors)\n\tcase errorResult(entity: Y)\n}\n```\n\n*SNResultBlock*\n```swift \npublic typealias SNResultBlock\u003cT: Codable\u003e = ((_ response: SNResult\u003cT\u003e) -\u003e Void)?\n```\n\n*SNResultBlockWithError*\n```swift \npublic typealias SNResultBlockWithError\u003cT: Codable, Y: Codable\u003e = ((_ response: SNResultWithEntity\u003cT, Y\u003e) -\u003e Void)?\n```\n\n*SNErrors*\n```swift\npublic  enum  SNErrors: Error {\n\tcase endpoint\n\tcase badResponse\n\tcase custom(error: Error?)\n\tcase emptyContent\n\tcase unknown(error: String?)\n}\n```\n\n## License\nSimpleNetworking 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%2Fmejiagarcia%2Fsimple-networking","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmejiagarcia%2Fsimple-networking","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmejiagarcia%2Fsimple-networking/lists"}