Skip to content

Commit

Permalink
Custom body export (#17)
Browse files Browse the repository at this point in the history
* Add customization for body export

* Set delegate for vc

* Add delegate to shared instance

* Add method for request body export

Co-authored-by: Pouya Yarandi <[email protected]>
  • Loading branch information
pouyayarandi and Pouya Yarandi authored Jun 11, 2022
1 parent 64cc502 commit 7675afd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
14 changes: 13 additions & 1 deletion Sources/NetShears/NetShears.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@

import UIKit

public protocol BodyExporterDelegate: AnyObject {
func netShears(exportResponseBodyFor request: NetShearsRequestModel) -> BodyExportType
func netShears(exportRequestBodyFor request: NetShearsRequestModel) -> BodyExportType
}

public extension BodyExporterDelegate {
func netShears(exportResponseBodyFor request: NetShearsRequestModel) -> BodyExportType { .default }
func netShears(exportRequestBodyFor request: NetShearsRequestModel) -> BodyExportType { .default }
}

public final class NetShears: NSObject {

public static let shared = NetShears()
public weak var bodyExportDelegate: BodyExporterDelegate?
internal var loggerEnable = false
internal var interceptorEnable = false
internal var listenerEnable = false
Expand Down Expand Up @@ -69,10 +80,11 @@ public final class NetShears: NSObject {
return config.removeModifier(at: index)
}

public func presentNetworkMonitor(){
public func presentNetworkMonitor() {
let storyboard = UIStoryboard.NetShearsStoryBoard
if let initialVC = storyboard.instantiateInitialViewController(){
initialVC.modalPresentationStyle = .fullScreen
((initialVC as? UINavigationController)?.topViewController as? RequestsViewController)?.delegate = bodyExportDelegate
UIViewController.currentViewController()?.present(initialVC, animated: true, completion: nil)
}
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/NetShears/UI/BodyDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ final class BodyDetailViewController: UIViewController, ShowLoaderProtocol {
@IBOutlet weak var buttonNext: UIBarButtonItem!

static let kPadding: CGFloat = 10.0

var bodyExportType: BodyExportType = .default

var searchController: UISearchController?
var highlightedWords: [NSTextCheckingResult] = []
Expand Down Expand Up @@ -47,7 +49,7 @@ final class BodyDetailViewController: UIViewController, ShowLoaderProtocol {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let hud = showLoader(view: view)
RequestExporter.body(data) { [weak self] (stringData) in
RequestExporter.body(data, bodyExportType: bodyExportType ?? .default) { [weak self] (stringData) in
let formattedJSON = stringData
DispatchQueue.main.async {
self?.textView.text = formattedJSON
Expand Down
20 changes: 16 additions & 4 deletions Sources/NetShears/UI/RequestDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UIKit
final class RequestDetailViewController: UIViewController, ShowLoaderProtocol {

@IBOutlet weak var tableView: UITableView!
weak var delegate: BodyExporterDelegate?

var request: NetShearsRequestModel?
var sections: [NetShearsSection] = [
Expand Down Expand Up @@ -73,15 +74,26 @@ final class RequestDetailViewController: UIViewController, ShowLoaderProtocol {

func shareContent(_ sender: UIBarButtonItem, requestExportOption: RequestResponseExportOption = .flat){
if let request = request{
NSHelper.shareRequests(presentingViewController: self, sender: sender, requests: [request], requestExportOption: requestExportOption)
NSHelper.shareRequests(presentingViewController: self, sender: sender, requests: [request], requestExportOption: requestExportOption, delegate: delegate)
}
}

func openBodyDetailVC(title: String?, body: Data?){
private var responseExportType: BodyExportType {
guard let request = request else { return .default }
return delegate?.netShears(exportResponseBodyFor: request) ?? .default
}

private var requestExportType: BodyExportType {
guard let request = request else { return .default }
return delegate?.netShears(exportRequestBodyFor: request) ?? .default
}

func openBodyDetailVC(title: String?, body: Data?, exportType: BodyExportType) {
let storyboard = UIStoryboard.NetShearsStoryBoard
if let requestDetailVC = storyboard.instantiateViewController(withIdentifier: "BodyDetailViewController") as? BodyDetailViewController{
requestDetailVC.title = title
requestDetailVC.data = body
requestDetailVC.bodyExportType = exportType
self.show(requestDetailVC, sender: self)
}
}
Expand Down Expand Up @@ -146,10 +158,10 @@ extension RequestDetailViewController: UITableViewDelegate{

switch section.type {
case .requestBody:
openBodyDetailVC(title: "Request Body", body: request?.httpBody)
openBodyDetailVC(title: "Request Body", body: request?.httpBody, exportType: requestExportType)
break
case .responseBody:
openBodyDetailVC(title: "Response Body", body: request?.dataResponse)
openBodyDetailVC(title: "Response Body", body: request?.dataResponse, exportType: responseExportType)
break
default:
break
Expand Down
4 changes: 3 additions & 1 deletion Sources/NetShears/UI/RequestListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import UIKit
class RequestsViewController: UIViewController, ShowLoaderProtocol {

@IBOutlet weak var collectionView: UICollectionView!
weak var delegate: BodyExporterDelegate?

private var filteredRequests: [NetShearsRequestModel] = Storage.shared.requests

Expand Down Expand Up @@ -101,7 +102,7 @@ class RequestsViewController: UIViewController, ShowLoaderProtocol {
}

private func shareContent(_ sender: UIBarButtonItem, requestExportOption: RequestResponseExportOption = .flat){
NSHelper.shareRequests(presentingViewController: self, sender: sender, requests: filteredRequests, requestExportOption: requestExportOption)
NSHelper.shareRequests(presentingViewController: self, sender: sender, requests: filteredRequests, requestExportOption: requestExportOption, delegate: delegate)
}

// MARK: - Navigation
Expand All @@ -119,6 +120,7 @@ class RequestsViewController: UIViewController, ShowLoaderProtocol {
let storyboard = UIStoryboard.NetShearsStoryBoard
if let requestDetailVC = storyboard.instantiateViewController(withIdentifier: String(describing: RequestDetailViewController.self)) as? RequestDetailViewController{
requestDetailVC.request = request
requestDetailVC.delegate = delegate
self.show(requestDetailVC, sender: self)
}
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/NetShears/Utils/NSHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import UIKit

final class NSHelper {

static func shareRequests(presentingViewController: UIViewController, sender: UIBarButtonItem, requests: [NetShearsRequestModel], requestExportOption: RequestResponseExportOption = .flat){
static func shareRequests(presentingViewController: UIViewController, sender: UIBarButtonItem, requests: [NetShearsRequestModel], requestExportOption: RequestResponseExportOption = .flat, delegate: BodyExporterDelegate?){
var text = ""
switch requestExportOption {
case .flat:
text = getTxtText(requests: requests)
text = getTxtText(requests: requests, delegate: delegate)
case .curl:
text = getCurlText(requests: requests)
text = getCurlText(requests: requests, delegate: delegate)
case .postman:
text = getPostmanCollection(requests: requests) ?? "{}"
text = text.replacingOccurrences(of: "\\/", with: "/")
Expand All @@ -42,18 +42,18 @@ final class NSHelper {
presentingViewController.present(activityViewController, animated: true, completion: nil)
}

private static func getTxtText(requests: [NetShearsRequestModel]) -> String {
private static func getTxtText(requests: [NetShearsRequestModel], delegate: BodyExporterDelegate?) -> String {
var text: String = ""
for request in requests{
text = text + RequestExporter.txtExport(request: request)
text = text + RequestExporter.txtExport(request: request, delegate: delegate)
}
return text
}

private static func getCurlText(requests: [NetShearsRequestModel]) -> String {
private static func getCurlText(requests: [NetShearsRequestModel], delegate: BodyExporterDelegate?) -> String {
var text: String = ""
for request in requests{
text = text + RequestExporter.curlExport(request: request)
text = text + RequestExporter.curlExport(request: request, delegate: delegate)
}
return text
}
Expand Down
25 changes: 17 additions & 8 deletions Sources/NetShears/Utils/RequestExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

import UIKit

public enum BodyExportType {
case `default`
case custom(_ text: String)
}

final class RequestExporter: NSObject {

static func overview(request: NetShearsRequestModel) -> NSMutableAttributedString{
Expand All @@ -33,14 +38,18 @@ final class RequestExporter: NSObject {
return final
}

static func body(_ body: Data?, splitLength: Int? = nil, completion: @escaping (String) -> Void){
static func body(_ body: Data?, splitLength: Int? = nil, bodyExportType: BodyExportType, completion: @escaping (String) -> Void){
DispatchQueue.global().async {
completion(RequestExporter.body(body, splitLength: splitLength))
completion(RequestExporter.body(body, splitLength: splitLength, bodyExportType: bodyExportType))
return
}
}

static func body(_ body: Data?, splitLength: Int? = nil) -> String{
static func body(_ body: Data?, splitLength: Int? = nil, bodyExportType: BodyExportType) -> String {
if case .custom(let text) = bodyExportType {
return text
}

guard body != nil else {
return "-"
}
Expand All @@ -52,26 +61,26 @@ final class RequestExporter: NSObject {
return "-"
}

static func txtExport(request: NetShearsRequestModel) -> String{
static func txtExport(request: NetShearsRequestModel, delegate: BodyExporterDelegate?) -> String{

var txt: String = ""
txt.append("*** Overview *** \n")
txt.append(overview(request: request).string + "\n\n")
txt.append("*** Request Header *** \n")
txt.append(header(request.headers).string + "\n\n")
txt.append("*** Request Body *** \n")
txt.append(body(request.httpBody) + "\n\n")
txt.append(body(request.httpBody, bodyExportType: delegate?.netShears(exportRequestBodyFor: request) ?? .default) + "\n\n")
txt.append("*** Response Header *** \n")
txt.append(header(request.responseHeaders).string + "\n\n")
txt.append("*** Response Body *** \n")
txt.append(body(request.dataResponse) + "\n\n")
txt.append(body(request.dataResponse, bodyExportType: delegate?.netShears(exportResponseBodyFor: request) ?? .default) + "\n\n")
txt.append("------------------------------------------------------------------------\n")
txt.append("------------------------------------------------------------------------\n")
txt.append("------------------------------------------------------------------------\n\n\n\n")
return txt
}

static func curlExport(request: NetShearsRequestModel) -> String{
static func curlExport(request: NetShearsRequestModel, delegate: BodyExporterDelegate?) -> String{

var txt: String = ""
txt.append("*** Overview *** \n")
Expand All @@ -81,7 +90,7 @@ final class RequestExporter: NSObject {
txt.append("*** Response Header *** \n")
txt.append(header(request.responseHeaders).string + "\n\n")
txt.append("*** Response Body *** \n")
txt.append(body(request.dataResponse) + "\n\n")
txt.append(body(request.dataResponse, bodyExportType: delegate?.netShears(exportResponseBodyFor: request) ?? .default) + "\n\n")
txt.append("------------------------------------------------------------------------\n")
txt.append("------------------------------------------------------------------------\n")
txt.append("------------------------------------------------------------------------\n\n\n\n")
Expand Down

0 comments on commit 7675afd

Please sign in to comment.