{"id":18060721,"url":"https://github.com/0xleif/network","last_synced_at":"2025-03-28T06:31:03.499Z","repository":{"id":177450057,"uuid":"660401877","full_name":"0xLeif/Network","owner":"0xLeif","description":"☁️ Lightweight networking library for Swift ","archived":true,"fork":false,"pushed_at":"2024-04-03T23:20:47.000Z","size":22,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-03T10:46:33.388Z","etag":null,"topics":["ios","mac","networking","rest","rest-api","swift","tvos","watchos"],"latest_commit_sha":null,"homepage":"https://network.0xl.io/","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/0xLeif.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}},"created_at":"2023-06-29T23:48:15.000Z","updated_at":"2024-11-16T16:18:52.000Z","dependencies_parsed_at":"2024-03-30T23:46:04.528Z","dependency_job_id":null,"html_url":"https://github.com/0xLeif/Network","commit_stats":null,"previous_names":["0xleif/network"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xLeif%2FNetwork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xLeif%2FNetwork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xLeif%2FNetwork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xLeif%2FNetwork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0xLeif","download_url":"https://codeload.github.com/0xLeif/Network/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245984376,"owners_count":20704790,"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","mac","networking","rest","rest-api","swift","tvos","watchos"],"created_at":"2024-10-31T04:10:19.720Z","updated_at":"2025-03-28T06:31:03.074Z","avatar_url":"https://github.com/0xLeif.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Network\n\n`Network` is a lightweight networking library for Swift that makes it easy to interact with RESTful APIs. With `Network`, you can effortlessly make HTTP requests, handle responses, and process data in a concise and efficient way.\n\n## Features\n\n- **Simple and Intuitive API**: Make various HTTP requests (GET, POST, PUT, DELETE, etc.) with ease.\n- **Flexible Request Handling**: Comprehensive support for request headers, URL encoding, and request bodies.\n- **Asynchronous Requests**: Utilize Swift's `async/await` syntax for asynchronous network requests.\n- **Customizable URLSession**: Customize and configure URLSession with default or custom configurations.\n- **Mocking Support**: Easily mock network requests for simplified testing and development.\n\n## Installation\n\nTo integrate `Network` into your project, you can use the Swift Package Manager. Simply add the following dependency to your `Package.swift` file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/0xLeif/Network.git\", from: \"1.0.0\")\n]\n```\n\n## Usage\n\n### Performing a GET Request\n\n```swift\nimport Network\n\nlet network = Network()\n\ndo {\n    let dataResponse = try await network.get(url: URL(string: \"https://api.example.com/posts\")!)\n\n    if let data = dataResponse.data {\n        let json = try JSONSerialization.jsonObject(with: data, options: [])\n        print(json)\n    } else {\n        print(\"No response data\")\n    }\n} catch {\n    print(\"Error: \\(error)\")\n}\n```\n\n### Sending a POST Request\n\n```swift\nimport Network\n\nlet network = Network()\n\ndo {\n    let headers = [\"Authorization\": \"Bearer your-token\"]\n    let body: [String: Any] = [\n        \"name\": \"John Doe\",\n        \"email\": \"johndoe@example.com\"\n    ]\n\n    let dataResponse = try await network.post(\n        url: URL(string: \"https://api.example.com/users\")!,\n        headerFields: headers,\n        body: body\n    )\n\n    if let data = dataResponse.data {\n        let json = try JSONSerialization.jsonObject(with: data, options: [])\n        print(json)\n    }\n} catch {\n    print(\"Error: \\(error)\")\n}\n\n```\n\n### Defining an API\n\nIn order to define an API, you must first create an `Endpoint`. This is a protocol that outlines the basic components of a network request. These components include the URL, HTTP request method, path, headers, and body of the request.\n\nHere's an example of an `Endpoint`:\n\n```swift\nenum ExampleEndpoint: Endpoint {\n    static var url: URL { URL(string: \"example.com\")! }\n\n    case hello\n\n    var method: HTTPRequestMethod {\n        switch self {\n        case .hello:    return .GET\n        }\n    }\n\n    var path: String {\n        switch self {\n        case .hello:    return \"hello\"\n        }\n    }\n\n    var headers: [String: String] {\n        switch self {\n        case .hello:\n            return [\n                \"id\": \"hello\"\n            ]\n        }\n    }\n\n    var body: Data? {\n        switch self {\n        case .hello:    return \"hello\".data(using: .utf8)\n        }\n    }\n}\n\n```\n\nOnce the `Endpoint` is defined, you can create an instance of `API` or `MockAPI` (for testing) to send requests to the endpoint. Here's how you can use the `MockAPI` in a test:\n\n```swift\nfunc testAPI() async throws {\n    let api = MockAPI\u003cExampleEndpoint\u003e()\n    let expectedString = \"hello\"\n\n    let dataResponse = try await api.request(.hello)\n    let data = try XCTUnwrap(dataResponse.data)\n    let string = String(data: data, encoding: .utf8)\n\n    XCTAssertEqual(string, expectedString)\n}\n\n```\n\nThe above test creates a `MockAPI`, sends a `GET` request to the `hello` endpoint, and checks if the response matches the expected string \"hello\".\n\n## Contributing\n\nContributions to `Network` are welcome! If you have ideas for improvements, find a bug, or want to contribute code, please submit a pull request. For major changes, please open an issue first to discuss potential updates.\n\n## License\n\n`Network` is available under the MIT license. See the [LICENSE](https://github.com/0xLeif/Network/blob/main/LICENSE) file for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xleif%2Fnetwork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0xleif%2Fnetwork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xleif%2Fnetwork/lists"}