{"id":17274210,"url":"https://github.com/davidthorn/datarequest","last_synced_at":"2025-03-26T13:26:44.683Z","repository":{"id":122657164,"uuid":"117364479","full_name":"davidthorn/DataRequest","owner":"davidthorn","description":"DataRequest framework provides a helpful object which can handles with all of the complex boiler plate code you need to write in order to download something from a URL.","archived":false,"fork":false,"pushed_at":"2018-01-28T17:05:27.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T14:47:58.001Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidthorn.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":"2018-01-13T17:42:18.000Z","updated_at":"2018-01-28T18:06:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"4fea0d7e-829e-4338-b801-77e2f253d683","html_url":"https://github.com/davidthorn/DataRequest","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2FDataRequest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2FDataRequest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2FDataRequest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2FDataRequest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidthorn","download_url":"https://codeload.github.com/davidthorn/DataRequest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245661516,"owners_count":20651878,"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":[],"created_at":"2024-10-15T08:53:22.544Z","updated_at":"2025-03-26T13:26:44.651Z","avatar_url":"https://github.com/davidthorn.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DataRequest\n\nThe framework is designed to reduce the amount of repeative boiler plate code which is required to make a URLSession request for a url string.\n\nBy using this framework you can really go from writing 20 lines of code to basically one if you are only interested in the Data which returns from the request.\n\n# Topics\n\n* [Requirements](#requirements)\n\n* [Installation](#installation)\n  * [Carthage](#carthage)\n  * [Swift Package Manager](#swift-pm)\n\n* [Usage](#usage)\n  \n  * [Create DataRequest instance using a String](#item1)\n  * [Create DataRequest instance using a URL](#item2)\n  * [Inferring URL is secure](#item3)\n  * [Using DataRequestResult](#item4)\n  * [Asynchronous Data Request with completion handler](#item5)\n  * [Asynchronous DataRequest using the value property of DataRequestResult](#item6)\n  * [Synchronous DataRequest ](#item7)\n  * [Optional Data using Synchronous DataRequest](#item8)\n  * [String's Extensions](#item9)\n  * [URL's Extensions](#item10)\n  \n* [References](#references)\n\n\u003ca name=\"requirements\"\u003e\u003c/a\u003e\n\n# Requirements\n\n* Xcode 9.2\n* Minimum iOS Deploment Target 9.0\n* Swift 4.0\n\n\u003ca name=\"installation\"\u003e\u003c/a\u003e\n\n# Installation\n\n\u003ca name=\"carthage\"\u003e\u003c/a\u003e\n\n# [Carthage](https://github.com/Carthage/Carthage)\n\n**Tested with `carthage version`: `0.26.2`**\n\nAdd this to `Cartfile`\n\n```\ngit \"https://github.com/davidthorn/DataRequest.git\"\n```\n\n```bash\n$ carthage update\n```\n\n \u003ca name=\"swift-pm\"\u003e\u003c/a\u003e\n\n# [Swift Package Manager](https://github.com/apple/swift-package-manager)\n\n**Tested with `swift build --version`: `Swift 4.0.0-dev (swiftpm-13126)`**\n\nCreate a `Package.swift` file.\n\n```swift\n\n// swift-tools-version:4.0\n\nimport PackageDescription\n\nlet package = Package(\n    name: \"PACKAGE_NAME\",\n    dependencies: [\n        .package(url: \"https://github.com/davidthorn/DataRequest.git\", from: \"2.0.0\")\n    ],\n    targets: [\n        .target(name: \"PACKAGE_NAME\", dependencies: [\n        \"DataRequest\"\n        ])\n    ]\n    )\n\n```\n\n```bash\n$ swift build\n\n```\n\n\u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n# Usage\n\n## Create DataRequest instance using a String \u003ca name=\"item1\"/\u003e\n\n```swift\n/// Creates an optional DataRequest object\nlet dataRequest: DataRequest? = DataRequest(urlString: \"https://github.com/davidthorn/DataRequest.git\")\n```\n\n \u003ca name=\"item2\"\u003e\u003c/a\u003e\n\n## Create DataRequest instance using a URL\n\n```swift\nlet url = URL(string: \"https://github.com/davidthorn/DataRequest.git\")\nlet dataRequest: DataRequest = DataRequest(url: url)\n..\n```\n\u003ca name=\"item3\"\u003e\u003c/a\u003e\n\n## Inferring URL is secure \n\nThe `https://` scheme can be left out of the url because it will be inferred that this is a secure request.\n\n```swift\nlet url = URL(string: \"github.com/davidthorn/DataRequest.git\")\nlet dataRequest: DataRequest = DataRequest(url: url)\n..\n```\n\n \u003ca name=\"item4\"\u003e\u003c/a\u003e\n\n## Using `DataRequestResult`\n\nAll methods in the `DataRequest` framework will return a `DataRequestResult`.\n\nA `DataRequestResult` is an enum containing 2 possible responses:\n\n```swift\npublic enum DataRequestResult {\n    // Used only if the data request returns Data and a URLResponse\n    case success(Data, URLResponse)\n    /// Used for all other situations where success is not used\n    case fail(Error)\n}\n```\n\n\u003ca name=\"item5\"\u003e\u003c/a\u003e\n\n## Asynchronous Data Request with completion handlers DataRequestResult \n\nIn this example the DataRequestResult has been used with a `switch` case.\n\n```swift\nimport Foundation\nimport DataRequest\n\nlet dataRequest = DataRequest(urlString: \"github.com/davidthorn/DataRequest.git\")\n\ndataRequest.data { (result: DataRequestResult) in\n    switch result {\n        case .success(let data: Data , let response: URLResponse):\n            // use the data\n        case .fail(let error):\n            // an error has occurred\n    }\n\n}\n```\n\n\u003ca name=\"item6\"\u003e\u003c/a\u003e\n\n## Asynchronouse DataRequest using the value property of DataRequestResult\n\nIf you are not interested in handling the error case, then you can access the `value: Data?` property of \nthe `DataRequestResult` directly\n\n```swift\nimport Foundation\nimport DataRequest\n\nlet dataRequest = DataRequest(urlString: \"github.com/davidthorn/DataRequest.git\")\n\ndataRequest.data { (result: DataRequestResult) in\n    // check if the result has data if so then the it will return here with a value\n    guard let data: Data = result.value else { return }\n\n}\n```\n\n\u003ca name=\"item7\"\u003e\u003c/a\u003e\n\n## Synchronous DataRequest \n\nIf this request should be completed synchronously then you can use the `sync` property of the `DataRequest` to retrieve the `DataRequestResult`.\n\n```swift\nimport Foundation\nimport DataRequest\n\nlet dataRequest = DataRequest(urlString: \"github.com/davidthorn/DataRequest.git\")\nlet dataResult: DataRequestResult = dataRequest.sync\n\n\n```\n\n\u003ca name=\"item8\"\u003e\u003c/a\u003e\n\n## Optional Data using Synchronous DataRequest \n\nOnce again you can use the `value: Data?` property of the `DataRequestResult` whilst using the `sync` property of the DataRequest.\n\n```swift\nimport Foundation\nimport DataRequest\n\nlet dataRequest: DataRequest = DataRequest(urlString: \"github.com/davidthorn/DataRequest.git\")\n\nguard let data: Data = dataRequest.sync.value else {\n    // not data has been return\n    return\n}\n```\n\n\u003ca name=\"item9\"\u003e\u003c/a\u003e\n\n# String's Extensions \n\nTo make this process even shorter you can use the `String`'s `syncURLRequest` property to execute this DataTask inline and synchronously\n\n```swift\n\nimport Foundation\nimport DataRequest\n\n// Option 1\n\nlet dataResult: DataRequestResult = \"github.com/davidthorn/DataRequest.git\".syncURLRequest\nlet data: Data? = dataResult.value\n\n// Option 2\n\nlet dataResult: Data? = \"github.com/davidthorn/DataRequest.git\".syncURLRequest.value\n\n// Option 3 - Do the request synchronously with a completion handler\n\n\"github.com/davidthorn/DataRequest.git\".urlSyncRequest { result in\n    guard let data: Data? = result.value else { return }\n}\n\n// Option 3 - Do the request Asynchronously with a completion handler\n\n\"github.com/davidthorn/DataRequest.git\".urlAsyncRequest { result in\n    guard let data: Data? = result.value else { return }\n}\n\n```\n\n\n\u003ca name=\"item10\"\u003e\u003c/a\u003e\n\n# URL's Extensions \n\nTo make this process even shorter you can use the `URL`'s `syncURLRequest` property to execute this DataTask inline and synchronously\n\n```swift\n\nimport Foundation\nimport DataRequest\n\nlet secureString = \"github.com/davidthorn/DataRequest.git\".asSecureURLString\n\nlet url = URL(string: secureString)!\n\n// Option 1\n\nlet dataResult: DataRequestResult = url.sync\nlet data: Data? = dataResult.value\n\n// Option 2\n\nlet dataResult: Data? = url.sync.value\n\n// Option 3 - Do the request synchronously with a completion handler\n\nurl.sync { result in\n    guard let data: Data? = result.value else { return }\n}\n\n// Option 3 - Do the request Asynchronously with a completion handler\n\nurl.async { result in\n    guard let data: Data? = result.value else { return }\n}\n\n```\n\n## References\u003ca name=\"references\"/\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidthorn%2Fdatarequest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidthorn%2Fdatarequest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidthorn%2Fdatarequest/lists"}