MoyaAPIClient

1.1.1

APIClient based on Moya
fummicc1/MoyaAPIClient

What's New

1.1.1

2023-08-05T14:33:15Z

Full Changelog: 1.1.0...1.1.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.1.0")),

and use MoyaAPIClient library.

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

Usage

  1. Define your APITarget (APITarget conforms to Moya.TargetType)
import Moya

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

extension APIRequest: APITarget {
    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. Call api request
public struct Response: Decodable {
    public let results: [Result]
}

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

let request: APIRequest = .index(text: "message")
let resppnse: Response = try await request.send()

Contributing

Pull requests, bug reports and feature requests are welcome 🚀

License

MIT LICENSE

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Thu Oct 17 2024 16:15:49 GMT-0900 (Hawaii-Aleutian Daylight Time)