{"id":24969410,"url":"https://github.com/intrepidpursuits/webservices-example-ios","last_synced_at":"2025-04-11T04:40:32.488Z","repository":{"id":83643457,"uuid":"68042114","full_name":"IntrepidPursuits/webservices-example-ios","owner":"IntrepidPursuits","description":"Example iOS project for the \"Consuming Web Services\" lecture at Intrepid Pursuits. Written in Swift.","archived":false,"fork":false,"pushed_at":"2018-06-18T18:34:58.000Z","size":44,"stargazers_count":1,"open_issues_count":0,"forks_count":10,"subscribers_count":11,"default_branch":"interactive","last_synced_at":"2025-03-25T02:43:45.074Z","etag":null,"topics":[],"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/IntrepidPursuits.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":"2016-09-12T19:47:58.000Z","updated_at":"2020-01-29T21:30:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"deefaa95-5158-47d6-8b55-4b06d125f534","html_url":"https://github.com/IntrepidPursuits/webservices-example-ios","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IntrepidPursuits%2Fwebservices-example-ios","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IntrepidPursuits%2Fwebservices-example-ios/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IntrepidPursuits%2Fwebservices-example-ios/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IntrepidPursuits%2Fwebservices-example-ios/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IntrepidPursuits","download_url":"https://codeload.github.com/IntrepidPursuits/webservices-example-ios/tar.gz/refs/heads/interactive","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345274,"owners_count":21088241,"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":"2025-02-03T14:37:56.881Z","updated_at":"2025-04-11T04:40:32.480Z","avatar_url":"https://github.com/IntrepidPursuits.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Consuming Web Services in iOS\n\n## In-lecture exercise\n\nUpdate the `name` property in the `ViewModel` class to match your first name (all lowercase, no spaces).\n\nImplement the `execute` method of the struct `HTTPRequestHandler` to handle **GET** requests. To test your implementation run the app in the simulator or on device then click the 🔁 icon.\n\n### Solution\n\nYour finished method should look something like this:\n\n```\nfunc execute(callback: @escaping (Result\u003cAny\u003e) -\u003e Void) {\n    guard let url = URL(string: path) else {\n        callback(.failure(RequestError.invalidURL))\n        return\n    }\n\n    var request = URLRequest(url: url)\n    request.httpMethod = method.rawValue\n\n    if let headers = headers {\n        for (key, value) in headers {\n            request.addValue(value, forHTTPHeaderField: key)\n        }\n    }\n\n    if let body = body {\n        do {\n            request.httpBody = try JSONEncoder().encode(body as? ColorData)\n        } catch (let e) {\n            callback(.failure(e))\n        }\n    }\n\n    let task = URLSession.shared.dataTask(with: request) { data, response, error in\n        if let error = error {\n            callback(.failure(error))\n            return\n        }\n\n        guard let response = response as? HTTPURLResponse else {\n            callback(.failure(RequestError.noResponse))\n            return\n        }\n\n        guard response.statusCode \u003c 400 else {\n            callback(.failure(RequestError.httpResponse(response.statusCode)))\n            return\n        }\n\n        guard let data = data else {\n            callback(.failure(RequestError.noData))\n            return\n        }\n\n        if let str = String(data: data, encoding: .utf8) {\n            print(\"Received response: \\(str)\")\n        }\n\n        do {\n            let colorData = try JSONDecoder().decode(ColorData.self, from: data)\n            callback(.success(colorData))\n        }\n        catch {\n            callback(.failure(RequestError.decoderFailure))\n        }\n    }\n    task.resume()\n}\n```\n\n## Post-lecture exercise\n\nBuild upon your `execute` function to handle **PUT** requests as well. You can see what the post takes in as arguments in the `ViewModel` class under the `setColor` method. You will need to add header values to and serialize the body data for URLRequest.\n\nTo test your implementation, run your app in simulator or on device and select a color from the color picker. The color you picked should appear under your name in the companion web app.\n\nCreate a pull request with this updated code for review and feedback.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintrepidpursuits%2Fwebservices-example-ios","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintrepidpursuits%2Fwebservices-example-ios","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintrepidpursuits%2Fwebservices-example-ios/lists"}