Raised This Month: $12 Target: $400
 3% 

GraphQL API requests with REST in Pawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 09-01-2020 , 02:00   GraphQL API requests with REST in Pawn
Reply With Quote #1

If anyone is interested in making requests to a GraphQL API from a plugin, here is an example using REST in Pawn:

PHP Code:
#include <sourcemod>
#include <ripext>

#pragma newdecls required
#pragma semicolon 1

HTTPClient httpClient;

public 
void OnPluginStart()
{
    
httpClient = new HTTPClient("https://api.graph.cool/simple/v1/movies");

    
JSONObject variables = new JSONObject();
    
variables.SetString("title""Inception");

    
GraphQLRequest(
        
OnGraphQLResponse,
        
"query($title: String!) {"
        
... "    Movie(title: $title) {"
        
... "        releaseDate"
        
... "        actors {"
        
... "            name"
        
... "        }"
        
... "    }"
        
... "}",
        
variables
    
);

    
delete variables;
}

void GraphQLRequest(HTTPRequestCallback callback, const char[] queryJSONObject variables nullany value 0)
{
    
JSONObject data = new JSONObject();
    
data.SetString("query"query);

    if (
variables != null) {
        
data.Set("variables"variables);
    }

    
httpClient.Post("graphql"datacallbackvalue);
    
delete data;
}

void OnGraphQLResponse(HTTPResponse responseany value, const char[] error)
{
    if (
error[0]) {
        
PrintToServer("cURL Error: %s"error);
        return;
    }

    
JSONObject obj view_as<JSONObject>(response.Data);

    if (
obj.HasKey("errors")) {
        
JSONArray errors view_as<JSONArray>(obj.Get("errors"));
        
PrintErrors(errors);
        
delete errors;
    }

    if (
obj.HasKey("data") && !obj.IsNull("data")) {
        
JSONObject data view_as<JSONObject>(obj.Get("data"));
        
JSONObject movie view_as<JSONObject>(data.Get("Movie"));
        
JSONArray actors view_as<JSONArray>(movie.Get("actors"));

        
char releaseDate[25];
        
movie.GetString("releaseDate"releaseDatesizeof(releaseDate));
        
PrintToServer("Release date: %s"releaseDate);

        
JSONObject actor;
        
char actorName[25];

        for (
int i 0actors.Lengthi++) {
            
actor view_as<JSONObject>(actors.Get(i));
            
actor.GetString("name"actorNamesizeof(actorName));

            
PrintToServer("Actor name: %s"actorName);
            
delete actor;
        }

        
delete (datamovieactors);
    }
}

void PrintErrors(JSONArray errors)
{
    
JSONObject error;
    
char message[1024];

    for (
int i 0errors.Lengthi++) {
        
error view_as<JSONObject>(errors.Get(i));
        
error.GetString("message"messagesizeof(message));

        
PrintToServer("API Error: %s"message);
        
delete error;
    }

__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 09-02-2020 , 02:57   Re: GraphQL API requests with REST in Pawn
Reply With Quote #2

thump up

Last edited by Bacardi; 09-02-2020 at 02:57.
Bacardi is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:30.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode