Dead-simple HTTP Networking in Swift using URLSession.
GampangHTTP can be installed using Swift Package Manager. Add it to your project's Package.swift
file:
dependencies: [
.package(url: "https://github.com/mrandika/swift-GampangHTTP.git", from: "x.x.x")
]
Then, add GampangHTTP to your target's dependencies:
targets: [
.target(
name: "YourTarget",
dependencies: ["GampangHTTP"]),
]
- Import GampangHTTP in your Swift file:
import GampangHTTP
- Create an instance of GampangHTTP:
let http = GampangHTTP()
- Make a request:
struct HttpBin: Decodable {
let origin: String
let url: String
}
do {
let response: HttpBin = try await http.request(
with: URLRequest(url: URL(string: "https://httpbin.org/get")!),
of: HttpBin.self
)
print("Response: \(response.origin) \(response.url)")
} catch {
print("Error: \(error)")
}
You can customize GampangHTTP with various options:
let customHTTP = GampangHTTP(
cache: URLCache(memoryCapacity: 20_000_000, diskCapacity: 200_000_000, diskPath: "custom_cache"),
logger: CustomLogger(),
retryPolicy: CustomRetryPolicy(),
pinnedCertificates: [/* Your pinned certificates */]
)
GampangHTTP uses GampangURLRequest
to construct HTTP requests. This struct provides a convenient way to set various components of a request.
let request = GampangURLRequest(url: "https://api.example.com/users", method: .get)
let postRequest = GampangURLRequest(url: "https://api.example.com/users", method: .post)
Available HTTP methods:
.post
.get
.put
.delete
.patch
.head
.options
.trace
.connect
Reference: HTTP request methods
let request = GampangURLRequest(
url: "https://api.example.com/users",
method: .get,
headers: [
(.contentType, "application/json"),
(.authorization, "Bearer your-token-here")
]
)
Available header fields:
.contentType
.authorization
.accept
💡 More header fields and custom are coming soon.
let request = GampangURLRequest(
url: "https://api.example.com/search",
method: .get,
queryItems: [
URLQueryItem(name: "q", value: "swift"),
URLQueryItem(name: "page", value: "1")
]
)
For requests with a JSON body:
let body: [String: Any] = ["name": "John Doe", "email": "john@example.com"]
let request = GampangURLRequest(
url: "https://api.example.com/users",
method: .post,
body: body
)
For requests with raw data:
Alternatively, you can use Encodable protocol and convert it to data representation
let data = "Raw data".data(using: .utf8)!
let request = GampangURLRequest(
url: "https://api.example.com/upload",
method: .post,
data: data
)
You can combine all these components in a single request:
let request = GampangURLRequest(
url: "https://api.example.com/users",
method: .post,
body: ["name": "John Doe", "email": "john@example.com"],
queryItems: [URLQueryItem(name: "version", value: "v2")],
headers: [
(.contentType, "application/json"),
(.authorization, "Bearer your-token-here")
]
)
Once you've constructed your request, you can use it with GampangHTTP:
let http = GampangHTTP()
do {
let urlRequest = try request.build
let response: APIResponse = try await http.request(with: urlRequest, of: APIResponse.self)
print("Response: \(response)")
} catch {
print("Error: \(error)")
}
Note: The build property of GampangURLRequest converts it to a URLRequest, which can be used with GampangHTTP.
GampangHTTP supports response caching out of the box. To disable caching:
let httpNoCache = GampangHTTP(cache: nil)
Enable certificate pinning by providing an array of pinned certificates:
let httpWithPinning = GampangHTTP(pinnedCertificates: [/* Your pinned certificates */])
GampangHTTP uses a default retry policy. You can provide a custom policy:
class CustomRetryPolicy: GampangRetryPolicy {
// Implement your custom retry logic here
}
let httpWithCustomRetry = GampangHTTP(retryPolicy: CustomRetryPolicy())
GampangHTTP includes a default console logger. You can provide a custom logger:
class CustomLogger: GampangLogger {
func log(_ message: String) {
// Implement your custom logging logic here
}
}
let httpWithCustomLogger = GampangHTTP(logger: CustomLogger())