Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes several issues with local HTTP requests #30

Merged
merged 5 commits into from
Oct 13, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions smartapps/ady624/webcore-piston.src/webcore-piston.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,10 @@ def handleEvents(event) {
//schedules.removeAll{ (it.t <= threshold) && (it.s == event.schedule.s) && (it.i == event.schedule.i) }
schedules.remove(event.schedule)
if (event.name == 'wc_async_reply') {
if (event.schedule.stack) event.schedule.stack.response = event.jsonData
if (event.schedule.stack) {
event.schedule.stack.response = event.responseData
event.schedule.stack.json = event.jsonData
}
event.name = 'time'
event.value = now()
int responseCode = cast(rtData, event.responseCode, 'integer')
Expand Down Expand Up @@ -3138,7 +3141,7 @@ public localHttpRequestHandler(physicalgraph.device.HubResponse hubResponse) {
}

def binary = false
def mediaType = hubResponse.getHeaders()['content-type']?.toLowerCase()
def mediaType = hubResponse.getHeaders()['content-type']?.toLowerCase().tokenize(';')[0]
switch (mediaType) {
case 'image/jpeg':
case 'image/png':
Expand All @@ -3151,20 +3154,21 @@ public localHttpRequestHandler(physicalgraph.device.HubResponse hubResponse) {
if (binary) {
setRtData.mediaType = mediaType
setRtData.mediaData = data?.getBytes()
} else {
} else if (data) {
try {
if (data.startsWith('{') && data.endsWith('}')) {
json = (LinkedHashMap) new groovy.json.JsonSlurper().parseText(data)
} else if (data.startsWith('[') && data.endsWith(']')) {
json = (List) new groovy.json.JsonSlurper().parseText(data)
def trimmed = data.trim()
if (trimmed.startsWith('{') && trimmed.endsWith('}')) {
json = (LinkedHashMap) new groovy.json.JsonSlurper().parseText(trimmed)
} else if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
json = (List) new groovy.json.JsonSlurper().parseText(trimmed)
} else {
json = [:]
}
} catch (all) {
json = [:]
}
}
handleEvents([date: new Date(), device: location, name: 'wc_async_reply', value: 'httpRequest', jsonData: json, responseCode: responseCode, setRtData: setRtData])
handleEvents([date: new Date(), device: location, name: 'wc_async_reply', value: 'httpRequest', contentType: mediaType, responseData: data, jsonData: json, responseCode: responseCode, setRtData: setRtData])
}

private long vcmd_httpRequest(rtData, device, params) {
Expand Down