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

Only parse valid json #28

Merged
merged 12 commits into from
Jan 12, 2023
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Brightscript Debugger> [{"id":"8575373301","type":"WatchEvent","actor":{"id":453

There’s also a builtin JSON encoder/decoder, in case you’re dealing with JSON data:
```
Brightscript Debugger> r = Requests().get("https://api.github.com/events")`
Brightscript Debugger> r = Requests().get("https://api.github.com/events")
Brightscript Debugger> ?r.json
Brightscript Debugger> <Component: roArray> =
[
Expand All @@ -86,6 +86,16 @@ Brightscript Debugger> <Component: roArray> =
]
```

You also also pass flags for json parsing. `parseJsonFlags` is passed to the [ParseJson()](https://developer.roku.com/en-ca/docs/references/brightscript/language/global-utility-functions.md#parsejsonjsonstring-as-string-flags---as-string-as-object) function.
```
Brightscript Debugger> r = Requests().get("https://api.github.com/events", {parseJsonFlags:"i"})
Brightscript Debugger> ?r.json
```
Or disable json parsing
```
Brightscript Debugger> r = Requests().get("https://api.github.com/events", {parseJson:false})
Brightscript Debugger> ?r.json
```
### Custom Headers

If you’d like to add HTTP headers to a request, simply pass in an `AA` to the `headers` key in the args dictionary.
Expand Down
22 changes: 17 additions & 5 deletions src/source/Requests.brs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function Requests_request(method, url as String, args as Object)
_verify = "common:/certs/ca-bundle.crt"
_useCache = true
_cacheSeconds = invalid
_parseJson = true
_parseJsonFlags = ""

if args <> invalid and type(args) = "roAssociativeArray"
if args.params <> invalid and type(args.params) = "roAssociativeArray"
Expand Down Expand Up @@ -67,6 +69,12 @@ function Requests_request(method, url as String, args as Object)
if args.cacheSeconds <> invalid and (type(args.cacheSeconds) = "Integer" or type(args.cacheSeconds) = "roInteger")
_cacheSeconds = args.cacheSeconds
end if
if args.parseJson <> invalid and (type(args.parseJson) = "Boolean" or type(args.parseJson) = "roBoolean")
_parseJson = args.parseJson
end if
if args.parseJsonFlags <> invalid and (type(args.parseJsonFlags) = "String" or type(args.parseJsonFlags) = "roString")
_parseJsonFlags = args.parseJsonFlags
end if
end if

' Constructs and sends a Request.
Expand All @@ -86,6 +94,8 @@ function Requests_request(method, url as String, args as Object)
' Defaults to `common:/certs/ca-bundle.crt`
' :param useCache: (optional) Boolean. Enable/Disable caching. Defaults to true
' :param cacheSeconds: (optional) Integer. Enable/Disable caching. Defaults to response's Cache-Control if it exists
' :param parseJson: (optional) Boolean. Enable/Disable parsing json response. Defaults to true
' :param parseJsonFlags: (optional) String. Flags passed to ParseJson() function
' :return: :class:`Requests <Response>` object
' Usage::
' req = Requests().get('http://httpbin.org/get')
Expand Down Expand Up @@ -124,7 +134,7 @@ function Requests_request(method, url as String, args as Object)
end if

if response = invalid
response = Requests_run(method, url, headers, data, _timeout, _retryCount, _verify)
response = Requests_run(method, url, headers, data, _timeout, _retryCount, _verify, _parseJson, _parseJsonFlags)
if rc <> invalid and _useCache
rc.put(response)
end if
Expand All @@ -134,7 +144,7 @@ function Requests_request(method, url as String, args as Object)

end function

function Requests_run(method, url, headers, data, timeout, retryCount, verify)
function Requests_run(method, url, headers, data, timeout, retryCount, verify, parseJson, parseJsonFlags)

urlTransfer = RequestsUrlTransfer(true, true, verify)
urlTransfer.setUrl(url)
Expand All @@ -153,6 +163,8 @@ function Requests_run(method, url, headers, data, timeout, retryCount, verify)
responseEvent = invalid
requestDetails = {
timesTried : 0,
parseJson : parseJson,
parseJsonFlags: parseJsonFlags,
}
'while we still have try times
while retryCount >= 0
Expand Down Expand Up @@ -439,10 +451,10 @@ function Requests_response(urlTransfer as Object, responseEvent as Object, reque
end if
end if



if rr.text <> invalid
rr.json = parseJson(rr.text)
if requestDetails.parseJson = true
rr.json = parseJson(rr.text, requestDetails.parseJsonFlags)
end if
rr.body = rr.text
end if

Expand Down