HttpKit is a Swift library for handling HTTP requests. It provides a convenient way to interact with HTTP endpoints and perform common HTTP operations.
To start using HttpKit in your Swift project, follow these installation instructions:
You can easily add HttpKit to your project by adding it as a dependency in your
Package.swift file:
.package(url: "https://github.com/kemalturk/HttpKit.git", .upToNextMajor(from: "1.0.0"))
In your Swift code, import the HttpKit module to start using it:
import HttpKit
HttpKit is designed to simplify making HTTP requests. Here are some examples of how to use HttpKit in your Swift project:
let BASE_URL = "127.0.0.1:3000"
extension Endpoint {
var scheme: String { "http" }
var host: String { BASE_URL }
}
In the code above, we set the base URL for our API requests.
HttpKit uses an Endpoint
protocol to define API endpoints. Here's an example of how to define movie-related endpoints:
enum MovieEndpoint {
case fetchMovies
case likeMovie(id: String)
case commentMovie(model: CommentMovieRequestModel)
}
extension MovieEndpoint: Endpoint {
var path: String {
switch self {
case .fetchMovies:
return "/movies"
case .likeMovie:
return "/movie/like"
case .commentMovie:
return "/click-event"
}
}
var method: RequestMethod {
switch self {
case .likeMovie, .commentMovie:
return .post
case .fetchMovies:
return .get
}
}
var body: [String : Any]? {
switch self {
case .likeMovie(let id):
return ["id": id]
case .commentMovie(let model):
return model.toDict()
default:
return nil
}
}
}
You can implement an API client using HttpKit to make API requests:
protocol ApiClient {
func fetchMovies() async -> FetchState<[Movie]>
func likeMovie(id: String) async -> FetchState<String>
func commentMovie(model: CommentMovieRequestModel) async -> FetchState<String>
}
struct ApiHttpClient: HttpClient, ApiClient {
func fetchMovies() async -> FetchState<[Movie]> {
await sendRequest(endpoint: MovieEndpoint.fetchMovies).toFetchState()
}
func likeMovie(id: String) async -> FetchState<String> {
await sendRequest(endpoint: MovieEndpoint.likeMovie(id: id)).toFetchState()
}
func commentMovie(model: CommentMovieRequestModel) async -> FetchState<String> {
await sendRequest(endpoint: MovieEndpoint.commentMovie(model: model)).toFetchState()
}
}
@MainActor
class MoviesViewModel: ObservableObject {
@Published var fetchState: FetchState<[Movie]> = .initial
let client: ApiClient
init(client: ApiClient = ApiHttpClient()) {
self.client = client
}
func fetchMovies() {
Task {
fetchState = .loading
fetchState = await client.fetchMovies()
}
}
}
Here are some practical examples of how to use HttpKit to interact with your API:
- Fetch a list of movies:
let client = ApiHttpClient()
let fetchState = await client.fetchMovies()
print(fetchState)
- Like a movie:
let client = ApiHttpClient()
let likeState = await client.likeMovie(id: "123")
print(likeState)
- Comment on a movie:
let client = ApiHttpClient()
let commentModel = CommentMovieRequestModel(/* provide request data */)
let commentState = await client.commentMovie(model: commentModel)
print(commentState)
Contributions to HttpKit are welcome. If you have any suggestions, bug reports, or would like to contribute to the development of HttpKit, please follow the guidelines in the Contributing.md file in the repository.