NATION

PASSWORD

[API] NationStates API for .NET

Bug reports, general help, ideas for improvements, and questions about how things are meant to work.
User avatar
Auralia
Senator
 
Posts: 4982
Founded: Dec 15, 2011
Ex-Nation

[API] NationStates API for .NET

Postby Auralia » Fri Oct 10, 2014 4:39 pm

NationStates API for .NET is no longer maintained.

NationStates API for .NET

Summary

NationStates API for .NET is a free and open source library that allows .NET applications to easily access the NationStates API without worrying about making HTTP requests, decoding XML or rate limiting.

Features

  • Complete interface to the NationStates API (supports the nation, region, world, World Assembly, telegram, authentication and data dump APIs)
  • Rate-limiting to prevent blocked access to the API, including for the telegram API (recruitment and non-recruitment)
  • XML decoding (all data is returned in simple C# data structures)
  • Currently supports version 7 of the API

Documentation

System Requirements

Help
The .NET interface is relatively intuitive, and all methods and properties are documented using standard C# XML documentation. You will need to have a working understanding of the NationStates API, whose documentation is available here.

Examples
The following is a simple example (written in C#) that retrieves a nation's full name and prints it to the console:

Code: Select all
using Auralia.NationStates.Api;

class SimpleExample {

    static void Main(string[] args) {

        // Create main API object
        var api = new Api("<INSERT USER AGENT HERE>");

        // Specify shards to be requested from the nation API
        // (In this case, the "fullname" shard, which represents the nation's full name)
        var nationShards = new NationShards();
        nationShards.FullName = true;

        // Retrieve those shards from the nation API
        var nationData = api.CreateNationApiRequest("<INSERT NATION HERE>", nationShards);

        // Print the nation's full name
        Console.WriteLine(nationData.FullName);

        // Prevent the console from closing immediately
        Console.ReadKey();
    }
}


The following is a more complex example that retrieves and sorts a list of nations in a region by their influence score, then prints the list to the console:

Code: Select all
using Auralia.NationStates.Api;

class ComplexExample {

    static void Main(string[] args) {

        // List of nations and their corresponding influence scores
        var influence = new List<Tuple<string, double>>();

        // Constant representing the census ID for influence
        var influenceId = 65;

        // Create main API object
        var api = new Api("<INSERT USER AGENT HERE>");

        // Specify shards to be requested from the region API
        // (In this case, the "nations" shard, which retrieves a list of nations in the region)
        var regionShards = new RegionShards();
        regionShards.Nations = true;

        // Retrieve those shards from the region API
        var regionData = api.CreateRegionApiRequest("<INSERT REGION HERE>", regionShards);
        var nations = regionData.Nations;

        // For each nation, request its name and influence score from the API and add it to the list
        for (var i = 0; i < nations.Length; i++)
        {
            var nationShards = new NationShards();
            nationShards.Name = true;
            nationShards.CensusStatistics = true;
            nationShards.CensusStatisticsIds = new List<int?>(new int?[] { influenceId });

            var nationData = api.CreateNationApiRequest(nations[i], nationShards);

            var name = nationData.Name;
            var score = nationData.CensusStatistics[0].Value.Value;
            influence.Add(new Tuple<string, double>(name, score));
        }

        // Order the list, first by nation name and then by influence score
        influence = influence.OrderBy(x => x.Item1).ToList();
        influence = influence.OrderByDescending(x => x.Item2).ToList();

        // Print the influence score of each nation in the region
        Console.WriteLine(String.Format("{0, -50} {1, -10}", "Nation", "Influence"));
        Console.WriteLine("================================================== ==========");
        foreach (Tuple<string, double> tuple in influence)
        {
            Console.WriteLine(String.Format("{0, -50} {1, -10}", tuple.Item1, tuple.Item2));
        }

        // Prevent the console from closing immediately
        Console.ReadKey();
    }
}


Downloads

Source code and binary downloads are available from the project's GitHub page. You can access the source and binary downloads for the latest version (0.2) here.

Copyright and License

Copyright (C) 2013 Auralia.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Last edited by Auralia on Sat Dec 23, 2017 9:04 am, edited 1 time in total.
Catholic Commonwealth of Auralia
"Amor sequitur cognitionem."

User avatar
Eluvatar
Director of Technology
 
Posts: 3086
Founded: Mar 31, 2006
New York Times Democracy

Postby Eluvatar » Tue Oct 14, 2014 7:43 pm

Does this library assume you have only one process using the API?
To Serve and Protect: UDL

Eluvatar - Taijitu member

User avatar
Railana
Diplomat
 
Posts: 518
Founded: Apr 11, 2014
Ex-Nation

Postby Railana » Tue Oct 14, 2014 8:43 pm

Eluvatar wrote:Does this library assume you have only one process using the API?


For the purposes of rate-limiting, yes. This is a known limitation that will ideally be fixed in a future version.
Dominion of Railana
Also known as Auralia

"Lex naturalis voluntas Dei est."

User avatar
Eluvatar
Director of Technology
 
Posts: 3086
Founded: Mar 31, 2006
New York Times Democracy

Postby Eluvatar » Tue Oct 14, 2014 8:56 pm

If we could cooperate on interoperability that might be good.

https://github.com/Eluvatar/trawler-parser-python may be relevant, but the thing that'd be shared would be a (possibly revised) https://github.com/Eluvatar/trawler-daemon-c

The daemon serves as the rate limiter for my stuff by being the intermediary between all my (recent) scripts and NS.net. It may be overengineered in that A) the daemon is in C and B) the protocol is in protocol buffers over zeromq -- but hey, it's efficient! :P

Thoughts?
To Serve and Protect: UDL

Eluvatar - Taijitu member

User avatar
Railana
Diplomat
 
Posts: 518
Founded: Apr 11, 2014
Ex-Nation

Postby Railana » Sun Oct 19, 2014 5:51 pm

Eluvatar wrote:If we could cooperate on interoperability that might be good.


If that's possible, certainly. :)

Do you have any documentation for the C daemon? There doesn't seem to be much on GitHub.
Dominion of Railana
Also known as Auralia

"Lex naturalis voluntas Dei est."

User avatar
Eluvatar
Director of Technology
 
Posts: 3086
Founded: Mar 31, 2006
New York Times Democracy

Postby Eluvatar » Sun Oct 19, 2014 6:50 pm

Railana wrote:
Eluvatar wrote:If we could cooperate on interoperability that might be good.


If that's possible, certainly. :)

Do you have any documentation for the C daemon? There doesn't seem to be much on GitHub.


Heh. Currently the main piece of documentation is the .proto file <_< That documentation should continue to be relevant even if you get me to drop zeromq.

(NB: The line about GET or POST being valid is about to become out of date with the addition of HEAD requests.) (The chunking branch is so as to support downloading the xml dumps through trawler, as otherwise it'd be a bit inefficient, and the addition of support for HEAD requests is related).

I could put some documentation in the Readme, also :blush: Haven't quite done it yet because I wanted to at least support the 10/minute non-api ratelimit in the rules as well before considering it v1.0... and probably also set up travis properly and make a spec file for RPMs/DEBs before widely publicizing it.
Last edited by Eluvatar on Sun Oct 19, 2014 6:54 pm, edited 1 time in total.
To Serve and Protect: UDL

Eluvatar - Taijitu member


Return to Technical

Who is online

Users browsing this forum: Chiho, Darcania, Haganham, Phydios, Stratocratic-Anarchy Oceanic Empire, Transitional Global Authority

Advertisement

Remove ads