Swiftpack.co - urjhams/Networking as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by urjhams.
urjhams/Networking back-up
This package supports the network layer based on URLSession with REST API request with JSON response.
⭐️ 0
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/urjhams/Networking.git", from: "back-up")


Formalized API requests calling by using URLSession.

Swift 5 Platform iOS Platform iPadOS Platform watchOS Platform macOS Platform tvOS Platform macCatalyst License: MIT


Swift Package Manager

dependencies: [
            url: "git@github.com:urjhams/Networking.git",
            from: "1.0.0"


Get the Networking instance

let networking = Network.shared

The Request object

With any Http request call, a single request object is used as the input. Create a Request object will automatically prepare the neccessary components for the request to use like header, parameter, etc. We can then later extract an URLRequest from this Request object.

let postRequest = Request(
    from: "https://api.m3o.com/v1/helloworld/Call",
    as: .post,
    authorization: .bearerToken(
      token: "YzhiYmFlNTUtNDE2Mi00MDk5LTg1Y2UtNmNmZDFmMWE1MzY2"
    parameters: ["name" : "Quan"]

Standard call with callbacks

// send a request with call back
networking.sendRequest(postRequest) { result in
  switch result {
  case .success(let data):
    // do something with the data
  case .failure(let error):
    // do something with the error

// get a decoded json object from a request task
  from: postRequest
) { result in
  switch result {
  case .success(let sample):
    // do something with the success model with type `Sample`
  case .failure(_):

Concurrency with async / await (available from iOS 13+, watchOS 6+, macOS 11+)

get(_:, from:) has a supported version for concurrency. All we need to do is declare the output with the expected Type for the generic requirement and apply try await.

  let sample = try await networking.get(Sample.self, from: postRequest)
  // do something with the success model with type `Sample`
  // to simply get Data and HttpResponse:
  let (data, response) = try await networking.sendRequest(postRequest)

Combine usage

var subscription = Set<AnyCancellable>()
  .publisher(for: Sample.self, from: postRequest)
  .sink { completion in
    switch completion {
    case .finished:
    case .failure(let error):
      print("got an error: \(error.localizedDescription)")
  } receiveValue: { sample in
    // do something with the data received
  .store(in: &subscription)

Connectivity Observing

from iOS 12.0+, macOS 10.14+, the connectivity can be monitor via the monitor object. This object is a static object. All we need to do is append the desired handle via Network.Connectivity.monitorChangeHandlers static property. This stack up a list of handles we want call whenever there is a change of each network availability state, and we can stack a handle from everywhere in the project.

let handler: Networking.Connectivity.Handler  = { state in
  switch state {
  case .available:
    // do something
  case .unavailable:
    // do something
  case .noConnection:
    // do something



Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Plain text keyword signature in header
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics