Coder Social home page Coder Social logo

trooptrack's Introduction

TroopTrack perl API

Overview

swagger-codegen generated perl libraries for TroopTrack.

TroopTrack runs on the 1.2 version of the Swagger schema. All existing code that I could find for generating code from the schema and thus having something that is simple to maintain and keep up to date uses 2.0. After wrangling with the tools I decided that the best thing to do would be to generate a 2.0 schema from the 1.2 schema.

The NPM package swagger-tools made this reasonably simple.

Usage

  1. Modify config.json for your environment:
{
    "api_url":            "https://trooptrack.com:443/api/swagger_doc",
    "java":               "/usr/bin/java",
    "swagger-codegen":    "../swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar",
    "swagger-tools":      "/opt/local/bin/swagger-tools",
    "swagger2_dir":       "swagger2",
    "swagger2_schema":    "tt.json",
    "swagger12_dir":      "swagger1.2"
}
  1. Pull the 1.2 schema, make necessary tweaks, convert to 2, and
$ perl generate.pl
  1. Use the generated library:
use strict;
use warnings;
use lib qw( lib );
use Data::Dumper;

# implicitly needs Log::Any
#                  URI::Query
use WWW::SwaggerClient::UsersApi;

my $users = WWW::SwaggerClient::UsersApi->new();

my $partner_token = "<your partner token>";
my $user_token = "<your user token>";

# Get a list of users
# note the relatively mangled method names.  This is a result of the 1.2
# schema nicknames and the swqgger-codegen algorithm.
my $ret = $users->g_et_version_users( 'x_partner_token' => $partner_token,
                                      'x_user_token'    => $user_token );
# $ret contains the json output 
print $ret;

Conversion Details

The Conversion from 1.2 to 2.0 is not perfect. A few points:

  • The 1.2 schema references 'String'. This is not understood by the converter. I change all instances of 'type': 'String' to 'type': 'string'. This had to happen before the conversion would work.
  • The 1.2 schema has implicit return values for most 200s. This in interpreted by swagger-codegen as no return. All operations thaht have a 'void' type have been converted to 'string' in order to get the JSON returned by the API. This then needs to be decoded into perl.

swagger-codegen

The conversion tool is swagger-codegen

This is a java library that supports the creation of API libraries in many languages including perl. Getting it built and working was tricky, but it is managable by following the instrictions on the referenced site. For reference I am doing it on OS X 10.10.

Contents

What is included:

  • generate.pl: script to fetch the 1.2 schema, convert it, and generate the perl library
  • example.pl: simple example for using the generated libraries to call the API
  • lib: directory that holds the perl libs. use libs './lib' to include for testing
  • swagger1.2: contains the 1.2 schemas ( pulled directly from TroopTrack )
  • swagger2.0: contains the 2.0 schema ( converted and modified as per above )
  • config.json: the config file that you should modify for your environment fro running generate.pl

Note that the contents of lib, swagger1.2, and swagger2 are all generated by the generate.pl script. They output of my conversion is included as it is very likley they will work for you out of the box.

TODO

  • Add api version checking ( is this in there implicitly already? )
  • Add testing

trooptrack's People

Contributors

sludin avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

joe-forbes

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.