Swiftpack.co - fummicc1/MoyaAPIClient as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by fummicc1.
fummicc1/MoyaAPIClient 1.0.1
APIClient based on Moya
⭐️ 0
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/fummicc1/MoyaAPIClient.git", from: "1.0.1")

MoyaAPIClient

This library is super simple APIClient based on Moya.

Installation

Add below line to your Package.swift.

.package(url: "https://github.com/fummicc1/MoyaAPIClient", .upToNextMajor(from: "1.0.0")),

and use MoyaAPIClient library.

.product(name: "MoyaAPIClient", package: "MoyaAPIClient"),

Usage

  1. Define your TargetType
import Moya

public enum APIRequest {
    case index(text: String)
}

extension APIRequest: TargetType {
    public var baseURL: URL {
        URL(string: "https://example.com")!
    }

    public var path: String {
        switch self {
        case .index:
            return "/"
        }
    }

    public var method: Moya.Method {
        switch self {
        case .index:
            return .get
        }
    }

    public var task: Moya.Task {
        switch self {
        case let .index(text):
            return .requestParameters(
                parameters: [
                    "text": text
                ],
                encoding: URLEncoding.default
            )
        }
    }

    public var headers: [String : String]? {
        nil
    }
}
  1. Build APIClient with defined Target
let apiClient = APIClientImpl<APIRequest>
// you can also hide concrete type.
// let apiClient: any APIClient<APIRequest> = APIClientImpl<APIRequest>
  1. Call api request

public struct Response: Decodable {
    public let results: [Result]
}

public extension Response {
    struct Result: Decodable {
        // ...
    }
}

let resppnse: Response = try await apiClient.request(with: .index(text: message))

Contributing

Pull requests, bug reports and feature requests are welcome 🚀

License

MIT LICENSE

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

1.0.1
2 weeks ago

What's Changed

  • Create swift.yml
  • Improve APIClientError

Full Changelog: https://github.com/fummicc1/MoyaAPIClient/compare/1.0.0+readme...1.0.1

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics