Getting Started with the Get Networking Framework
The Get framework is a lightweight Swift framework for working with web APIs. Get is a nice framework, but the README file doesn’t provide much explanation on how to use the framework. This article shows how to start using the Get framework in your apps.
Basic Workflow
Using the Get framework involves the following basic steps:
- Create an API client instance
- Create a network request
- Send the request
Create an API Client Instance
Get provides an APIClient
class for API clients. Create an instance, supplying the base URL to the network API you want to use. The following example creates an API client for the Jira REST API:
let client = APIClient(baseURL:
URL(string: "https://api.atlassian.com"))
Create a Network Request
Get has a Request
struct that represents a network request. Fill the Request
struct to create a network request. The Request
struct has the following fields:
- The body, which you use to send data from your app to the API. POST and PUT methods require you to supply a body.
- Headers for the request. The
headers
field is a Swift dictionary where each key and value are strings. - An ID for the request. You probably won’t need to set an ID.
- The method, such as GET, POST, or PUT.
- Query items. The
query
field is an array of Swift tuples, consisting of two strings. - The URL for the network call.
The Get framework defaults to using GET methods and leaving the other fields blank so the only fields you have to fill are the ones you need to make the network call.
To create a request for a GET method, start by creating a variable of type Request
. Place the data type you want to return in angle brackets and supply the URL for the network call.
var request = Request<T>(url: urlPath)
Replace T
with the type you want to return from the network call. Replace urlPath
with the URL string for the API call to make.
After creating the request, fill the fields your network call requires.
request.headers = [
"Accept": "application/json",
"Content-Type": "application/json"
]
// Sort results alphabetically
request.query = [("orderBy", "name")]
If you have a method that sends data to the server, such as a POST or PUT method, you must create the body data and supply it when creating the request. The body
property is a let constant so you can’t set the body after creating the request.
var request = Request<T>(url: urlPath, body: bodyData)
request.method = "POST"
request.headers = [
"Accept": "application/json",
"Content-Type": "application/json"
]
The body must conform to the Encodable
protocol.
Sending the Request
The API client has a send
method to send the network request. Supply the request.
Usually a network call returns data. By attaching the value
property when sending the request, you will get decoded data back in the type you specified when creating the request.
return try await client.send(request).value
You’re going to use this code a lot when using Get. In one line of code you can send a network request and get data back in a form that your app can use.