REST in Pawn 1.3 - HTTP client for JSON REST APIs (Updated 2021/08/22)
This extension exposes a high performance HTTP client for JSON REST APIs. It supports HTTP/2, HTTPS and gzip, and provides methodmaps for JSON objects and arrays.
Requires SourceMod 1.10 or later. Note that requests are not processed during hibernation. You can set sv_hibernate_when_empty to 0 to disable hibernation. Note that HTTP/2 is only attempted over HTTPS, in line with most web clients. Installation
Source Code HTTP natives JSON natives https://img.shields.io/github/workfl...xt/CI/main.svg https://img.shields.io/github/releas.../sm-ripext.svg https://img.shields.io/github/downlo...pext/total.svg |
REST in Pawn Code Examples
Code Examples
JSON Create a JSON object PHP Code:
Create a JSON array PHP Code:
Nest JSON PHP Code:
Export to and import from files and strings PHP Code:
Iterate over the keys in a JSON object PHP Code:
HTTP Retrieve an item PHP Code:
Retrieve a collection PHP Code:
Create an item PHP Code:
Update an item PHP Code:
Delete an item PHP Code:
PHP Code:
Set the credentials for HTTP Basic authentication PHP Code:
PHP Code:
PHP Code:
Resource methodmaps You can also write your own methodmaps which inherit from JSONObject, and which abstract away the fields of a resource. This makes your code cleaner when reading and writing resources. plugin.inc PHP Code:
plugin.sp PHP Code:
Files Download a file PHP Code:
Upload a file PHP Code:
Forms Post form data PHP Code:
|
Re: REST in Pawn
Nice one :)
|
Re: REST in Pawn
Sweet.
Quote:
|
Re: REST in Pawn
Could be used with something like https://firebase.google.com/
|
Re: REST in Pawn
This looks really nice. I've been using steamworks+smjansson for things, this might be able to replace both for me.
|
Re: REST in Pawn
Looks amazing, will use for sure!
|
Re: REST in Pawn - HTTP and JSON natives
How to return information from https? use post
|
Re: REST in Pawn - HTTP and JSON methodmaps
Just a suggestion.
You can also add a function that generates the Authorization header... :D |
Re: REST in Pawn - HTTP and JSON methodmaps
Quick question,
Do i have to delete response.Data after im done with the data or does that get auto cleaned up? In your examples you only delete the handles spawned by looping through an array. Thanks! edit: looking at the source, Code:
handlesys->FreeHandle(hndlResponse, &sec); |
Re: REST in Pawn - HTTP and JSON methodmaps
Quote:
Quote:
|
Re: REST in Pawn - HTTP and JSON methodmaps
Quote:
Awesome. Something that would be very useful is a URL encode function. For example if you call an API with parameters such as /items?type=5&name=i have spaces it will error out on the API end because of the spaces. Not sure how easy that is to solve really. |
Re: REST in Pawn - HTTP and JSON methodmaps
Quote:
As this thing seems to be aimed at developers making their own things, I assume you're developing your own stuff that uses this thing, which means you could (remember code attribution!) "copy" this one: From Dynamic MOTD by @psychonic:
UrlEncodeString
|
Re: REST in Pawn - HTTP and JSON methodmaps
Quote:
Correct, making an API and a plugin to use it. Hmm yeah i guess this would solve most things. Would be maybe nice to bake it into the extension since its focused around making web calls :) |
Re: REST in Pawn - HTTP and JSON methodmaps
Looks like there's a curl_easy_escape(), so shouldn't be that hard.
|
Re: REST in Pawn - HTTP and JSON methodmaps
Quote:
|
Re: REST in Pawn - HTTP and JSON methodmaps
Sorry for the double post but...
I have a bug and i dont really even know how to describe it in words easily since I cant make a test plugin to replicate it but its definitely not working in the plugin i want it to. Could i add you on steam DJ Tsunami? Thanks |
Re: REST in Pawn - HTTP and JSON methodmaps
Any chance of functions to iterate through objects and arrays?
|
Re: REST in Pawn - HTTP and JSON methodmaps
Quote:
PHP Code:
|
Re: REST in Pawn - HTTP and JSON methodmaps
I intentionally left out object iteration to keep the code simple. Besides, when you're talking to an API you should know which fields are returned. If you're not talking to an API, I believe StringMap should suffice.
Quote:
|
Re: REST in Pawn - HTTP and JSON methodmaps
Good job :D
|
Re: REST in Pawn - Communicate with JSON REST APIs
Hi, I have a problem:
HTML Code:
L 08/14/2017 - 19:07:16: [RIPEXT] HTTP request failed: Error reading ca cert file /etc/ssl/certs/ca-certificates.crt - mbedTLS: (-0x3E00) PK - Read/write of file failed |
Re: REST in Pawn - Communicate with JSON REST APIs
It's because SSL_CERT_FILE environment variable is set to that path (which your user doesn't have permission to read). I guess you are trying to access a HTTPS endpoint? However, I don't know a fix. I'm sure you could Google that errors because it's a cURL error, it doesn't come from the extension.
|
Re: REST in Pawn - Communicate with JSON REST APIs
Version 1.0.3 now comes with the CA bundle in the configs/ripext folder, instead of it being hardcoded to /etc/ssl/certs/ca-certificates.crt. This fixes the issue above and also allows you to replace it with your own CA bundle if your API isn't trusted.
|
Re: REST in Pawn - Communicate with JSON REST APIs
Is there no way to see if a key exists in a JSONObject? Even GetString produces an exception if the key doesn't exist, and only returns false if the key is set but is null. I feel like that's a huge piece that's missing... It shouldn't be up to the API you're communicating with to insert a null value for every possible key it could return.
|
Re: REST in Pawn - Communicate with JSON REST APIs
Hmm, that behaviour is wrong according to the SourceMod error model - errors should always be avoidable and thus always caused by programmer error (either due to misuse or missing checks).
|
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
Quote:
|
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
Given an arbitrary adt trie it's not possible to prevent a lookup to a non-existing key based on any check - thus the lookups need to be safe. Even without that the lack of a "key existence check", most languages don't let you index to random parts of dynamic arrays (exception / undefined behavior), but most will let you attempt lookups to non-existent keys in maps. |
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
(Yes, a KeyExists function and throwing on invalid keys would also be suitable for the error model, but you pay the hash and lookup penalty twice, so it is simpler in that case to do in one go.) |
Re: REST in Pawn - Communicate with JSON REST APIs
https://sm.alliedmods.net/api/index....d=show&id=692&
The ADT_Array API doesn't support this behaviour either (see how with GetTrieValue the return is the state of the key, and the param is actually optional (well; should be)). Anyways; yeah, in a bunch of plugins I do similar things with checking keys; this is definitely the more intuitive approach then erroring out if a single json key is missing (is this really the point of contention???). |
Re: REST in Pawn - Communicate with JSON REST APIs
https://sm.alliedmods.net/api/index....d=show&id=692&
The ADT_Array API doesn't support this behaviour either (see how with GetTrieValue the return is the state of the key, and the param is actually optional (well; should be)). Anyways; yeah, in a bunch of plugins I do similar things with checking keys; this is definitely the more intuitive approach then erroring out if a single json key is missing (is this really the point of contention???). |
Re: REST in Pawn - Communicate with JSON REST APIs
Accelerator is telling me ripext is causing server crashes, can anyone confirm I'm reading this correctly? https://crash.limetech.org/yeyj445f3wip
Or could it be a false-positive? Either way, I have 7 crash reports like that from today. It says something about threads so HTTPClient must be causing it rather than the JSON stuff. |
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
|
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
edit: also what SM version were you using? |
Re: REST in Pawn - Communicate with JSON REST APIs
I have a single HTTPClient with no extra headers and I'm making several GET requests at a time periodically, maybe something like every few minutes on average. The crashes are relatively infrequent relative to the amount of requests being made.
SM version is 1.9.0.6080. |
Re: REST in Pawn - Communicate with JSON REST APIs
Possible reference to 1.9 and crashes: https://forums.alliedmods.net/showpo...&postcount=324
However I don't know too much about this extension or what was added/changed in 1.9 |
Re: REST in Pawn - Communicate with JSON REST APIs
I think there's a leak within closing the HTTPClientObjects: (ignore the trie snapshot leak i've fixed it already)
Code:
L 09/23/2017 - 17:55:44: [SM] Unloading plugin to free 24279 handles. PHP Code:
|
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
Either you should delete HTTPClient on response, or only form it once per host. |
Re: REST in Pawn - Communicate with JSON REST APIs
Quote:
|
Re: REST in Pawn - Communicate with JSON REST APIs
I think the code examples in the second post are better examples than the test plugin, as they do show the HTTPClient being stored in a global variable, and they are well documented.
|
All times are GMT -4. The time now is 06:30. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.