Swiftpack.co -  AnasAlhasani/CoreNetwork as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
AnasAlhasani/CoreNetwork
Protocol oriented networking layer on top of Alamofire
.package(url: "https://github.com/AnasAlhasani/CoreNetwork.git", from: "v1.0.8")

CoreNetwork

GitHub tag (latest SemVer) Codebeat

Protocol oriented networking layer on top of Alamofire

Used Libraries

  • Alamofire: Elegant HTTP Networking in Swift.
  • Google/Promises: a modern framework that provides a synchronization construct for Objective-C and Swift to facilitate writing asynchronous code.

Examples

1. Entities:

  • Responses
struct Book: Decodable {
    var id: String
    var title: String
    var author: String
    var releaseDate: Date?
    var pages: Int
}

struct VoidResponse: Decodable {
    // Some endpoints might return: 204 No Content
}
  • Parameters
struct AddBookParameters: Encodable {
    var title: String
    var author: String
    var releaseDate: Date?
    var pages: Int
}

2. UseCase:

protocol BooksUseCase {
    func loadBooks() -> Promise<[Book]>
    func addBook(_ parameters: AddBookParameters) -> Promise<Book>
    func deleteBook(_ book: Book) -> Promise<VoidResponse>
}

final class APIBooksUseCase {
    
    private let apiClient: APIClient
    
    // Automatic configurations
    init(apiClient: APIClient = DefaultAPIClient()) {
        self.apiClient = apiClient
    }
}

extension APIBooksUseCase: BooksUseCase {
    func loadBooks() -> Promise<[Book]> {
        let request = RequestBuilder<[Book]>()
            .path("books")
            .method(.get)
            .build()
        
        return apiClient.execute(request)
    }
    
    func addBook(_ parameters: AddBookParameters) -> Promise<Book> {
        let request = RequestBuilder<Book>()
            .path("books")
            .method(.post)
            .encode(parameters, bodyEncoding: .jsonEncoding)
            .build()
        
        return apiClient.execute(request)
    }
    
    func deleteBook(_ book: Book) -> Promise<VoidResponse> {
        let request = RequestBuilder<VoidResponse>()
            .path("books/\(book.id)")
            .method(.delete)
            .build()
        
        return apiClient.execute(request)
    }
}

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate CoreNetwork into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'CoreNetwork', :git => "https://github.com/AnasAlhasani/CoreNetwork"

Then, run the following command:

$ pod install

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding CoreNetwork as a dependency is as easy as adding it to the dependencies value of your Package.swift.

Swift 4

dependencies: [ 
    .package(url: "https://github.com/AnasAlhasani/CoreNetwork.git", from: "v1.0.7")
]

Author

Anas Alhasani

Twitter Follow

GitHub Follow

GitHub

link
Stars: 5
Last commit: 6 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Related Packages

Release Notes

Fixed Alamofire manager
42 weeks ago

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