Skip to main content

A Python wrapper for the Politics and War API.

Project description

pnwkit-py

Logo

pnwkit-py

Politics & War API Library
Explore the docs

JavaScript/TypeScript Version - Report Bug - Request Feature

pnwkit-py is here to make interacting with the V3 Politics and War API easy. All you have to do is import the library, add your key, and make a query.

Getting Started

To get started using pnwkit-py you must first have Python and PIP installed.

Installing

Python 3.9 or higher is required.

Install the library using PIP.

# Linux/MacOS
python3 -m pip install -U pnwkit-py

# Windows
py -3 -m pip install -U pnwkit-py

Usage

To use pnwkit-py just import the library, create a QueryKit, then you can make synchronous or asynchronous queries.

import pnwkit
kit = pnwkit.QueryKit("YOUR_API_KEY")

query = kit.query("nations", {"id": 251584, "first": 1}, "nation_name")
# get synchronously
result = query.get()
# get asynchronously
result = await query.get_async()
# OR
result = await query

print(f"Nation name: {result.nations[0].nation_name}")

If you want to paginate your query for more results, just ask to paginate the query. Instead of returning a tuple of results, pnwkit will return a Paginator object which you can iterate through. For asynchronous queries you can use async for to iterate through the results. In addition, async paginators support batching queries to perform multiple queries simultaneously.

# .batch is async only, will perform 2 queries
# when it runs out of results instead of one at a time
nations = query.paginate("nations")
# async only
async_nations = query.paginate("nations").batch(2)

for nation in nations:
    print(f"Nation name: {nation.nation_name}")
async for nation in nations:
    print(f"Nation name: {nation.nation_name}")
print(f"Current page: {nations.paginator_info.currentPage}")

The queries are written in normal GraphQL, so you can get all the cities in a nation like this

query = kit.query("nations", {"id", 251584, "first": 1},
  """
  nation_name
  cities {
    name
  }
  """
)
result = query.get()

print(f"First city of {result.nations[0].nation_name}: {result.nations[0].cities[0].name}")

Unlike the JavaScript/TypeScript and Google Apps Script libraries, the Python library has a few additional features.

  • Support for subscriptions
async def callback(nation):
  ... # this function will be called every time an event is received
  # nation is a Nation object with the updated fields

subscription = await kit.subscribe("nation", "update")
async for nation in subscription:
  ... # here nation is a Nation object with the updated fields
  • Additional arguments on a query will be concatenated with the first to form the query.
  • You can also just pnwkit.Field to get support for nested fields without using raw GraphQL.
query = kit.query("nations", {"id", 251584, "first": 1}, "nation_name", pnwkit.Field("cities", {}, "name"))
result = query.get()

print(f"First city of {result.nations[0].nation_name}: {result.nations[0].cities[0].name}")
  • Keyword arguments provided to a query function will be passed in as query variables.
  • When pnwkit.Variable, check the API docs for the correct type for your argument.
query = kit.query("nations", {"id": pnwkit.Variable("id", pnwkit.VariableType.INT_ARRAY), "first": 1}, "nation_name", pnwkit.Field("cities", {}, "name"), id=251584)
# variables can also be set with the set_variables method
query.set_variables(id=251584)
result = query.get()


print(f"First city of {result.nations[0].nation_name}: {result.nations[0].cities[0].name}")
  • Extensions to access the daily data dumps and scrape data from the game.
  • Access to the bankWithdraw and bankDeposit mutations.
# the API requires a verified bot key to use mutations
kit = pnwkit.QueryKit("YOUR_API_KEY", bot_key="YOUR_BOT_KEY", bot_key_api_key="YOUR_BOT_KEY_API_KEY")

query = kit.mutation("bankDeposit", {"money": 100}, "id")
result = query.get()

print(f"Deposited ${result.bankDeposit.money} as bank record #{result.bankDeposit.id}")
  • Query fields as aliases
query = kit.query_as("nations", "the_nations", {"id": 251584, "first": 1}, "nation_name", pnwkit.Field("cities", {}, "name"))
result = query.get()

print(f"First city of {result.the_nations[0].nation_name}: {result.the_nations[0].cities[0].name}")
  • Ordering results
query = kit.query("nations", {"orderBy": pnwkit.OrderBy("date", pnwkit.Order.ASC)}, "nation_name")
result = query.get()

print(f"Oldest nation {result.nations[0].nation_name}")

You can look at the arguments and possible data to collect here by experimenting on the GraphQL Playground.

Moving Forward

  • Improved support for query variables
  • Argument typings
  • In-built cache management with subscriptions
  • Support for query fragments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pnwkit-py-2.6.22.tar.gz (58.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pnwkit_py-2.6.22-py3-none-any.whl (64.7 kB view details)

Uploaded Python 3

File details

Details for the file pnwkit-py-2.6.22.tar.gz.

File metadata

  • Download URL: pnwkit-py-2.6.22.tar.gz
  • Upload date:
  • Size: 58.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pnwkit-py-2.6.22.tar.gz
Algorithm Hash digest
SHA256 ea044a17c73faa09ec61b8f4cbc47d749b4ed04594b000824f52af64b42c6a96
MD5 7feeb2ad68a63b663969083a1b6c3eea
BLAKE2b-256 4dca64ef8151e3b6775ecf5ad8a126cfee5c1307b5aac6411623c3276bc8208a

See more details on using hashes here.

File details

Details for the file pnwkit_py-2.6.22-py3-none-any.whl.

File metadata

  • Download URL: pnwkit_py-2.6.22-py3-none-any.whl
  • Upload date:
  • Size: 64.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pnwkit_py-2.6.22-py3-none-any.whl
Algorithm Hash digest
SHA256 2c7b481e270ea46bf70ba5a5de061c9d8cb88dd47ad03aa4f5bd00f05ee3e332
MD5 2e23b998956fae003a7ed12ee86df858
BLAKE2b-256 76e2097b8b74d62d32788f75c11cf77bc3d41d9ac85a8f51cad65b7f7f79a343

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page