-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating cURL representation
- Loading branch information
Mathias Amnell
committed
Jan 23, 2019
1 parent
e702acf
commit a618bae
Showing
5 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...s/ContentRepresentation/ContentRepresentation/CURLRepresentation/CURLRepresentation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |