Skip to content

Commit

Permalink
Add support for generating cURL representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Amnell committed Jan 23, 2019
1 parent e702acf commit a618bae
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
12 changes: 12 additions & 0 deletions mac/Bagel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1191142A21F8B9CB00BFCA48 /* CURLRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1191142921F8B9CB00BFCA48 /* CURLRepresentation.swift */; };
9AAA3A9C54B46F8D2C8DD674 /* Pods_Bagel.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 00E3BE2EECB0984D92EFCA30 /* Pods_Bagel.framework */; };
BC1F5B37216746D30045C871 /* FontManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC1F5B36216746D30045C871 /* FontManager.swift */; };
BC32643621738AF0006452FE /* KeyValueRepresentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC32643521738AF0006452FE /* KeyValueRepresentation.swift */; };
Expand Down Expand Up @@ -114,6 +115,7 @@

/* Begin PBXFileReference section */
00E3BE2EECB0984D92EFCA30 /* Pods_Bagel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Bagel.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1191142921F8B9CB00BFCA48 /* CURLRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CURLRepresentation.swift; sourceTree = "<group>"; };
BC1F5B36216746D30045C871 /* FontManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontManager.swift; sourceTree = "<group>"; };
BC32643521738AF0006452FE /* KeyValueRepresentation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyValueRepresentation.swift; sourceTree = "<group>"; };
BC5E76C8216A64DB000F658D /* Theme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Theme.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -236,6 +238,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1191142821F8B9AB00BFCA48 /* CURLRepresentation */ = {
isa = PBXGroup;
children = (
1191142921F8B9CB00BFCA48 /* CURLRepresentation.swift */,
);
path = CURLRepresentation;
sourceTree = "<group>";
};
659EF5768D424A8F8EEB1541 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -489,6 +499,7 @@
BCEB9AA4217C951C008BBF3C /* ContentRepresentation */ = {
isa = PBXGroup;
children = (
1191142821F8B9AB00BFCA48 /* CURLRepresentation */,
BCEB9AA5217C9532008BBF3C /* ContentRepresentation.swift */,
BCEB9AA9217CB0BB008BBF3C /* OverviewRepresentation */,
BC32643421738AB5006452FE /* ParameterRepresentation */,
Expand Down Expand Up @@ -857,6 +868,7 @@
BCFC853021383267001EC6D7 /* ProjectsViewController.swift in Sources */,
BCA60B442162395000B9DCAD /* DevicesViewModel.swift in Sources */,
BC73DFAE215BFB18002E533B /* BagelPublisher.swift in Sources */,
1191142A21F8B9CB00BFCA48 /* CURLRepresentation.swift in Sources */,
BCA60B41216201B900B9DCAD /* BagelConfiguration.swift in Sources */,
BC61DA752162B976000F6D2F /* DetailViewModel.swift in Sources */,
BC9C77B22163897600E8ADE8 /* DataTextViewModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class OverviewViewController: BaseViewController {

@IBOutlet var overviewTextView: NSTextView!

@IBOutlet weak var curlButton: NSButton!
@IBOutlet weak var copyToClipboardButton: NSButton!

var viewModel: OverviewViewModel?
private var isCurl: Bool = false

override func setup() {

Expand All @@ -32,11 +34,27 @@ class OverviewViewController: BaseViewController {

func refresh() {

self.overviewTextView.textStorage?.setAttributedString(TextStyles.codeAttributedString(string: self.viewModel?.overviewRepresentation?.rawString ?? ""))
if isCurl {

self.overviewTextView.textStorage?.setAttributedString(TextStyles.codeAttributedString(string: self.viewModel?.curlRepresentation?.rawString ?? ""))
curlButton.state = .on
} else {

self.overviewTextView.textStorage?.setAttributedString(TextStyles.codeAttributedString(string: self.viewModel?.overviewRepresentation?.rawString ?? ""))
curlButton.state = .off
}
}

@IBAction func curlButtonAction(_ sender: Any) {
self.isCurl.toggle()
self.refresh()
}

@IBAction func copyButtonAction(_ sender: Any) {

self.viewModel?.copyToClipboard()
if isCurl {
self.viewModel?.copyCURLToClipboard()
} else {
self.viewModel?.copyTextToClipboard()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class OverviewViewModel: BaseViewModel {

var packet: BagelPacket?
var overviewRepresentation: OverviewRepresentation?
var curlRepresentation: CURLRepresentation?

func register() {

Expand All @@ -26,12 +27,17 @@ class OverviewViewModel: BaseViewModel {
if let requestInfo = self.packet?.requestInfo {

self.overviewRepresentation = OverviewRepresentation(requestInfo: requestInfo)
self.curlRepresentation = CURLRepresentation(requestInfo: requestInfo)
}

self.onChange?()
}

func copyToClipboard() {
func copyTextToClipboard() {
self.overviewRepresentation?.copyToClipboard()
}

func copyCURLToClipboard() {
self.curlRepresentation?.copyToClipboard()
}
}
14 changes: 14 additions & 0 deletions mac/Bagel/Components/Details/Details.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,21 @@
<action selector="copyButtonAction:" target="dbv-oR-1ws" id="ZeH-wK-TFV"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="ze8-fq-lWt">
<rect key="frame" x="379" y="7" width="36" height="16"/>
<buttonCell key="cell" type="recessed" title="cURL" bezelStyle="recessed" alignment="center" state="on" imageScaling="proportionallyDown" inset="2" id="MSt-7f-JHj">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="systemBold" size="12"/>
</buttonCell>
<color key="contentTintColor" red="0.70980392160000005" green="0.49803921569999998" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<connections>
<action selector="curlButtonAction:" target="dbv-oR-1ws" id="H2c-aD-nWX"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="ze8-fq-lWt" firstAttribute="centerY" secondItem="9zZ-1e-l97" secondAttribute="centerY" id="9Mr-Ib-eBx"/>
<constraint firstItem="U6O-Tm-OuZ" firstAttribute="leading" secondItem="ze8-fq-lWt" secondAttribute="trailing" constant="10" id="pzJ-6j-Rbh"/>
<constraint firstAttribute="trailing" secondItem="U6O-Tm-OuZ" secondAttribute="trailing" constant="10" id="tkO-3l-Gzv"/>
</constraints>
</view>
Expand Down Expand Up @@ -241,6 +254,7 @@
</view>
<connections>
<outlet property="copyToClipboardButton" destination="U6O-Tm-OuZ" id="Bis-TZ-SjP"/>
<outlet property="curlButton" destination="ze8-fq-lWt" id="FCG-bS-aGL"/>
<outlet property="overviewTextView" destination="yGA-r3-ock" id="sTN-fX-keW"/>
</connections>
</viewController>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// CURLRepresentation.swift
// Bagel
//
// Created by Mathias Amnell on 2019-01-23.
// Copyright © 2019 Yagiz Lab. All rights reserved.
//

import Cocoa

class CURLRepresentation: ContentRepresentation {

init(requestInfo: BagelRequestInfo?) {

super.init()

if let requestInfo = requestInfo {
self.rawString = requestInfo.curlString
}
}
}

extension BagelRequestInfo {
// Credits to shaps80
// https://gist.github.com/shaps80/ba6a1e2d477af0383e8f19b87f53661d
fileprivate var curlString: String {
guard let url = url else { return "" }
var baseCommand = "curl \(url)"

if requestMethod == "HEAD" {
baseCommand += " --head"
}

var command = [baseCommand]

if let method = self.requestMethod, method != "GET" && method != "HEAD" {
command.append("-X \(method)")
}

if let headers = requestHeaders {
for (key, value) in headers where key != "Cookie" {
command.append("-H '\(key): \(value)'")
}
}

if let data = requestBody {
command.append("-d '\(data)'")
}

return command.joined(separator: " \\\n\t")
}
}

0 comments on commit a618bae

Please sign in to comment.