GithubHelp home page GithubHelp logo

whateverbest / senetworking Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kudoleh/senetworking

0.0 0.0 0.0 4.15 MB

Simple Codable NSURLSession wrapper, ideal for new or existing projects and code challenges. Works with Codable, Decodable for responses and Encodable for requests. Used in Clean Architecture.

Home Page: https://tech.olx.com/clean-architecture-and-mvvm-on-ios-c9d167d9f5b3

License: MIT License

Ruby 2.29% Swift 97.71%

senetworking's Introduction

SENetworking

CocoaPods Carthage compatible Swift Package Manager compatible Swift 5 Platforms License

Super Easy Networking is simple and convenient wrapper around NSURLSession that supports common needs. A framework that is small enough to read in one go but useful enough to include in any project. It is fully tested framework for iOS, tvOS, watchOS and OS X.

  • Super Minimal and Light implementation
  • Easy network configuration
  • Works with Decodable for responses and Encodable for Requests
  • Friendly API which makes declarations of Endpoints super easy
  • Easy use of Data Trasfer Objects and Mappings
  • No Singletons
  • No external dependencies
  • Simple request cancellation
  • Optimized for unit testing
  • Fully tested
  • Ideal for code challenges

Example

Endpoint definitions:

struct APIEndpoints {
    static func getMovies(with moviesRequestDTO: MoviesRequest) -> Endpoint<MoviesResponse> {
        return Endpoint(path: "search/movie/",
                        method: .get,
                        headerParamaters: ["Content-Type": "application/json"], // Optional
                        queryParametersEncodable: moviesRequestDTO)
    }
}

API Data (Data Transfer Objects):

struct MoviesRequest: Encodable {
    let query: String
    let page: Int
}

struct MoviesResponse: Decodable {
    struct Movie: Decodable {
        private enum CodingKeys: String, CodingKey {
            case title
            case overview
            case posterPath = "poster_path"
        }
        let title: String
        let overview: String
        let posterPath: String
    }

    private enum CodingKeys: String, CodingKey {
        case movies = "results"
    }
    let movies: [Movie]
}

API Networking Configuration:

struct AppConfiguration {
    var apiKey: String = "xxxxxxxxxxxxxxxxxxxxxxxxx"
    var apiBaseURL: String = "http://api.themoviedb.org"
}

class DIContainer {
    static let shared = DIContainer()

    lazy var appConfiguration = AppConfiguration()

    lazy var apiDataTransferService: DataTransferService = {
        let config = ApiDataNetworkConfig(baseURL: URL(string: appConfiguration.apiBaseURL)!,
                                          queryParameters: ["api_key": appConfiguration.apiKey,
                                                            "language": NSLocale.preferredLanguages.first ?? "en"])

        let apiDataNetwork = DefaultNetworkService(config: config)
        return DefaultDataTransferService(with: apiDataNetwork)
    }()
}

Making API call:

let endpoint = APIEndpoints.getMovies(with: MoviesRequest(query: "Batman Begins", page: 1))
dataTransferService.request(with: endpoint) { result in

    guard case let .success(response) = result, let movies = response.movies else { return }

    self.show(movies)
}

Installation

CocoaPods: To install it with CocoaPods, simply add the following line to your Podfile:

pod 'SENetworking'

Then pod install and import SFNetworking in files where needed

Carthage: To install it with Carthage, simply add the following line to your Cartfile:

github "kudoleh/SENetworking"

Then carthage update and import SFNetworking_iOS in files where needed (e.g. for iOS platform)

Swift Package Manager: To install it with Package Manager:

Xcode tab: File -> Swift Packages -> Add Package Dependency 
Enter package repository URL: https://github.com/kudoleh/SENetworking

And then import SFNetworking in files where needed

Manual installation: To manually install it:

Copy folder SENetworking into your project

Author

Oleh Kudinov, [email protected]

License

MIL License, Open Source License

senetworking's People

Contributors

kudoleh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.