Perl Example

NOTE: Get an APIKey.

Get a Session

 #!/usr/bin/env perl

 use Modern::Perl;
 use LWP::UserAgent;
 use HTTP::Request::Common;
 use JSON;
 use Data::Dumper;
 
 my $response = LWP::UserAgent->new->request( POST 'https://www.thegamecrafter.com/api/session', [
    username    => 'joe',
    password    => 'abc123',
    api_key_id  => '', # get your own
 ]);

 my $result = from_json($response->decoded_content); 
 if ($response->is_success) {
    say 'Session ID: ', $result->{result}{id};
 }
 else {
    die 'Error: ', $result->{error}{message};
 }

 my $session = $result->{result};

Fetch My Account Info

 my $response = LWP::UserAgent->new->request( GET 'https://www.thegamecrafter.com/api/user/'.$session->{user_id}, [
    session_id  => $session->{id},
 ]);

 my $result = from_json($response->decoded_content); 
 if ($response->is_success) {
    say Dumper($result->{result});
 }
 else {
    die 'Error: ', $result->{error}{message};
 }
 
 my $user = $result->{result};

Upload a File

 my $response = LWP::UserAgent->new->request( POST 'https://www.thegamecrafter.com/api/file', [
    name        => 'image.jpg',
    file        => \'/path/to/file.jpg',
    folder_id   => $user->{root_folder_id},
    session_id  => $session->{id},
 ]);

 my $result = from_json($response->decoded_content); 
 if ($response->is_success) {
    say Dumper($result->{result});
 }
 else {
    die 'Error: ', $result->{error}{message};
 }

Search Games

 my $response = LWP::UserAgent->new->request( GET 'https://www.thegamecrafter.com/api/game', [
    q           => 'Steampunk',
    session_id  => $session->{id}, # optional
 ]);

 my $result = from_json($response->decoded_content); 
 if ($response->is_success) {
    say Dumper($result->{result});
 }
 else {
    say 'Error: ', $result->{error}{message};
 }

Session ID is optional for this.

Perl Client Wrapper

You can make this even easier on yourself by using the Perl client wrapper for TGC: https://metacpan.org/pod/TheGameCrafter::Client

 use TheGameCrafter::Client;
 
 my $session = tgc_post('session', { username => 'me', password => '123qwe', api_key_id => 'abcdefghijklmnopqrztuz' });

 my $user = tgc_get('user/'.$session->{user_id}, {session_id => $session->{id}});
 
 my $file = tgc_post('file', {name => 'image.jpg', file => \'/path/to/file.jpg', folder_id => $user->{root_folder_id}, session_id => $session->{id}});
 
 my $games = tgc_get('game', {q => 'Steampunk', session_id => $session->{id}});
^ Back to Top ^