Git Product home page Git Product logo

webservice's Introduction

WebService

A lightweight Web API for Apple devices, written in Swift 5.x and using Combine.

Features

Features
Request Build your URLRequest to make a call
WebService Main object you can subclass to create an API client.
URLRequestEncodable Protocol If you would like to provide your own request builder

Usage

There are three libraries that you can use.

  1. WebService is the core library that provides the closure base APIs to fetch data. Just import WebService at the top of the Swift file that will interact create the API for web call.
  2. WebServiceCombine provides the Combine based API.
  3. WebServiceConcurrency provides the structured concurrecny based api that is backwards compatiable.
import WebService

You now make your client

class APIClient {
    let webService: WebService

    init(baseURLString: String, session: URLSession = .shared) {
        webService = WebService(baseURLString: baseURLString, session: session)
    }
}

Once you have setup the service you can start to add your APIs for your backend service. This is hypothetical search call to get your search Data

func search(query: String, limit: UInt) -> AnyPublisher<SearchResponse, Error>? {
    let queryParameters: [String: Any] = ["query": query, "limit": limit]
    return webService.GET("/search").setQueryParameters(query)
        .tryMap { (result) -> Data in
            let validData = try result.data.ws_validate(result.response).ws_validateNotEmptyData()
            return validData
    }
    .decode(type: SearchResponse.self, decoder: JSONDecoder())
    .receive(on: queue)
    .eraseToAnyPublisher()
}

Request

If you did not want the service to build your URLRequest you can build your request using the Request structure.

func search<ObjectType: Decodable>(query: String, limit: UInt) -> AnyPublisher<SearchResponse, Error>? {
    let request = Request(.GET, url: webService.baseURLString + "/search")
        .setQueryParameters(query)
        .setHeaders([Request.Header.contentType: Request.ContentType.json])

    return webService.servicePublisher(request: request)
        .tryMap { (result) -> Data in
            let validData = try result.data.ws_validate(result.response).ws_validateNotEmptyData()
            return validData
    }
    .decode(type: ObjectType.self, decoder: JSONDecoder())
    .receive(on: queue)
    .eraseToAnyPublisher()
}

async/await

func search<ObjectType: Decodable>(query: String, limit: UInt) async throws -> SearchResponse {
    let request = Request(.GET, url: webService.baseURLString + "/search")
        .setQueryParameters(query)
        .setHeaders([Request.Header.contentType: Request.ContentType.json])
    return try await webService.decodable(request)
}

webservice's People

Contributors

wmalloc avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.