Coder Social home page Coder Social logo

sservicelibrary's Introduction

ServiceLayer

This is a protocol oriented network layer using URLSession. My intention its to continue improving and completing more features. For now it create Data task to create and receive data. Its orchested using 3 subsystems having as core URLSession.

The URL Loading System provides access to resources identified by URLs, using standard protocols like https or custom protocols you create. Loading is performed asynchronously, so your app can remain responsive and handle incoming data or errors as they arrive.''

I took in consideration for this the types of Request Methods and design a fluid api that are easy to understand and have their implementation applied to standards very close as open api specification. For example a POST request with body can be of this 4 types.

  • form-data
  • x-www-form-urlencoded
  • binary
  • text ( json for example )

So expressing this on Request i propose a SLRequest wich will be

    let person = Employee(name: "Jhon", salary: "430000", age: "45")
    let personRequest = SLRequest(requestType: .body(.json(person)),
                                                         serviceName: "create")

this is the constructor signature

    init(requestType: SLParameterType, method: SLHTTPMethod = .GET, serviceName: String) 

Where RequestType is :

enum SLParameterType {
    case body(SLBodyParameterEncodingType)
    case requestURL([String: String])
    
    enum SLBodyParameterEncodingType {
        case formdata([String: Any])
        case urlencoded([String: String])
        case json(Encodable)
        
        var headerValue: Dictionary<SLHeaderField, String> {
            switch self {
            case .urlencoded:
                return [.contentType: "application/x-www-form-urlencoded"]
            case .formdata:
                return [.contentType: "multipart/form-data"]
            case .json:
                return [.contentType: "application/json"]
            }
        }
    }
}

easy right ?. By this im creating a request wichh will be with body ( become automatically in POST. Take a look to body having an asociated type SLBodyParameterEncodingType

    enum SLBodyParameterEncodingType {
        case formdata([String: Any])
        case urlencoded([String: String])
        case json(Encodable)
        
        var headerValue: Dictionary<SLHeaderField, String> {
            switch self {
            case .urlencoded:
                return [.contentType: "application/x-www-form-urlencoded"]
            case .formdata:
                return [.contentType: "multipart/form-data"]
            case .json:
                return [.contentType: "application/json"]
            }
        }
}

All together.

Asume we want to create an employee in the given url.

 -> "https://aws4h.example.com/api/v1/create"

Code make look like this.

        let person = Employee(name: "Jhon", salary: "430000", age: "45")
        
        let dispatcher = SLRequestDispatcher(env: APIEnv.dev) //1
        
        let personRequest = SLRequest(requestType: .body(.json(person)),
                                                         serviceName: "create") //2
        
        let operation = SLOperation(personRequest) //3
        
        operation.execute(in: dispatcher) { response in //4
            // do something with response
        }

Basically the idea is separate concerns about request, who make it and who perform it. 1 - Dispatcher wrap network session with the environment. You can pass custom SLNetworkSessionProtocol type if you want special behaviour. 2 - Create the request 3 - Pass the request to the operation 4 - Execute the operation on the dispatcher.

Thats all. Thank you so much, this is a first try of course theres a lot of improvements.

Install

In Xcode : File/Add Packages .In the dialog showing available packages put this repo url in the upper right corner and type enter

sservicelibrary's People

Contributors

bolekhub avatar

Watchers

 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.