Contents

cembaykara/SwiftyEndpoint

Easy endpoint building | Lightweight | Type safe

πŸ”§ Installation

Swift Package Manager

You can install the SwiftyEndpoint via Swift Package Manager. - In Xcode, install the SwiftyEndpoint by navigating to File > Add Packages - And enter the GitHub link: ``https://github.com/cembaykara/SwiftyEndpoint.git``

With Packages.swift

Add SwiftyEndpoint to your Package.swift:

dependencies: [ 
	.package(url: "https://github.com/cembaykara/SwiftyEndpoint.git", from: "1.0.0")
]

πŸš€ Usage

1. Define Your Configuration

First, create a configuration that conforms to SwiftyConfiguration:

import SwiftyEndpoint

struct NetworkConfiguration: SwiftyConfiguration {
	
	let host: String? = "api.example.com"
	let port: Int?
	let disableSecureConnection: Bool = false
	
	init(port: Int? = nil) {
		self.port = port
	}
}
2. Create Your Endpoints

Define your endpoints by conforming to SwiftyEndpoint:

enum MoviesEndpoint: SwiftyEndpoint {
	case mostPopular
	case topRated
	
	static let configuration: NetworkConfiguration = NetworkConfiguration()
	
	static var basePath: String { "/api/v2/movies" }
	
	var path: String {
		switch self {
			case .mostPopular: "/most_popular"
			case .topRated: "/top_rated"
		}
	}
}
3. Define Query Parameters (Optional)

Create query parameters by conforming to SwiftyOptions:

enum MovieOptions: SwiftyOptions { 
    case page(Int)
    case language(String) 
    
    func toQueryParameter() -> URLQueryItem { 
        switch self { 
            case .page(let value): URLQueryItem(name: "page", value: String(value))
            case .language(let value): URLQueryItem(name: "language", value: value)
            }
        }
    }
4. Use Your Endpoints
// Basic usage
let popularMoviesURL = MovieEndpoint.mostPopular.url()
// Result: https://api.example.com/api/v2/movies/most_popular

// With query parameters
let options: [MovieOptions] = [.page(1), .language("en-US") ]
let urlWithParams = MovieEndpoint.popular.url(with: options)
// Result: https://api.example.com/api/v2/movies/most_popular?page=1&language=en-US

Base URL Generation

Generate base URLs without endpoint-specific paths:

let baseURL = MovieEndpoint.baseURL()
// Result: https://api.example.com/api/v2/movies

πŸ”¬ Advanced Usage

Custom URL Construction

You can customize URL construction using the url(with:custom:) method:

let customURL = endpoint.url(with: options) { components, path in 
    var modified = components
    modified.path = "/custom" + path

    return modified
}

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

βš–οΈ License

SwiftyEndpoint is released under the Apache License 2.0.

See the LICENSE file for full details.

Package Metadata

Repository: cembaykara/SwiftyEndpoint

Stars: 5

Forks: 0

Open issues: 0

Default branch: master

Primary language: swift

License: Apache-2.0

README: README.md