SkriptWebAPI

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

faketuna

New Member
Dec 29, 2022
3
1
3
Addon in alpha:
This Addon is in alpha, so cause probrems please use at your own risk.


Features:
Web API Server feature:
  • Handle DELETE, GET, POST, PUT request.
Web request feature:
  • Send DELETE, GET, POST, PUT request.
Install:

Note:

  • Current version (0.0.1) is can only work with paper server. Im plan to make it work with Spigot in the future.
  • Current version (0.0.1) is only run with Minecraft version 1.19.3. Older version is supporting soon.
Installing:
Same as other common addons.
  1. Download SkriptWebAPI-<Version>.jar from here. Lager number mean recent.
  2. Put your downloaded jar to server plugins folder
  3. Run and ready to use
Documentation:
Example and syntax has provided in Skript Hub.




Important:
If you use reply effect in if statement, you need exit after reponse for avoid error
code_language.skript:
if (something):
   reply %httpexchange% with body %string% and response code 200
   exit
Also you need reply %httpexchange% as not found in last line of event

code_language.skript:
on receive get request:
   set {_request} to event-request

   if (something):
       reply {_request} with body %string% and response code 200
       exit
 
   reply {_request} as not found

Example:

code_language.skript:
# Written in plugin version 0.0.1

on skript load:
    start http server in port 8080

on skript unload:
    stop http server

command get:
    trigger:
        set {_header} to blank header
        set {_header}'s properties "Content-Type" to "applicaiton/json"
        set {_header}'s properties "User-Agent" to "SkriptWebAPI/0.0.1"
        set {_header}'s properties "Custom" to "Custom header"
        set {_a} to response of get request to "http://httpbin.org/get" with header {_header}
        broadcast "response: %{_a}%"

command put:
    trigger:
        set {_header} to blank header
        set {_header}'s properties "Content-Type" to "applicaiton/json"
        set {_header}'s properties "User-Agent" to "SkriptWebAPI/0.0.1"
        set {_header}'s properties "Custom" to "Custom header"
        set {_a} to response of put request to "http://httpbin.org/put" with header {_header} and body "{""json"":""value""}"
        broadcast "response: %{_a}%"

command post:
    trigger:
        set {_header} to blank header
        set {_header}'s properties "Content-Type" to "applicaiton/json"
        set {_header}'s properties "User-Agent" to "SkriptWebAPI/0.0.1"
        set {_header}'s properties "Custom" to "Custom header"
        set {_a} to response of post request to "http://httpbin.org/post" with header {_header} and body "{""json"":""value""}"
        broadcast "response: %{_a}%"

command delete:
    trigger:
        set {_header} to blank header
        set {_header}'s properties "Content-Type" to "applicaiton/json"
        set {_header}'s properties "User-Agent" to "SkriptWebAPI/0.0.1"
        set {_header}'s properties "Custom" to "Custom header"
        set {_a} to response of delete request to "http://httpbin.org/delete" with header {_header} and body "{""json"":""value""}"
        broadcast "response: %{_a}%"
  
on receive get request:
    set {_request} to event-request
    set {_path} to event-path parsed as text

    if ({_path} is "/user"):
        set {_user} to number of all players
        set {_body} to "{""user"":""%{_user}%""}"
        set {_request}'s response header "Content-Type" to "application/json"
        reply {_request} with body {_body} and response code 200
        exit

    reply {_request} as not found

on receive put request:
    set {_request} to event-request
    set {_path} to event-path parsed as text


    if ({_path} contains "/send/"):
        set {_query} to {_path}
        replace "/send/" in {_query} with ""
        set {_query::*} to split {_query} at "/"

        set {_user} to {_query::1} parsed as offline player
        set {_message} to {_query::2}
  
        if ({_user} is online):
            send {_message} to {_user}
            set {_body} to "{""sent"":true}"
        else:
            set {_body} to "{""sent"":false}"
  
        set {_request}'s response header "Content-Type" to "application/json"
        reply {_request} with body {_body} and response code 200
        exit

    reply {_request} as not found


on receive post request:
    set {_request} to event-request
    set {_path} to event-path parsed as text
    set {_body} to ""

    # If URI is http://domain/add-value/list/value
    if ({_path} contains "/add-value/"):
        set {_query} to {_path}
        replace "/add-value/" in {_query} with ""
        set {_query::*} to split {_query} at "/"


        if (size of {_query::*} >= 3):
            set {_body} to "{""error"":""To may arguments""}"
            reply {_request} with body {_body} and response code 400
            exit
  
        # {_query::1} is "list"
        # {_query::2} is "value"
 
        if ({%{_query::1}%::*} is not set):
            set {_body} to "{""list"":""not found""}"
        else:
            # add "value" to {list::*}
            add {_query::2} to {%{_query::1}%::*}
            set {_body} to "{""list"":""added""}"

        set {_request}'s response header "Content-Type" to "application/json"
        reply {_request} with body {_body} and response code 200
        exit
  
    reply {_request} as not found

on receive delete request:
    set {_request} to event-request
    set {_path} to event-path parsed as text

    if ({_path} contains "/delete-variable/"):
        set {_query} to {_path}
        replace "/delete-variable/" in {_query} with ""
        set {_query::*} to split {_query} at "/"

        if ({%{_query::1}%} is not set):
            set {_body} to "{""variable"":""not found""}"
        else:
            delete {%{_query::1}%}
            set {_body} to "{""variable"":""removed""}"

        set {_request}'s response header "Content-Type" to "application/json"
        reply {_request} with body {_body} and response code 200
        exit


    reply {_request} as not found

Todo:
Todo is found in my github readme.

Contributors:
Rily_: wrote the addon icon.
 
faketuna updated SkriptWebAPI with a new update entry:

Version 0.1.1 | Bug fix and new expression

What's Changed
* Fix: java.io.IOException: too many bytes to write to stream error by faketuna in https://github.com/faketunaPrivateCamp/SkriptWebAPI/pull/15
* Add get remote IP expression in HTTP server by faketuna in https://github.com/faketunaPrivateCamp/SkriptWebAPI/pull/16

New expression doc is here

Full Changelog: https://github.com/faketunaPrivateCamp/SkriptWebAPI/compare/0.1.1...v0.1.2

Read the rest of this update entry...
 
  • Like
Reactions: BaeFell