Requestify

1.2.1

insub4067/Requestify

What's New

1.2.1

2024-09-22T09:02:02Z

🛜 Requestify

Platform

Requestify is a flexible and reusable network request utility built using Alamofire. It allows developers to easily construct and send HTTP requests with customizable methods, headers, parameters, and logging options.

Features

  • Simple, chainable interface to set URL, HTTP method, headers, and parameters.
  • Supports request and response logging for easier debugging.
  • Handles both JSON-encoded parameters and decodable responses.
  • Utilizes Swift's async/await pattern for modern concurrency support.
  • Supports multiple HTTP methods (GET, POST, PUT, DELETE, etc.).
  • Allows for raw HTTP responses without decoding.
  • Easily customizable for different API configurations.
  • Supports multipart form data uploads, including images and JSON objects.

Usage

Creating a Request

To create a request, use the RequestBuilder struct by chaining configuration methods:

import Requestify

let requestify = Requestify()
    .setURL("https://jsonplaceholder.typicode.com/posts")
    .setMethod(.get)
    .setPrintLog(true)

Sending a Request

Once your request is configured, send the request and handle the response using Swift's async/await:

struct Post: Codable {
    let userId: Int
    let id: Int
    let title: String
    let body: String
}

Task {
    do {
        let posts: [Post] = try await requestify.request([Post].self)
        print("Posts: \(posts)")
    } catch {
        print("Error: \(error)")
    }
}

Customizing Headers and Parameters

You can send custom headers and parameters with your request:

let customHeaders: HTTPHeaders = [
    "Authorization": "Bearer token"
]

let parameters = Params()
    .add("userId", value: 1)
    .build()

let requestify = Requestify()
    .setURL("https://jsonplaceholder.typicode.com/posts")
    .setMethod(.post)
    .setHeaders(customHeaders)
    .setParameters(parameters)
    .setPrintResponse(false)

Handling Raw HTTP Responses

If you need a raw HTTPURLResponse without decoding the body, use:

Task {
    do {
        let response = try await requestify.requeust()
        print("Response status code: \(response.statusCode)")
    } catch {
        print("Error: \(error)")
    }
}

Multipart Uploads (iOS only)

For uploading files or images, Requestify provides an easy interface for creating multipart requests:

import UIKit

let post = Post()

let requestify = Requestify()
    .setURL("https://yourapi.com/upload")
    .setMethod(.post)
    .addObject(post, withName: "post")
    .addImages([UIImage(named: "example")], withName: "file")
    .setPrintResponse(false)

Task {
    do {
        let response: YourResponseType = try await requestify.upload(YourResponseType.self)
        print("Upload successful: \(response)")
    } catch {
        print("Error: \(error)")
    }
}

Logging

You can control the request and response logging using setPrintLog and setPrintResponse:

.setPrintLog(true)
.setPrintResponse(false)

Description

  • Swift Tools 5.4.0
View More Packages from this Author

Dependencies

Last updated: Wed May 14 2025 23:41:26 GMT-0900 (Hawaii-Aleutian Daylight Time)