Examples

These examples demonstrate a few common usage scenarios.

Replying to a packet from a transaction

#!/usr/bin/env python3

"""
This example demonstrates how to respond to a transaction in progress
For this example, Transaction 12345 has an incoming RequestProjectCreate
packet, which we will respond to with a NotifyProjectCreate.
"""
from amieclient import AMIEClient

# Create the client
psc_client = AMIEClient(site_name='PSC', api_key='some_secret_key')

# Get the transaction you want
transaction = psc_client.get_transaction(trans_rec_id='12345')

# Get the most recent packet (? this may need a more robust method)
project_creation_request = transaction.packets[-1]

# The assumption here is that rpc is a RequestProjectCreate.
# Here, you'd go ahead and do what you need to create the project.

# SomeInternalSystem.create_project()

# Once that's done, send a NotifyProjectCreate packet.
# If nothing needs to be changed from the RequestProjectCreate packet,
# you can use the packet's reply_packet() method. This will create a packet that
# automatically has the proper type and a reference to the preceeding packet.
# The AMIE service will extrapolate the needed information from the
# RequestProjectCreate packet.

project_created = project_creation_request.reply_packet()

psc_client.send_packet(project_created)

# You can also create a client as a context manager, if you want.
# This complete example would look like
with AMIEClient('psc', 'some_secret_key') as client_too:
    transaction = client_too.get_transaction(transaction_id='12345')
    project_creation_request = transaction.packets[-1]
    # Do something...
    project_created = project_creation_request.reply_packet()
    client_too.send_packet(project_created)

Creating a new packet from scratch

#!/usr/bin/env python3

"""
This example demonstrates how to create and send along a new packet."""
from amieclient import AMIEClient
from amieclient.packet import RequestProjectCreate

from datetime import datetime, timedelta

# Create the packet
rpc = RequestProjectCreate()
rpc.AllocationType = 'extremely hihg most important'
rpc.GrantNumber = '3'
rpc.PfosNumber = '3'
rpc.PiFirstName = 'Jessica'
rpc.PiLastName = 'Scienceperson'
rpc.PiOrganization = 'PSC'
rpc.PiOrgCode = '12345'
rpc.EndDate = datetime.now() + timedelta(days=90)
rpc.StartDate = datetime.now()
rpc.ResourceList = ['IDK, somthing pretty fast']
rpc.ServiceUnitsAllocated = '3'

# Send the packet
with AMIEClient('psc', 'some_secret_key') as c:
    c.send_packet(rpc)

Reading configuration information from a file

Here’s the configuration file:

[PSC]
site_name = PSC
api_key = some_secret_key

[PSC_TEST]
site_name = PSC
amie_url = https://amieclient.xsede.org/v0.20/
api_key = some_beta_key

[NCSA_LOCAL_DEV]
site_name = NCSA
amie_url = http://localhost:12345
usage_url = http://localhost:23456
api_key = some_dev_key

And here’s how you read from the configuration file and use it to create clients:

#!/usr/bin/env python3

"""
This example demonstrates one way to store neccessary connection information
for amieclient. Take a look at ./config.ini for an example of the format that
Python's configparser expects. Three different sections are defined there --
PSC, PSC_TEST, and NCSA_LOCAL_DEV -- containing three different configurations.

PSC: Site name is PSC; points to the production URL endpoint
PSC_TEST: Site name is also PSC, but the base URL is for a hypothetical version 0.2
NSCA_LOCAL_TEST: Site name is NSCA. It points to a AMIE REST API running on localhost:12345

We load and parse that configuration file, then use that to create client
objects for each of the provided configurations.
"""

from configparser import ConfigParser

from amieclient import AMIEClient

# For more information on the configparser library, please see the python docs:
# https://docs.python.org/3.5/library/configparser.html
config = ConfigParser()
config.read('config.ini')

# Get each section of the config file and give it a friendly name.
psc_config = config['PSC']
psc_test_config = config['PSC_TEST']
local_dev_config = config['NCSA_LOCAL_DEV']

# Create the various clients. In real life, you'd almost certainly only have
# one client.

# These clients all use the default value for the base URL, which is
# https://amieclient.xsede.org/v0.10/
psc_client = AMIEClient(site_name=psc_config['site_name'], api_key=psc_config['api_key'])

# These clients use (made-up) different base URLs.
psc_test_client = AMIEClient(site_name=psc_test_config['site_name'],
                         amie_url=psc_test_config['amie_url'],
                         api_key=psc_test_config['api_key'])
local_dev_client = AMIEClient(site_name=local_dev_config['site_name'],
                          amie_url=local_dev_config['amie_url'],
                          usage_url=local_dev_config['usage_url'],
                          api_key=local_dev_config['api_key'])

# If you're into being mysterious and obscure, you could also use dictionary
# expansion to pass your configuration variables based on the configuration
# section of your choice. This will save yourself like 10 keystrokes.
psc_test_client_2 = AMIEClient(**psc_test_config)