Printing data requests using a custom URLProtocol – onlinecode

Printing data requests using a custom URLProtocol – onlinecode

In this post we will give you information about Printing data requests using a custom URLProtocol – onlinecode. Hear we will give you detail about Printing data requests using a custom URLProtocol – onlinecodeAnd how to use it also give you demo for it if it is necessary.

Almost all apps contain some kind of data requests. Printing data requests could sometimes be handy for debugging purposes. This can be done fairly easy by using a custom URLProtocol.

Creating a custom URLProtocol

A custom URLProtocol is needed to print out the data requests. A custom implementation of URLProtocol including the canInit method is enough for this case.

Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.
/// A custom protocol for logging outgoing requests.
final class PrintProtocol: URLProtocol {

    override open class func canInit(with request: URLRequest) -> Bool {
        // Print valuable request information.
        print("? Running request: (request.httpMethod ?? "") - (request.url?.absoluteString ?? "")")

        // By returning 'false', this URLProtocol will do nothing less than logging.
        return false
    }
}

The protocol will mostly be ignored as we’re returning false. It’s only used for printing and will not affect outgoing requests.

Enabling the protocol

The protocol needs to be enabled before it starts working. This can be done with a simple line of code:

// Register the custom URL Protocol.
URLProtocol.registerClass(PrintProtocol.self)

Although this is often enough to make it work, it’s sometimes needed to create your own custom session configuration. This also counts for usage with Alamofire.

let configuration = URLSessionConfiguration.default
configuration.protocolClasses?.insert(PrintProtocol.self, at: 0)
let sessionManager = Alamofire.SessionManager(configuration: configuration)
sessionManager.request("https://www.onlinecode.org/feed/")

For more information about advanced usage with Alamofire, checkout their documentation.

Try it out yourself

? Running request: GET - https://www.onlinecode.org/feed/
✅ Request completed

Using this playground code gives you the above output:

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

/// A custom protocol for logging outgoing requests.
final class PrintProtocol: URLProtocol {

    override open class func canInit(with request: URLRequest) -> Bool {
        // Print valuable request information.
        print("? Running request: (request.httpMethod ?? "") - (request.url?.absoluteString ?? "")")

        // By returning 'false', this URLProtocol will do nothing less than logging.
        return false
    }
}

// Register the custom URL Protocol.
URLProtocol.registerClass(PrintProtocol.self)

// Execute a data request.
URLSession.shared.dataTask(with: URL(string: "https://www.onlinecode.org/feed/")!) { (_, _, _) in
    print("✅ Request completed")
}.resume()
Architecting SwiftUI apps with MVC and MVVMAlthough you can create an app simply by throwing some code together, without best practices and a robust architecture, you’ll soon end up with unmanageable spaghetti code. Learn how to create solid and maintainable apps with fewer bugs using this free guide.

An advanced usage of a custom URLProtocol

This is just a simple example of what you can do with a custom URLProtocol. More advanced implementations enable you to for example mock your unit tests. You can checkout WeTransfer’s Mocker framework for some inspiration.

 

Hope this code and post will helped you for implement Printing data requests using a custom URLProtocol – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US