Web-API [v4] - AdminPanel & RESTful web server [now with screenshots!]

Well, nope :stuck_out_tongue:

I can only get the events-all hook to work.

Here is my config : https://pastebin.com/Qmb7ka3v
event_join.php : https://pastebin.com/NMViJXDi
event_log.txt (tried with login, suicide, get wood, logout) : https://pastebin.com/rxHk3uzq
console output : [14:59:25 INFO]: Done (3,813s)! For help, type "help" or "?"[14:59:26 INFO] [w - Pastebin.com

No error during startup, still using a fresh install with latest vanilla sponge 5.2

What is wrong in my config ?

Edit : added console ouptut pastebin

Thank you for all the log files, I found the error, it has to do with the config files (once again, I honestly HATE how Sponge currently solved thisā€¦). Iā€™ll try and fix this as soon as I can.

@Keuterio for now I suggest using the all event hook, and then checking $_SERVER for the ā€œX-WEBAPI-EVENTā€ header that will tell you which event it is

I do my best to provide enough information itā€™s easier for you to fix the problem :slight_smile:

Nice suggestion, I now listen to $_SERVER['HTTP_X_WEBAPI_EVENT'] and it works like a charm :

However, I can wait for next update as I am not in a hurry.

Donā€™t you have any support button ?

Edit : By chance, could you had hook ā€œuseā€ (button press) ? And if possible, ā€œopenā€ (open chest) ?. Only if they are not fired too often, I donā€™t want to overload the requests.

Was gonna ask, is it possible for us to add sponge (or other plugin) events onto the hooks file in order to notify the server about these events being triggered and respond to it accordingly.
(I believe GriefPrevention is adding claimEnter and claimLeave events and want the server to be notified when they happen)

2 Likes

Very interesting idea. I could definitely add that. Iā€™ll have to look into how to turn the event data into nice json.

3 Likes

Even better if we can hook in whatever event we like. That could lead to a real interest for a server website.

I hope @Valandur will find an easy way to implement it :slight_smile:

Sorry for the late reply @Keuterio and @tridaak.

Iā€™ve been working on a new version that supports custom events and serializers, and am happy to say that Iā€™m almost done. Most of the stuff is in the git repository but I havenā€™t released a new version yet because it needs more testing and documentation.

How this is going to work is that you can specify any event you want to subscribe to. In case youā€™re not sure about the events you can check the /class endpoint which now supports looking up subclasses of a certain class. The Web-API will serialize all data which it knows how to into json. But if thatā€™s not good enough you can now write your own serializer which will get compiled and loaded when the server runs.

Let me make a short example:

You have to create a class that extends from the Jackson StdSerializer. The file has to have the same name as the class. E.g. the following file could be placed into the /webapi/serializers folder (not the config folder):

package serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import me.ryanhamshire.griefprevention.api.event.ClaimEvent;

import java.io.IOException;

public class ClaimEventSerializer extends StdSerializer<ClaimEvent> {

    public ClaimEventSerializer() {
        this(null);
    }

    public ClaimEventSerializer(Class<ClaimEvent> t) {
        super(t);
    }

    @Override
    public void serialize(ClaimEvent value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("owner", value.getOwnerName());
        gen.writeEndObject();
    }
}

Together with a custom event in config/webapi/hooks.conf along the lines of:

custom {
    "me.ryanhamshire.griefprevention.api.event.ClaimEvent"=[
        {
            address="http://localhost:25000?event=claim"
            method=POST
        }
    ]
}

Would then cause the ClaimEvent from GriefPrevention to be serialized and sent to the hook.

Iā€™m probably going to set up a repository or something to collect some common serializers.

Tell me what you guys think of that, and Iā€™ll probably be releasing a new version sometime this weekend.

:open_mouth: Amazing work, canā€™t wait till itā€™s done!

@Valandur coming with the new feature, only 3 days after initial suggestion :

Much incredible, very astonishing, wow.

Did you fix configuration issue as well ?

I have released version 3.1.0 with the following changes:

  • Add support for custom serializers
  • Rework class endpoint
  • Add ageable data
  • Add support for fetching custom fields and methods without an extra call
  • Add optional parameters to command hooks
  • Fix config files once again
  • Improve error responses
  • Rework hooks structure

Custom serializers now work and are documented.

You can also get arbitrary fields when getting the details of any player/entity/world/tile-entity by using the query parameters fields and methods (methods must have no arguments and must not return void).

A call would then look like this:
/player/{uuid}?fields=testField,anotherField&methods=methodWithNoArgs

1 Like

Wooooow, Iā€™m on my way to play with postman :smiley:

I prefer making a new post than editing the previous one because I have some updates.

After trying to figure out how serializers work, I saw the ā€œcustomā€ line in hooks. As a test, I tried with org.spongepowered.api.event.block.ChangeBlockEvent and org.spongepowered.api.event.block.InteractBlockEvent

The hook is added and the request is sent to the webserver but I have an issue with datas.

All I get is the position of the player and no information about the targetted block :
{"class":"org.spongepowered.api.event.ChangeBlockEvent$Break$Impl","cause":{"sou - Pastebin.com + http://json.parser.online.fr/

My goal is still to hook ā€œpress button eventā€ and ā€œopen chest eventā€.

Of course, thatā€™s a super nice update :wink:

Edit : it didnā€™t like org.spongepowered.api.event.entity.living.humanoid.HandInteractEvent [22:40:44 ERROR] [STDERR]: valandur.webapi.shadow.fasterxml.jackson.databind.Jso - Pastebin.com

What if you use the following serializer:

package serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.spongepowered.api.event.block.InteractBlockEvent;
import valandur.webapi.json.serializers.WebAPISerializer;

import java.io.IOException;

public class InteractEventSerializer extends WebAPISerializer<InteractBlockEvent> {
    @Override
    public void serialize(InteractBlockEvent value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeObjectField("cause", value.getCause());
        gen.writeObjectField("block", value.getTargetBlock());
        gen.writeEndObject();
    }
}

with the org.spongepowered.api.event.block.InteractBlockEvent?

There are still quite a few events that arenā€™t very well supported. Iā€™ll try and add them when I can.

The weird part is that it doesnā€™t load the serializer :

[10:11:16 INFO] [webapi]: Loading additional serializers... [10:11:16 INFO] [webapi]: Found 0 serializer files in C:\Users\Administrateur.DESKTOP-AG15HPJ\Documents\Minecraft\test 1.10\webapi [10:11:16 INFO] [webapi]: Starting Web Server...

I tried to name it InteractEventSerializer and InteractEventSerializer.class as well as putting it in server/webapi and server/webapi/serializers

Maybe itā€™s the wrong name ?

Edit : I donā€™t need to hook interactions with villagers, it was just to try an other event. Donā€™t bother looking at it now unless someone else need it.

Edit 2 : I named it .java in server/webapi/serializers and webapi found it but doesnā€™t like it :

Oh that is totally my bad, I forgot to mention that you have to name it .java.

Also it seems that you might need the jdk installed, not the jreā€¦ which might be rather cumbersum to do for most people. Iā€™ll see if I can fix/change that.

For now if you want to try it youā€™d have to install a JDK and use that to run the server. It shouldnā€™t affect anything else and you dont have to remove the JRE.

1 Like

So ClasseNameSerializer.java is the good name. Donā€™t worry, it will be useful for the documentation later :slight_smile:
By the way, why donā€™t you add hooks in redoc ? With examples and option, I could never think of ā€œdataTypeā€ without your advice.

I have access to my dedicated server so itā€™s not a problem for me and I think that advanced server owners who need to implement special hooks also own a ssh and sudo access. Maybe you can put a low priority on this issue. Can you install jre on your server tridaak ?

I installed JDK8 -not JRE8- on my computer, ran it again and here is what I got :

[NOTE] SpongePowered MIXIN Annotation Processor Version=0.6.8 [NOTE] ObfuscationServiceMCP supports type: "searge" [NOTE] ObfuscationServiceMCP supports type: "notch" [ERROR] Line 3:34 | package com.fasterxml.jackson.core does not exist [ERROR] Line 4:38 | package com.fasterxml.jackson.databind does not exist [ERROR] Line 5:41 | package org.spongepowered.api.event.block does not exist [ERROR] Line 6:40 | package valandur.webapi.json.serializers does not exist [ERROR] Line 10:46 | cannot find symbol symbol: class WebAPISerializerInteractBlockEvent [ERROR] Line 12:27 | cannot find symbol symbol: class InteractBlockEvent location: class serializers.InteractEventSerializer [ERROR] Line 12:53 | cannot find symbol symbol: class JsonGenerator location: class serializers.InteractEventSerializer [ERROR] Line 12:72 | cannot find symbol symbol: class SerializerProvider location: class serializers.InteractEventSerializer

No idea what does it meanā€¦

Also it seems like I am speaking too much :joy:

Havent gotten to really test it out, installed new version on my test server to see if itā€™ll run and itā€™s working. Had to change the port because commands for main server were going to test serverā€™s web-api, aside from that, the configuration is the same as the default.
So everything in the hooks.conf is either null or disabled.

Got the following today: [06:50:24] [Server thread/INFO] [STDERR]: [valandur.webapi.json.JsonConverter:to - Pastebin.com

Iā€™ll have more time to test stuff out, do have a private server where I can install JDK (seems thatā€™s whatā€™s needed), later when I have more free time. Otherwise @Keuterio you could try installing JDK and see if it works for you.

Hell, I meant ā€œI installed JKD8ā€, not JRE8 :x

Do you mind sharing the serializer you used? I believe this might have to do with the fact that I shadow the libraries, so the references are then incorrect.

Sorry for staying so quiet, Iā€™ve been quite busy lately.

A new version has been released for Web-API, it is available for download here.


  • Support SpongeAPI 7.0ā€¦0
  • Improve serializer feedback & support shadowed packages
  • Fix error for query strings without an equals sign [r15ch13]