Raised This Month: $85 Target: $400
 21% 

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
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
I do not provide support in PMs. Please use the forum 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 online now
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 14:17.


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