Skip to content

miletliyusuf/YMNetwork

Repository files navigation

YMNetwork

HTTP Networking library written in Swift.

YMNetwork

Swift Version License Carthage compatible CocoaPods Compatible
Platform PRs Welcome

Requirements

  • iOS 15.0
  • Xcode 11
  • Swift 5.0

Usage

YMNetwork has been designed to create HTTP requests very easily.

IMPORTANT NOTE: If you're implementing via Cocoapods you need to import framework as import YMNetwork_Swift and for Carthage import YMNetwork is enough. Please see Installation for detail informations. Also check Example Project.

Features

Mapping

Mapping is super easy in YMNetwork. You just need to conform your response struct to YMResponse and model to YMModel.

struct MovieResponse: YMResponse {

    private enum CodingKeys: String, CodingKey {

        case page
        case numberOfResults = "total_results"
        case numberOfPages = "total_pages"
        case movies = "results"
    }

    let page: Int
    let numberOfResults: Int
    let numberOfPages: Int
    let movies: [Movie]
}

// MARK: - Movie

struct Movie: YMModel {

    let id: Int
    let posterPath: String
    let backdrop: String
    let title: String
    let releaseDate: String
    let rating: Double
    let overview: String

    enum CodingKeys: String, CodingKey {
        case id
        case posterPath = "poster_path"
        case backdrop = "backdrop_path"
        case title
        case releaseDate = "release_date"
        case rating = "vote_average"
        case overview
    }
}

Download Management

Create your download requests using YMDownloadRequest. If you conform YMNetworkManagerDownloadDelegate in your class you will be able to manage and see progress all your downloads on

func ymNetworkManager(_ manager: YMNetworkManager, request: YMDownloadRequest?, downloadTask: URLSessionDownloadTask)

You can also pause, resume or cancel your download tasks.

private let manager = YMNetworkManager(
        configuration: YMNetworkConfiguartion(
            baseURL: environment.baseURL,
            headers: [:]
        )
    )

// Start Download
try? manager.shared.download(with: &request)

// Pause Download
manager.pauseDownloadTask(of: request)

// Resume Download
manager.resumeDownloadTask(of: request) { (status, error) in
            completion(status, error)
        }

// Cancel Download
manager.cancelDownloadTask(of: request)

HTTP Requests

You can manage your request requirements on your request class by conforming it to YMRequest.

All possible values are:

var path: String { get }
var method: HTTPMethod { get }
var headers: HTTPHeaders? { get }
var bodyParameters: Parameters? { get }
var urlParameters: Parameters? { get }
var task: HTTPTaskType { get }

Simple usage:

struct MovieRequest: BaseRequest {

    var path: String = "now_playing"
    var urlParameters: Parameters?

    init(page: Int) {

        urlParameters?["page"] = page
    }
}

// Send request

private let manager = YMNetworkManager(
        configuration: YMNetworkConfiguartion(
            baseURL: environment.baseURL,
            headers: [:]
        )
    )
    
let request = MovieRequest(page: 1)

manager.request(request) { (response, result: YMResult<T>, error) in

    if error != nil {
	completion(nil, "Please check your network connection")
    }

    switch result {
    case .success(let data):
	completion(data, nil)
    case .failure(let error):
	completion(nil, error.rawValue)
    }
}

File Upload

You can upload your files by passing it's file url into YMUploadRequest.

struct UploadRequest: YMUploadRequest {

    var fileURL: URL?
    var path: String = "post"
    
    init(fileURL: URL?) {
    	
	self.fileURL = fileURL
    }
}

let request = UploadRequest(fileURL: Bundle.main.url(forResource: "cat", withExtension: "jpg"))

private let manager = YMNetworkManager(
        configuration: YMNetworkConfiguartion(
            baseURL: environment.baseURL,
            headers: [:]
        )
    )
    
manager.request(request) { (response, result: YMResult<T>, error) in

    if error != nil {
	completion(nil, "Please check your network connection")
    }

    switch result {
    case .success(let data):
	completion(data, nil)
    case .failure(let error):
	completion(nil, error.rawValue)
    }
}

Installation

CocoaPods

Well.. YMNetwork is already taken for cocoapods. You should be installing pod as pod 'YMNetwork-Swift' but when you import framework in your code please just use import YMNetwork_Swift

Check out Get Started tab on cocoapods.org.

To use YMNetwork in your project add the following 'Podfile' to your project

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '15.0'
use_frameworks!

pod 'YMNetwork-Swift'

Then run:

pod install

Carthage

Check out the Carthage docs on how to add a install. The YMNetwork framework is already setup with shared schemes.

Carthage Install

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate YMNetwork into your Xcode project using Carthage, specify it in your Cartfile:

github "miletliyusuf/YMNetwork"

Swift Package Manager

https://github.com/miletliyusuf/YMNetwork

TODO

  • Better Upload Management
  • Image caching
  • Better support for URLSession configurations
  • Upload with Data
  • Better Documentation

Meta

Yusuf Miletli – Linkedin, [email protected]

Distributed under the MIT license. See LICENSE for more information.

https://github.com/miletliyusuf/YMNetwork