Code Monkey home page Code Monkey logo

swift-network's Introduction

Network

Build & Test GitHub tag (latest SemVer) Swift Package Manager License

A minimal network layer library for performing basic requests.

Requirements

Platforms: iOS 13.0+ / macOS 10.15+

Installation

Swift Package Manager

This library can be added as dependency in your project by using Swift Package Manager, which is integrated into the swift compiler.

You'll need to add the following in your Package.swift file:

Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies value of your Package.swift or the Package list in Xcode.

dependencies: [
    .package(url: "https://github.com/martin-e91/swift-network.git", branch: "main")
]

Mainly you'll want to depend on the interface NetworkAPI target

...
.target(name: "MyPackage", dependencies: [.product(name: "NetworkAPI", package: "swift-network")]),
...

while in the module where you'll need library's concrete types you'll want to depend on the concrete implementation target, Network like so:

...
.target(name: "MyPackage", dependencies: [.product(name: "Network", package: "swift-network")]),
...

System Design

The package has been designed with modularisation in mind. For this reason its components have been split in between 3 exposed targets, that will result in a more flexible and decoupled integration in importing modules.

  • NetworkAPI containing the module's interfaces. This module should be imported wherever you'll need to perform a request leveraging this library.
  • Network: containing concrete implementations of the components. Ideally this module should only be imported in your dependency injection layer, where you'll need to retrieve and inject concrete implementation components (like NetworkClientFactoryImpl).
  • NetworkMocks: this target exposes types for mocking NetworkAPI protocols'.

Usage

Ideally you'd import the Network module in your dependency injection layer, extracting a NetworkClient instance from exposed factory NetworkClientFactoryImpl, like so

struct AppDependencies {
	let networkClient: NetworkClient

	init() {
		...
		let factory = NetworkClientFactoryImpl()
		self.networkClient = factory.networkClient(with: URLSession.shared)
		...
	}
}

After that, let's say you want to hit the PUNK API to retrieve a list of beers.

You'll want to declare a concrete Endpoint

struct PunkBeersEndpoint: Endpoint {
	var scheme: String { "https" }
	var host: String { "api.punkapi.com" }
	var path: PathComponents { ["v2"] }
}

a concrete NetworkRequest

struct GetBeersRequest: NetworkRequest {
	typealias Response = [Beer]

	var method: HTTPMethod { .get }

	var parameters: RequestParameter? { nil }

	var endpoint: Endpoint { PunkBeersEndpoint() }
}

and finally you'll be able to hit the network with just 1 line of code

try await networkClient.perform(GetBeersRequest()) // will hit the network for the endpoint `https://api.punkapi.com/v2/beers`

Contributions

Please feel free to open an issue for requesting a feature or reporting a bug.

swift-network's People

Contributors

martin-e91 avatar

Stargazers

Kevin Ha avatar  avatar

Watchers

 avatar

swift-network's Issues

Add support for different decoding strategies

Description

Add support for different decoding strategies.

Use Case

  • Decoding of data with snake-case keys
  • Decoding of data with a custom date format

Proposed Solution

  • Including a decoder property in the NetworkRequest protocol that allow to specify desired decoder to use when serializing the response. Default value for the property should translate to using a JSONDecoder() instance.

Add support for Headers

Description

Add support for custom headers fields in a request.

  • provide support for a default set of request headers if they're not specified: Accept: application/json and Content-Type: application/json
  • support custom headers for each request

Add CI pipeline

Description

Add a CI pipeline that:

  • runs tests on main branch
  • runs tests on opened pull requests. Additionally set the repo to require that CI checks for merging PRs.

Add CI execution for iOS platforms

Description

Currently the CI is configured to only run on macOS configuration. Adding iOS configuration for running tests will increase code robustness when the library is used for iOS targets.

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.