Skip to content

Commit

Permalink
Update curl command constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
chao committed Mar 21, 2014
1 parent 32052e9 commit baf5dfa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
26 changes: 18 additions & 8 deletions content/js/restclient.curl.js
Expand Up @@ -30,15 +30,25 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

restclient.curl = {
constructCommand: function(request){
//console.log(request);
var headersStrings = "";
for(var i=0, header; header = request.headers[i]; i++) {
headersStrings += " -H '"+ header[0] + ':' + header[1] + "'";
var curl = 'curl';
if(typeof request.headers !== 'undefined') {
var headersStrings = "";
for(var i=0, header; header = request.headers[i]; i++) {
headersStrings += " -H '"+ header[0] + ':' + header[1] + "'";
}
curl += ' -i ' + headersStrings;
}
var body = '';
if(request.body != ''){
body = " -d '" + request.body + "' ";
if(typeof request.method !== 'undefined')
curl += ' -X ' + request.method;

if(typeof request.body !== 'undefined' && request.body !== '') {
curl += " -d '" + request.body + "' ";
//TODO escape special chars
}
return 'curl -i ' + headersStrings + ' -X ' + request.method + body +" '" + request.url + "'" ;

if(typeof request.url !== 'undefined') {
curl += " '" + request.url + "'";
}
return curl;
}
}
6 changes: 5 additions & 1 deletion content/js/restclient.main.js
Expand Up @@ -604,7 +604,11 @@ restclient.main = {
}
}
});
}
}

var request = restclient.main.getRequest();
var curl = restclient.curl.constructCommand(request);
$('#modal-bookmark-request [name="saved-curl"]').val(curl);
}).on('hide', function() {
$('#modal-bookmark-request [name="saved-request-name"]').val('');
$('#modal-bookmark-request [name="saved-request-label"]').tagsinput('removeAll');
Expand Down
5 changes: 4 additions & 1 deletion content/js/restclient.sqlite.js
Expand Up @@ -37,6 +37,7 @@ restclient.sqlite = {
requestUrl TEXT NOT NULL, \
requestMethod TEXT NOT NULL, \
request TEXT NOT NULL, \
curl TEXT NOT NULL, \
creationTime INTEGER NOT NULL, \
lastAccess INTEGER NOT NULL",
labels: " labelName TEXT NOT NULL, \
Expand All @@ -59,7 +60,7 @@ restclient.sqlite = {
getRequestsByName: 'SELECT * FROM requests WHERE requestName = :requestName',
getRequestsByLabels: 'SELECT * FROM requests WHERE uuid IN (SELECT uuid FROM labels WHERE labelName IN (placeholder) group by uuid having count(uuid) = :num) ORDER BY creationTime DESC, lastAccess DESC',
getRequestByUUID: 'SELECT * FROM requests WHERE uuid = :uuid',
newRequests: 'INSERT INTO requests (uuid, requestName, favorite, requestUrl, requestMethod, request, creationTime, lastAccess) VALUES (:uuid, :requestName, :favorite, :requestUrl, :requestMethod, :request, :creationTime, :lastAccess)',
newRequests: 'INSERT INTO requests (uuid, requestName, favorite, requestUrl, requestMethod, request, curl, creationTime, lastAccess) VALUES (:uuid, :requestName, :favorite, :requestUrl, :requestMethod, :request, :curl, :creationTime, :lastAccess)',
findRequestsByKeyword: 'SELECT * FROM requests WHERE requestName LIKE :word OR requestUrl LIKE :word ORDER BY creationTime DESC, lastAccess DESC',
findRequestsByKeywordAndLabels: 'SELECT * FROM requests WHERE (requestName LIKE :word OR requestUrl LIKE :word) AND uuid IN (SELECT uuid FROM labels WHERE labelName IN (placeholder) group by uuid having count(uuid) = :num) ORDER BY creationTime DESC, lastAccess DESC',
removeRequests: 'DELETE FROM requests WHERE uuid = :uuid',
Expand Down Expand Up @@ -227,6 +228,7 @@ restclient.sqlite = {
favorite = favorite || 0;
labels = labels || [];
labels = _.uniq(labels);
var curl = restclient.curl.constructCommand(request);
try{
var stmt = restclient.sqlite.getStatement('newRequests');
var params = stmt.newBindingParamsArray(),
Expand All @@ -238,6 +240,7 @@ restclient.sqlite = {
binding.bindByName("requestUrl", request.url);
binding.bindByName("requestMethod", request.method);
binding.bindByName("request", JSON.stringify(request));
binding.bindByName("curl", curl);
binding.bindByName("creationTime", creationTime);
binding.bindByName("lastAccess", creationTime);

Expand Down
7 changes: 6 additions & 1 deletion content/restclient.html
Expand Up @@ -289,7 +289,12 @@ <h3 class="title">Bookmark This Request</h3>
<p class="help-block hide">Please give this request a name for future usage.</p>
</div>
</div>

<div class="control-group">
<label class="control-label" for="saved-curl">Curl Command</label>
<div class="controls">
<textarea name="saved-curl" rows="3" class="span3" readonly></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="saved-request-label">Label</label>
<div class="controls">
Expand Down
1 change: 1 addition & 0 deletions content/tests/index.html
Expand Up @@ -11,6 +11,7 @@
<script src="../js/restclient.js"></script>
<script src="../js/restclient.helper.js"></script>
<script src="../js/restclient.oauth.js"></script>
<script src="../js/restclient.curl.js"></script>
<script src="../js/restclient.sqlite.js"></script>
<script src="../js/underscore.js"></script>
<!-- unit tests -->
Expand Down

0 comments on commit baf5dfa

Please sign in to comment.