Skip to content

Commit

Permalink
Merge pull request #51 from ddavison/fix-payload-json
Browse files Browse the repository at this point in the history
Fix payload json, unify headers
  • Loading branch information
javaguirre authored Aug 30, 2016
2 parents 2ea4b68 + 3c29729 commit 619b6e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 60 deletions.
16 changes: 3 additions & 13 deletions lib/rest-client-persist.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ class RestClientPersist

saveFile: ->
requestsToBeSaved = @get(@requestFileLimit)
fs.writeFile(
fs.writeFileSync(
"#{@path}",
JSON.stringify(requestsToBeSaved),
@showErrorOnPersist
)
JSON.stringify(requestsToBeSaved))

update: (requests) ->
@requests = requests
Expand All @@ -53,15 +51,7 @@ class RestClientPersist
requestEquals: (request1, request2) ->
return (request1.url == request2.url and
request1.method == request2.method)


showErrorOnPersist: (err) =>
if err
atom.confirm(
message: 'Cannot save file: ' + @path,
detailedMessage: JSON.stringify(err)
)


getRequestFileLimit: () ->
return @requestFileLimit

Expand Down
63 changes: 16 additions & 47 deletions lib/rest-client-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ PACKAGE_PATH = atom.packages.resolvePackagePath('rest-client')
ENTER_KEY = 13
DEFAULT_NORESPONSE = 'NO RESPONSE'
DEFAULT_REQUESTS_LIMIT = 10
DEFAULT_HEADERS = ['User-Agent', 'Content-Type']
RECENT_REQUESTS_FILE_LIMIT = 5
current_method = 'GET'

# Error messages
PAYLOAD_JSON_ERROR_MESSAGE = 'The json payload is not valid'
RECENT_REQUESTS_ERROR_MESSAGE = 'Recent requests couldn\'t be loaded'
SAVED_REQUESTS_ERROR_MESSAGE = 'Saved requests couldn\'t be loaded'

response = '' # global object for the response.

Expand All @@ -29,7 +30,6 @@ rest_form =
payload: '.rest-client-payload',
encode_payload: '.rest-client-encodepayload',
decode_payload: '.rest-client-decodepayload',
content_type: '.rest-client-content-type',
clear_btn: '.rest-client-clear',
send_btn: '.rest-client-send',
save_btn: '.rest-client-save',
Expand All @@ -38,7 +38,6 @@ rest_form =
result_link: '.rest-client-result-link',
result_headers_link: '.rest-client-result-headers-link',
status: '.rest-client-status',
user_agent: '.rest-client-user-agent',
strict_ssl: '.rest-client-strict-ssl',
proxy_server: '.rest-client-proxy-server',
open_in_editor: '.rest-client-open-in-editor'
Expand Down Expand Up @@ -97,8 +96,6 @@ class RestClientView extends ScrollView
@button class: 'btn selected', 'Raw'

@textarea class: "field #{rest_form.headers.split('.')[1]}", rows: 7
@strong 'User-Agent'
@input class: "field #{rest_form.user_agent.split('.')[1]}", value: 'atom-rest-client'
@strong 'Strict SSL'
@input type: 'checkbox', class: "field #{rest_form.strict_ssl.split('.')[1]}", checked: true

Expand All @@ -117,16 +114,6 @@ class RestClientView extends ScrollView

@textarea class: "field #{rest_form.payload.split('.')[1]}", rows: 7

# Content-Type
@select class: "list-group #{rest_form.content_type.split('.')[1]}", =>
@option class: 'selected', 'application/x-www-form-urlencoded', 'application/x-www-form-urlencoded'
@option value: 'application/atom+xml', 'application/atom+xml'
@option value: 'application/json', 'application/json'
@option value:'application/xml', 'application/xml'
@option value: 'application/multipart-formdata', 'application/multipart-formdata'
@option value: 'text/html', 'text/html'
@option value: 'text/plain', 'text/plain'

# Result
@div class: 'tool-panel panel-bottom padded', =>
@a class: "#{rest_form.result_link.split('.')[1]}", 'Result'
Expand Down Expand Up @@ -223,19 +210,11 @@ class RestClientView extends ScrollView
$(rest_form.result).text(RestClientResponse.DEFAULT_RESPONSE)

getHeaders: ->
headers = {
'User-Agent': $(rest_form.user_agent).val(),
'Content-Type': $(rest_form.content_type).val() + ';charset=utf-8'
}
headers = @getCustomHeaders(headers)

return headers

getCustomHeaders: (headers) ->
headers = []
custom_headers = $(rest_form.headers).val().split('\n')

for custom_header in custom_headers
current_header = custom_header.split(':')
current_header = custom_header.trim().split(':')
if current_header.length > 1
headers[current_header[0]] = current_header[1].trim()

Expand Down Expand Up @@ -270,7 +249,7 @@ class RestClientView extends ScrollView
getRequestOptions: ->
options =
url: $(rest_form.url).val()
headers: this.getHeaders()
headers: @getHeaders()
method: current_method,
strictSSL: $(rest_form.strict_ssl).is(':checked'),
proxy: $(rest_form.proxy_server).val(),
Expand All @@ -286,7 +265,7 @@ class RestClientView extends ScrollView
else
@showErrorResponse(statusMessage)

headers = @formatHeaders response.headers
headers = @getHeadersAsString response.headers
response = new RestClientResponse(body).getFormatted()
result = response
else
Expand All @@ -300,23 +279,20 @@ class RestClientView extends ScrollView
getRequestBody: ->
payload = $(rest_form.payload).val()
body = ""
content_type = @getContentType()

if payload
switch $(rest_form.content_type).val()
switch content_type
when "application/json"
body = JSON.stringify JSON.parse(payload)
body = JSON.stringify(JSON.parse(payload))
else
body = payload

body

formatHeaders: (headers) =>
formattedHeaders = ''

for key, value of headers
formattedHeaders += key + ': ' + value + '\n'

formattedHeaders
getContentType: ->
headers = @getHeaders()
headers['Content-Type'] || headers['content-type']

showSuccessfulResponse: (text) =>
$(rest_form.status)
Expand All @@ -336,15 +312,15 @@ class RestClientView extends ScrollView

loadRecentRequestsInView: (err, requests) =>
if err
console.log('Recent requests couldn\'t be loaded')
atom.notifications.addError RECENT_REQUESTS_ERROR_MESSAGE
return

@recentRequests.update(JSON.parse(requests))
@addRequestsInView(recent_requests.list, @recentRequests.get())

loadSavedRequestsInView: (err, requests) =>
if err
console.log('Saved requests couldn\'t be loaded')
atom.notifications.addError SAVED_REQUESTS_ERROR_MESSAGE
return

@savedRequests.update(JSON.parse(requests))
Expand Down Expand Up @@ -390,10 +366,8 @@ class RestClientView extends ScrollView
fillInRequest: (request) ->
$(rest_form.url).val(request.url)
@setMethodAsSelected(request.method)
$(rest_form.payload).val(request.payload)
$(rest_form.payload).val(request.body)
$(rest_form.headers).val(@getHeadersAsString(request.headers))
$(rest_form.user_agent).val(request.headers['User-Agent'])
$(rest_form.content_type).val(request.headers['Content-Type'])

addRecentRequestItem: (data) =>
@addRequestItem(recent_requests.list, data)
Expand All @@ -405,19 +379,14 @@ class RestClientView extends ScrollView
output = ''

for header, value of headers
if not @isDefaultHeader(header)
output = output.concat(header + ': ' + value + '\n')
output = output.concat(header + ': ' + value + '\n')

return output

setMethodAsSelected: (method) ->
$method = $(rest_form.method + '-' + method.toLowerCase())
$method.click()

isDefaultHeader: (header) ->
return DEFAULT_HEADERS.indexOf(header) != -1


# Returns an object that can be retrieved when package is activated
serialize: ->
deserializer: @constructor.name
Expand Down

1 comment on commit 619b6e9

@ddavison
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to apm publish <major|minor|patch> ! i'll leave it up to your discretion the size of the change

Please sign in to comment.