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 1.0.3
This package supports the network layer based on URLSession with REST API request with JSON response.
⭐️ 0
🕓 23 hours ago
.package(url: "https://github.com/urjhams/Networking.git", from: "1.0.3")


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 smth with the data
  case .failure(let error):
    // do smth with the error

// get a decoded json object from a request task
  from: postRequest
) { result in
  switch result {
  case .success(let sample):
    // do smth 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 smth with the success model with type `Sample`
  // to simply get Data and HttpResponse:
  let (data, response) = try await networking.sendRequest(postRequest)

Combine usage

var subcriptions = 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: &subcriptions)

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 availibility state, and we can stack a handle from everywhere in the project.

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



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

Release Notes

Combine support

This patch includes:

  • Support Combine framework use case.
  • Rename the getObjectVia(_:) functions that need a specific generic typecasting from the JSON response. To avoid ambiguity, these functions now require a Codable type as the parameter. for example, to receive the except value of type Sample, we call get(Sample.self, from: request).

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