An open API service indexing awesome lists of open source software.

https://github.com/gabrieltheodoropoulos/gtrest

A lightweight Swift library for making web requests and consuming RESTful APIs!
https://github.com/gabrieltheodoropoulos/gtrest

ios rest restful-webservices swift web-services

Last synced: 6 months ago
JSON representation

A lightweight Swift library for making web requests and consuming RESTful APIs!

Awesome Lists containing this project

README

          

# GTRest
A lightweight Swift library for making web requests and consuming RESTful APIs!

![Platform](https://img.shields.io/cocoapods/p/GTRest.svg)
![Language](https://img.shields.io/github/languages/top/gabrieltheodoropoulos/GTRest.svg?color=orange)
![License](https://img.shields.io/github/license/gabrieltheodoropoulos/GTRest.svg)
![Version](https://img.shields.io/cocoapods/v/GTRest.svg)

## Adding GTRest to your project

### Using CocoaPods

In your Podfile add:

```ruby
pod 'GTRest'
```

Don't forget to import GTRest anywhere you want to use it in your project:

```swift
import GTRest
```

### Manually

Download or clone the repository, and add the files in *GTRest/Source* directory to your project.

## Usage

Making web requests with GTRest is really simple and straightforward!

See the [Documentation](https://gtiapps.com/docs/gtrest/index.html) generated by [jazzy](https://github.com/realm/jazzy) on how to use GTRest.

A quick example:

```swift
let url = URL(...) // A URL object.
let rest = GTRest()

// Set any request HTTP headers:
rest.requestHttpHeaders.add(value: "application/json", forKey: "Accept")

// Set any URL query parameters:
rest.urlQueryParameters.add(value: "2", forKey: "page")

// Make a request.
rest.makeRequest(toURL: url, httpMethod: .get) { [unowned self] (results) in // or [weak self] (results) in
// Access data returned by the server:
if let data = results.data {
// Perform app-specific actions
}

// Access the response:
if let response = results.response {
// Do something with the response object if necessary.
// Checking the HTTP status code :
if (200...299).contains(response.httpStatusCode) {
// Successful request.
} else { ... }
}

// Access the error:
if let error = results.error {
// Do something with the error.
}

// Always update your UI on main thread:
DispatchQueue.main.async {
// Update UI.
}
}
```

## Requirements

iOS 11.0 and above.

## See Also

You might be also interested in [GTNetMon](https://github.com/gabrieltheodoropoulos/GTNetMon), another Swift library to get network status and connection information, and to monitor for network changes.