Actions, Functions

Actions and Functions are set up automatically when using reflection. Unbound callables are collected in actions and functions. Bound callables are assigned to the Entity classes they are bound to.

>>> from odata import ODataService
>>> Service = ODataService(url, reflect_entities=True)
>>> Product = Service.entities['Product']

>>> prod = Service.query(Product).get(1234)
>>> prod.GetAvailabilityDate()
datetime.datetime(2018, 6, 1, 12, 0, 0)

>>> import datetime
>>> GetExampleDecimal = Service.functions['GetExampleDecimal']
>>> GetExampleDecimal(Date=datetime.datetime.now())
Decimal('34.0')

Unbound action/functions

Similar to Entities, Functions are subclassed from the Service baseclasses Action and Function:

Service = ODataService(url)

class _GetExampleDecimal(Service.Function):
    name = 'GetExampleDecimal'
    parameters = dict(
        Date=DatetimeProperty,
    )
    return_type = DecimalProperty

GetExampleDecimal = _GetExampleDecimal()

Usage:

>>> import datetime
>>> # calls GET http://service/GetExampleDecimal(Date=2017-01-01T12:00:00Z)
>>> GetExampleDecimal(Date=datetime.datetime.now())
Decimal('34.0')

Bound action/function

Bound functions are otherwise the same, but the instanced object should be set under the Entity it belongs to:

class _GetAvailabilityDate(Service.Function):
    name = 'ODataService.GetAvailabilityDate'
    parameters = dict()
    return_type = DatetimeProperty

class _RemoveAllReservations(Service.Action):
    name = 'ODataService.RemoveAllReservations'
    parameters = dict()
    return_type = BooleanProperty

class _ReserveAmount(Service.Action):
    name = 'ODataService.ReserveAmount'
    parameters = dict(
        Amount=DecimalProperty,
    )
    return_type = BooleanProperty

class Product(Service.Entity):
    Id = IntegerProperty('Id', primary_key=True)
    Name = StringProperty('Name')

    GetAvailabilityDate = GetAvailabilityDate()
    RemoveAllReservations = _RemoveAllReservations()
    ReserveAmount = _ReserveAmount()

Usage:

>>> # collection bound Action. calls POST http://service/Product/ODataService.RemoveAllReservations
>>> Product.RemoveAllReservations()
True

>>> # if the Action is instance bound, call the Action from the Product instance instead
>>> from decimal import Decimal
>>> prod = Service.query(Product).get(1234)
>>> # calls POST http://service/Product(1234)/ODataService.ReserveAmount
>>> prod.ReserveAmount(Amount=Decimal('5.0'))
True

>>> # calls GET http://service/Product(1234)/ODataService.GetAvailabilityDate()
>>> prod.GetAvailabilityDate()
datetime.datetime(2018, 6, 1, 12, 0, 0)

API

class odata.action.Action

Baseclass for all Actions. Should not be used directly, use the subclass Action instead.

name

Action’s fully qualified name. Bound Actions are prefixed with their schema’s name (SchemaName.ActionName). Required when subclassing

parameters

Dictionary. Defines all the keyword arguments and their types the Action can accept. Key names must match with ones accepted by the server. Required when subclassing

return_type

Reference to either Entity class or Property class. Defines the return value’s type for this Action. Required when subclassing

return_type_collection

Reference to either Entity class or Property class. Defines the return value’s type for this Action when retuning multiple values. Required when subclassing

bound_to_collection

Action is bound to Entity collection. If True, Action is not available in Entity class instances. If False, Action is not available in Entity class objects. Defaults to false

class odata.action.ActionCallable(actionbase_instance, url, errmsg=None)

A helper class for ActionBase, representing a callable

class odata.action.Function

Baseclass for all Functions. Should not be used directly, use the subclass Function instead.

name

Function’s fully qualified name. Function Actions are prefixed with their schema’s name (SchemaName.FunctionName). Required when subclassing

parameters

Dictionary. Defines all the keyword arguments and their types the Function can accept. Key names must match with ones accepted by the server. Required when subclassing

return_type

Reference to either Entity class or Property class. Defines the return value’s type for this Function. Required when subclassing

return_type_collection

Reference to either Entity class or Property class. Defines the return value’s type for this Function when retuning multiple values. Required when subclassing

bound_to_collection

Function is bound to Entity collection. If True, Function is not available in Entity class instances. If False, Function is not available in Entity class objects. Defaults to false