I'm working on a wordpress plugin using Stupeflix api, the old one with php.
I have to migrate it to the new one. But there's a real lack of explanation on stupeflix website, and no code example on how to do.
I created an account to test it. But it seems like I need to register a domain name. Ok. But what if I need to work localy ?
Then I use the task api. The JSON's a bit skinny : How would I pass my other params like my secret key ? How to keep it secret for example, if it's clearely readable in the source code ?
Thans for your spotlights :)
PS : I cannot create a stupeflix tag on stackoverflow. Such a shame, the stupeflix author told me to use that to get supports here...
The new stupeflix API is a standard HTTP API with JSON bodies, and you can send requests to it with any librairy in any language. Every language has a decent HTTP requesting library, for example in Python I recommend python-requests or Jquery.ajax()
There's not code examples as such, but you have many JSON examples here though.
Most of the time, you only need to send POST requests to dragon.stupeflix.com/v2/create with a JSON body containing one or many tasks definitions :
{
"task_name": "video.create",
"definition": "<movie service=\"craftsman-1.0\">\n <body>\n <effect type=\"none\">\n <video filename=\"http://s3.amazonaws.com/stupeflix-assets/apiusecase/footage.mov\" speed=\"2.0\" audio=\"false\">\n <filter type=\"colormap\" preset=\"bw\"/>\n </video>\n </effect>\n </body>\n</movie>"
}
Here is the documentation. You will get a JSON response as such :
{
"status": "executing",
"result": {
"duration": 10,
"preview": "http://dragon.stupeflix.com/storage/flvstreamer/222/LY5XZIPILG6WKKIAGQAB4RLHBY/360p/preview.flv"
},
"key": "LY5XZIPILG6WKKIAGQAB4RLHBY",
"progress": 10,
"events": {
"started": "2013-11-16T06:02:55.669278+00:00",
"queued": "2013-11-16T06:02:55.667394+00:00"
}
}
I hope this will help.
Related
I am a new bee to sigfox. I want to receive sensors' data from sigfox server to my webpage. I defined a callback function as in the pic below in sigfox. I want to know how to receive the JSON data in my webpage. My web page is running on a personal apache web server with a public ip.
I am using php. Like normally use a curl call to receive json from any server using its URL. Here in sigfox case, what source i.e. url I specify in curl to retrieve json data from it. OR what mechanism to use. I searched web & forum but didn't find any answer. Sigfox has a nice description about how to receiver data to an email but not for a custom url.
The body of the callback should be something like this
{
"deviceId": "{device}",
"time": {time},
"seqNumber": {seqNumber},
"data": "{data}",
"reception": [{ "id": "{station}", "RSSI": {rssi}, "SNR": {snr} }],
"duplicate": {duplicate},
"ack": {ack},
}
Other parts in your config seem ok, just write your endpoint in sigfoxdata.php to handle the data (eg. parse it).
right now I'm building a project with the laravel framework. I'm not a professional or whatever, in the past I built all my applications from scratch without the use of such frameworks.
Right now I'm implenting the Twitch API for certain parts of my website. While doing that I encoutered situations where the Twitch API returns an http error code (40x) even though it provides a "valid" response. Right now I'm using a pretty popular Twitch API package from packagist.org. As soon as I receive an 40x error code an exception will be thrown which would would break my application.
Reason for that is, that the response request looks like this:
$response = $this->client->send($request);
As a workaround I changed this to:
$response = $this->client->send($request, ['http_errors' => false]);
Obviously I could also make use of GuzzleHttp\Exception to handle these errors aswell. But from what I get, all of this would have to happen inside the package I downloaded via composer which means that as soon as the author releases an update and I run a composer update my changes would be overwritten and my app might be broken until I fix it again.
I guess not everyone is familiar with the Twitch API. An easy example for my question is an API call where you can check wether a specific user is following a a specific channel. When the user does NOT, the response looks like that:
{
"error": "Not Found",
"message": "12345 is not following 67890",
"status": 404 }
So this is a perfectly fine and valid response that would be easy to handle but instead of an 200 http status response the api responds with a 404 http status code which will throw an exception.
So my question is, is it really good practice in APIs to return a 40x status code even though you send and receive perfectly fine requests? It's not like there's an error like missing parameters, ids or whatever. It's just some kind of "bool" query where return can be true or false and where false will always return a 40x http status code but still contains a valid response.
Thank you
Package that is throwing an exception is the best kind, because you can simply write "global" handler for those exceptions and simply "display" error that is sent along, nevertheless you should not.
You should write an API adaptor for Twitch API package and handle errors / exceptions inside those methods instead of using Twitch package right in controller or model. Further more you should delegate making request to dedicated server (such as Twitch) for queued job.
To answer your question:
Are HTTP status codes in APIs good practice?
Yes, where else would you want to use status codes? API is the perfect place.
Status codes are easy to compare, easy to understand. Messages sent along are just meta information for human to "understand" what is going on.
Part below is way off the scope of the question:
You should never change package code itself (except when testing), instead clone/fork package, make changes you need and use composer to load your version instead.
composer.json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/username/repository"
}
],
"require": {
.
.
.
"original-repo-package": "dev-branch-form-your-repo as 1.0.0"
}
I have done research and I can't seem to find a solution. I have a website running XenForo and added XenAPI to the Software.
When you call the API: ( api.php?action=authenticate&username=USERNAME&password=PASSWORD)
it returns the following JSON:
{
"hash": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
}
I am trying to find the "proper" way of capturing the JSON and decoding it. I hope you guys can help! Note that I am looking for a example to learn from, because all other examples fail.
For simple requests with 1-2 params, you can use
$json = file_get_contents(url...);
$response = json_decode($json, true);
For more difficult queries use curl extension.
For full web-applications use Guzzle - curl wrapper for communicates with API.
I have a HTTP JSON API, which runs on php, on a small framework. This API is a wrapper for a databases pgsql functions.
Php framework returns responses in such way:
{
code: 200,
data: []
}
Codes are HTTP code responses (such as 200, 301, 302, etc). pgsql functions returns their own code (negative values for errors, positive for success results), message (meaning of code) and result data:
{
code: -1,
message: 'Wrong data',
data: []
}
So, my packages from API are:
{
code: 200,
data: {
code: 1
message: 'Succeed'
data: []
}
}
Isn't it messy?
Occur some confusions when writing client code, that requests this API.
Maybe there are some standard patterns for making some kind of packages of API.
Your API layout is not messy. As Botond suggested, it is actually pretty logical. The only change I would make to it would be to move your status codes into HTTP headers rather than in the JSON data, to reduce the format a bit. This will also allow you to easily differentiate between successful calls and errors.
Suppose your API can answer with 4 different codes: 200, 201, 403, 404. Respectively: done, not changed, forbidden, not found. Instead of passing this as a JSON variable, you could easily bind it into the HTTP response header, as the values already exist and are well understood. This, as in this question, is a pretty well-accepted method of providing status codes, provided that you are not using this specific header for anything else.
See you have to read the responses in Iterative manner. You can read the JSON response and then check if the data field has another object/array.
You have to assess the code and show error messages on all codes except 200.
I'm using the (JSON) HTTP interface to communicate with SharePoint. The communication itself is done via cURL and a convinience-wrapper in PHP. Problems arise, when I want to push data to SP.
Since I'm no Microsoft / SharePoint guy, I'm missing the proper vocabulary to explain my needs. I'll therefore demonstrate using data I received from SharePoint.
GET http://example.org/mytest/_vti_bin/listdata.svc/Aufgaben(2) returns the following (truncated by me) data:
{
"d" : {
"__metadata": {
"uri": "http://example.org/mytest/_vti_bin/listdata.svc/Aufgaben(2)",
"etag": "W/\"5\"",
"type": "Microsoft.SharePoint.DataService.AufgabenItem"
},
"ID": 2,
"InhaltstypID": "0x010800821BC29B80192B4C960A688416597526",
"Inhaltstyp": "Aufgabe",
"Titel": "Neuer Titel",
"ZugewiesenAn": {
"__deferred": {
"uri": "http://example.org/mytest/_vti_bin/listdata.svc/Aufgaben(2)/ZugewiesenAn"
}
},
"ZugewiesenAnId": 29,
"F\u00e4lligkeitsdatum": "\/Date(1323993600000)\/"
}
}
"ZugewiesenAn" is a user. If I query the deferred values, I get (truncated by me, again)
{
"d" : {
"__metadata": {
"uri": "http://example.org/mytest/_vti_bin/listdata.svc/Benutzerinformationsliste(29)",
"etag": "W/\"1\"",
"type": "Microsoft.SharePoint.DataService.BenutzerinformationslisteItem"
},
"InhaltstypID": "0x010A000719C31710976A48867763D86F6586E0",
"Name": "Rehm Rodney",
"Konto": "EXT\\rodney.rehm",
"ID": 29,
"Inhaltstyp": "Person",
}
}
So I can see that the value of "ZugewiesenAn" should be "EXT\rodney.rehm" (as I need the username). Thus far, no problem.
The question is how I create a new or update an existing object with a different user for "ZugewiesenAn" (a User/Group field)?
I've tried:
Sending the username as the value of "ZugewiesenAn" or "ZugewiesenAnId" results in a Bad Request.
Querying http://example.org/_vti_bin/People.asmx (SOAP: SearchPrincipals) only yields numeric IDs for people that have actually worked with the list. If I query a username that hasn't logged into that SharePoint list before, I get ID -1.
I could not find out how to add users to the userlist via REST. You can, however, use the SOAP ResolvePrincipal request (example) - which does the job!
I am not a SharePoint guy and focus mostly on REST and OData. But think that REST OData API for SharePoint follows common rules for REST OData.
Common rules for REST and OData is to use different HTTP verbs for different operations. Read, create, update, and delete operations are mapped directly to GET, POST, PUT, and DELETE HTTP verbs.
So, you are getting your user by GET HTTP verb on URI http://example.org/mytest/_vti_bin/listdata.svc/Benutzerinformationsliste(29)
To delete this user use verb DELETE on the same URI and user id with empty HTTP message body.
To create user HTTP verb POST, same URI and json in message body. Also while creating ID shouldn't be specified (except situations when ID isn't auto-incremented in databases). Content-Type for HTTP message should be set: application/json for JSON.
The same situation with update - PUT, same URI
http://example.org/mytest/_vti_bin/listdata.svc/Benutzerinformationsliste(29)
and json in HTTP message body with content-type:application/json.
Format of json should be the same as you've received.
{
"InhaltstypID": "0x010A000719C31710976A48867763D86F6586E0",
"Name": "Rehm Rodney",
"Konto": "EXT\\rodney.rehm",
"ID": 29,
"Inhaltstyp": "Person",
}