SkillAgentSearch skills...

Harbor

Harbor is a library for making API requests in Swift in a simple way using async/await.

Install / Use

/learn @javiermanzo/Harbor
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<p align="center" width="100%"> <img width="40%" src="https://raw.githubusercontent.com/javiermanzo/Harbor/main/Resources/Harbor.png"> </p>

Release CI Swift Platforms Swift Package Manager(https://swiftpackageindex.com/javiermanzo/Harbor) CocoaPods Compatible

Harbor is a library for making API requests in Swift in a simple way using async/await.

Table of Contents

Features

  • [x] Rest Requests
  • [x] JSON RPC Requests
  • [x] Auth provider handler
  • [x] Multipart Post Requests
  • [x] Retry Requests
  • [x] Cancel Request
  • [x] Debug Requests
  • [x] cURL Command Output
  • [x] Default Headers
  • [x] Custom URLSession
  • [x] mTLS Certificate
  • [x] SSL Pinning
  • [x] Swift 6 Compatible
  • [x] Mock Requests

Requirements

  • Swift 5.9+
  • iOS 15.0

Installation

You can add Harbor to your project using CocoaPods or Swift Package Manager.

CocoaPods

Add the following line to your Podfile:

pod 'Harbor'

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/javiermanzo/Harbor.git")
]

Usage

Configuration

This provides a centralized way to manage common configuration.

Default Headers

You can include default headers in every request.

To configure the default headers:

await Harbor.setDefaultHeaderParameters([
    "MY_CUSTOM_HEADER": "VALUE"
])

Auth Provider

You can implement the HAuthProviderProtocol if you need to handle authentication. Use the setAuthProvider method of the Harbor class to set the authentication provider.

You need to create a class that implements HAuthProviderProtocol:

class MyAuthProvider: HAuthProviderProtocol {
    func getAuthorizationHeader() async -> HAuthorizationHeader {
        // Return a HAuthorizationHeader instance
    }
    
    func authFailed() async {
        // This method is called when the request receives a 401 status code
    }
}

After that, set your Auth provider:

await Harbor.setAuthProvider(MyAuthProvider())

If the request class has the needsAuth property set to true, Harbor will call the getAuthorizationHeader method of the authentication provider to get the HAuthorizationHeader instance to set it in the header before executing the request.

Custom URLSession

Harbor allows you to set a custom URLSession for your requests, providing flexibility for advanced configurations such as custom caching, timeout settings, or additional protocols.

To set a custom URLSession, use the setCustomURLSession method:

let customSession = URLSession(configuration: .default)
await Harbor.setCustomURLSession(customSession)

mTLS Support

Harbor supports mutual TLS (mTLS) for enhanced security in API requests. This feature allows clients to present certificates to the server, ensuring both the client and server authenticate each other.

To set up mTLS, use the setMTLS method:

let mTLS = HmTLS(p12FileUrl: yourP12FileUrl, password: "yourPassword")
await Harbor.setMTLS(mTLS)

SSL Pinning

Harbor supports SSL Pinning to enhance the security of your API requests. SSL Pinning ensures that the client checks the server's certificate against a known pinned certificate, adding an additional layer of security.

To configure SSL Pinning, use the setSSlPinningSHA256 method:

let sslPinningSHA256 = "yourSHA256CertificateHash"
await Harbor.setSSlPinningSHA256(sslPinningSHA256)

Request Protocols

To make a request using Harbor, you need to create a class that implements one of the following protocols.

HGetRequestProtocol

Use the HGetRequestProtocol protocol if you want to send a GET request.

Extra Properties:
  • queryParameters: A dictionary of query parameters that will be added to the URL.
  • Model: The result of the request will be parsed to this entity.

HPostRequestProtocol

Use the HPostRequestProtocol protocol if you want to send a POST request.

Extra Properties:
  • bodyParameters: A dictionary of parameters that will be included in the body of the request.
  • bodyType: Specifies the type of data being sent in the body of the request. It can be either json or multipart.

HPatchRequestProtocol

Use the HPatchRequestProtocol protocol if you want to send a PATCH request.

Extra Properties:
  • bodyParameters: A dictionary of parameters that will be included in the body of the request.
  • bodyType: Specifies the type of data being sent in the body of the request. It can be either json or multipart.

HPutRequestProtocol

Use the HPutRequestProtocol protocol if you want to send a PUT request.

Extra Properties:
  • bodyParameters: A dictionary of parameters that will be included in the body of the request.
  • bodyType: Specifies the type of data being sent in the body of the request. It can be either json or multipart.

HDeleteRequestProtocol

Use the HDeleteRequestProtocol protocol if you want to send a DELETE request.

HRequestWithResultProtocol

Use the HRequestWithResultProtocol protocol if you want to parse the response into a specific model. This protocol requires you to define the type of model you expect in the response.

Extra Properties:
  • Model: The result of the request will be parsed to this entity.

Request Calling

Once the request class is created, you can execute the request using the request method.

Task {
    let response = await MyRequestWithResult().request()
}

Response

HResponse

If you use a protocol different from HGetRequestProtocol or HRequestWithResultProtocol, the result of calling request() will be an HResponse enum.

switch response {
case .success:
    break
case .error(let error):
    break
}

HResponseWithResult

If you use HGetRequestProtocol or HRequestWithResultProtocol, the result of calling request() will be an HResponseWithResult enum.

switch response {
case .success(let result):
    break
case .error(let error):
    break
}

Cancel Request

You can cancel the task of the request if it is running. request() will return cancelled as an error case.

let task = Task {
    let response = await MyRequestWithResult().request()
}
task.cancel()

Debug

You can print debug information about your request using the HDebugRequestProtocol protocol. Implement the protocol in the request class.

class MyRequest: HRequestWithResultProtocol, HDebugRequestProtocol {
    var debugType: HDebugRequestType = .requestAndResponse
    
    // ...
}

debugType defines what you want to print in the console. The options are none, request, response, or requestAndResponse.

When your request is called, you will see in the Xcode console the information about your request.

JSON RPC

Harbor also supports JSON RPC via the HarborJRPC package.

Installation

To use HarborJRPC, add the following import to your file:

import HarborJRPC

Configuration

Set URL

Use this method to set the URL for the JSON RPC requests:

HarborJRPC.setURL("https://api.example.com/")

Set JSON RPC Version

Use this method to set the JSON RPC version:

// It uses 2.0 as default 
HarborJRPC.setJRPCVersion("2.0")

Request Protocol

HJRPCRequestProtocol

Use the HJRPCRequestProtocol protocol if you want to send a JRPC request.

Properties:
  • Model: The model that conforms to the Codable protocol, representing the expected response structure.
  • method: A string that represents the JRPC method to be called.
  • needsAuth: A boolean indicating whether the request requires authentication.
  • retries: The number of retries in case the request fails.
  • headers: An optional dictionary con
View on GitHub
GitHub Stars18
CategoryDevelopment
Updated5mo ago
Forks1

Languages

Swift

Security Score

92/100

Audited on Oct 23, 2025

No findings