Skip to main content

Synchronous / Asynchronous HTTPX extension for NationStates

Project description

sans

pypi Licensed under the MIT License Ruff pre-commit.ci status Documentation Status

Synchronous / Asynchronous NationStates

A fully typed extension for HTTPX for the NationStates API.

Installing

python3 -m pip install -U sans

Development version:

python3 -m pip install -U https://github.com/zephyrkul/sans/archive/master.zip#egg=sans

Support

If you need help with using sans, find a bug, or have a feature request, feel free to find me in the NS Coders Discord server, or file an issue.

Examples

Synchronous

import sans
from xml.etree import ElementTree as ET

def main():
   sans.set_agent("Darcania")
   request = sans.Nation(
      "darcania",
      "fullname population flag census",
      mode="score",
      scale="65 66",
   )
   root = sans.get(request).xml
   sans.indent(root)
   print(ET.tostring(root, encoding="unicode"))

   with sans.stream("GET", sans.RegionsDump()) as response:
      for region in response.iter_xml():
         sans.indent(region)
         print(ET.tostring(region, encoding="unicode"))

if __name__ == "__main__":
   main()

Asynchronous

import asyncio
import sans
from xml.etree import ElementTree as ET

async def main():
   sans.set_agent("Darcania")
   async with sans.AsyncClient() as client:
      request = sans.Nation(
         "darcania",
         "fullname population flag census",
         mode="score",
         scale="65 66",
      )
      root = (await client.get(request)).xml
      sans.indent(root)
      print(ET.tostring(root, encoding="unicode"))

      async with client.stream("GET", sans.RegionsDump()) as response:
         async for region in response.aiter_xml():
            sans.indent(region)
            print(ET.tostring(region, encoding="unicode"))

if __name__ == "__main__":
   asyncio.run(main())

Authentication

auth = sans.NSAuth(password="hunter2")
sans.get(sans.Nation("testlandia", "ping"), auth=auth)
# X-Autologin is automatically retrieved and stored for when the auth object is re-used
print(auth.autologin)
# X-Pin is cached internally for repeated requests
root = sans.get(sans.Nation("testlandia", "packs"), auth=auth).xml

Telegrams

limiter = sans.TelegramLimiter(recruitment=False)
# The Telegram API can be used without a TelegramLimiter, but marking it ahead of time can save an API call.
response = sans.get(sans.Telegram(client="abcd1234", tgid="1234", key="abcdef1234567890", to="testlandia"), auth=limiter)
assert response.content == b"queued"

Server-Sent Events

for event in sans.serversent_events(
   client, "move", "founding", "cte", "member", "endo"
).view(regions=["the north pacific"]):
   print(event["str"])

Command Line

sans --nation darcania census --scale "65 66" --mode score --agent Darcania
<NATION id="darcania">
   <CENSUS>
      <SCALE id="65">
         <SCORE>8145.00</SCORE>
      </SCALE>
      <SCALE id="66">
         <SCORE>0.00</SCORE>
      </SCALE>
   </CENSUS>
</NATION>

sans --nation testlandia fullname
<NATION id="testlandia">
   <FULLNAME>The Hive Mind of Testlandia</FULLNAME>
</NATION>

sans --region "the north pacific" numnations lastupdate
<REGION id="the_north_pacific">
   <LASTUPDATE>1685681810</LASTUPDATE>
   <NUMNATIONS>9535</NUMNATIONS>
</REGION>

sans --quit
No query provided. Exiting...

Requirements

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

sans-1.3.4.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

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

sans-1.3.4-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file sans-1.3.4.tar.gz.

File metadata

  • Download URL: sans-1.3.4.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sans-1.3.4.tar.gz
Algorithm Hash digest
SHA256 be65dcbff5c70ae3426b62abac3f3fc6db54468e0c27991f72a038cc99a2be12
MD5 0feae48d0ab5031d7fef8a0851ed8102
BLAKE2b-256 e7694f10894bf0fe8c36ed2019ebc459cde256e122cb3f8795ed08371bd50f36

See more details on using hashes here.

Provenance

The following attestation bundles were made for sans-1.3.4.tar.gz:

Publisher: pythonpublish.yml on Zephyrkul/sans

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sans-1.3.4-py3-none-any.whl.

File metadata

  • Download URL: sans-1.3.4-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sans-1.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1b1681fa3145f3fb74911d32aa836d502d76b5ab84e399cd3687778aec9f0c10
MD5 3e500b867287ae6d01e2e13894bbefc1
BLAKE2b-256 1c1164bff79a45d36865114375c52ac47edb5cab7fff521dfbfd728f76222603

See more details on using hashes here.

Provenance

The following attestation bundles were made for sans-1.3.4-py3-none-any.whl:

Publisher: pythonpublish.yml on Zephyrkul/sans

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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