com.mp.GW2APIDriver.java Source code

Java tutorial

Introduction

Here is the source code for com.mp.GW2APIDriver.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mp;

import com.mp.gw2api.GW2API;
import com.mp.gw2api.GW2APIEndpoints;
import com.mp.gw2api.data.GW2APIAccount;
import com.mp.gw2api.data.GW2APIColor;
import com.mp.gw2api.data.GW2APIMap;
import com.mp.gw2api.lists.GW2APIColorList;
import com.mp.gw2api.lists.GW2APIMapList;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Wrapper class and driver for the GW2API class.
 * @author Mitchell Pell
 */
public class GW2APIDriver implements Runnable {

    /**
    * Members
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

    /*! Guild Wars 2 API Interface */
    private GW2API m_api;

    /**
     * Constructors
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

    /*! Default constructor, no API Key */
    public GW2APIDriver() {
        m_api = new GW2API();
    }

    /*! Overloaded constructor, API Key supplied */
    public GW2APIDriver(String api) {
        m_api = new GW2API(api);
    }

    /*! Overloaded constructor information supplied from argument array. */
    public GW2APIDriver(String[] api) {
        if (api.length <= 0)
            m_api = new GW2API("C0D2766E-48EF-784B-9FD2-1F3EDE1E858FA688B691-" + "72AB-4B33-B364-D5E7408E8A33");
        else
            m_api = new GW2API(api[0]);
    }

    /**
     * Operations
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/

    /**
     * Program start.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        GW2APIDriver driver = new GW2APIDriver(args);
        driver.run();
    }

    /*! Main run operation for driver. */
    @Override
    public void run() {
        //Getting an Array (Maps)
        JSONArray ja = m_api.GetJSONArray(GW2APIEndpoints.POINTS.V2_MAPS, //GW2 API (vw) Endpoint
                null, // 
                null); // Additional Parameters
        //Getting an Object (Map)
        JSONObject jo = m_api.GetJSONObject(GW2APIEndpoints.POINTS.V2_MAPS, //GW2 API (vw) Endpoint
                "/15", // 
                null); //

        GetAccountTest();
        //GetMapsTest(); 
        //GetColorsTest();      
    }

    private void GetAccountTest() {
        System.out.println("Getting Account Information:");
        GW2APIAccount account = (GW2APIAccount) m_api.GetData(GW2APIEndpoints.POINTS.V2_ACCOUNT, null, null);
        System.out.println("\t Fetched Account:");

        if (account.errorText != null)
            System.out.println("\t\t Error: " + account.errorText);
        else {
            System.out.println("\t\t Name: " + account.name);
            System.out.println("\t\t ID: " + account.id);
            System.out.println("\t\t Commander: " + account.commander);
        }
    }

    private void GetColorsTest() {
        int max = 4;
        System.out.println("Getting First " + max + " Colors:");
        try {

            //Get the maplist from the API
            GW2APIColorList colorList = (GW2APIColorList) m_api.GetData(GW2APIEndpoints.POINTS.V2_COLORS, null,
                    null);
            //Create a list of map data objects.
            List<GW2APIColor> colors = new ArrayList<>();
            //Load the map data objectts with first 25 maps in list.
            for (int i = 0; i < max; i++) {
                //get map id from list
                long colorID = colorList.maps[i];
                //Get map data object from the API.
                GW2APIColor color = (GW2APIColor) m_api.GetData(GW2APIEndpoints.POINTS.V2_COLORS, "/" + colorID,
                        null);
                //Store the map data object in the array.
                colors.add(color);
                System.out
                        .println("\t Fetched Color (" + i + "/" + max + "): " + color.name + "(" + color.id + ")");
            }
            int ff = 22;
        } catch (Exception ex) {
            Logger.getLogger(GW2APIDriver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*! Test function for retrieving the map list and map data. */
    private void GetMapsTest() {
        int max = 4;
        System.out.println("Getting First " + max + " Maps:");
        try {

            //Get the maplist from the API
            GW2APIMapList mapList = (GW2APIMapList) m_api.GetData(GW2APIEndpoints.POINTS.V2_MAPS, null, null);
            //Create a list of map data objects.
            List<GW2APIMap> maps = new ArrayList<>();
            //Load the map data objectts with first 25 maps in list.
            for (int i = 0; i < max; i++) {
                //get map id from list
                long mapID = mapList.maps[i];
                //Get map data object from the API.
                GW2APIMap map = (GW2APIMap) m_api.GetData(GW2APIEndpoints.POINTS.V2_MAPS, "/" + mapID, null);
                //Store the map data object in the array.
                maps.add(map);
                System.out.println("\t Fetched Map (" + i + "/" + max + "): " + map.name + "(" + map.id
                        + ") Levels: " + map.minLevel + "-" + map.maxLevel);
            }

        } catch (Exception ex) {
            Logger.getLogger(GW2APIDriver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}