Giter Site home page Giter Site logo

swift_genericapilayer's Introduction

Swift_GenericAPILayer

A simple generic API layer implemented in Swift. Good solution to avoid external dependencies in small/midsize projects.

Usage:

  • Instantiate APICore
let api: APIProvider = APICore()
  • Access the generic request method passing your parameters
let urlPath: [PathURL] = [.base, .myPath, .variable("dynamicValue")]
        
api.request(url: urlPath, httpMethod: .get, body: nil) { (result: Result<Object, ErrorHandler>) in
    switch result {
    case .success(let value):
    // handle value (as your custom Object or [Object])
    case .failure(let error):
    // handle error.message (as String)
    }
}

Feel free to customize it adding your own

URL Paths (APIPaths.swift):

enum PathURL {
    case base
    case myPath
    case variable(String)
}

extension PathURL: RawRepresentable {
    public typealias RawValue = String
    public init?(rawValue: RawValue) { switch rawValue { default: return nil } }
    
    public var rawValue: RawValue {
        switch self {
        case .base: return "http://myUrlBasePath.com/"
        case .myPath: return "myPath/"
        case .variable(let value): return value
        }
    }    
}

Custom errors (APIError.swift):

enum NetworkErrors: String {
    case `default` = "Default message error"
    case connection = "Connection message error"
    case json = "Json message error"
}

Http methods (APICore.swift):

enum HttpMethod: String {
    case get = "GET"
    case post = "POST"
    case put = "PUT"
    case patch = "PATCH"
}

TODO

  • Add API Stub with Unit test sample

swift_genericapilayer's People

Contributors

victorpanitz avatar

Stargazers

Michel Anderson Lütz Teixeira avatar

Watchers

Michel Anderson Lütz Teixeira avatar James Cloos 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.