Skip to main content

A simple library for read/write access to OData services.

Project description

python-odata

A simple library for read/write access to OData services.

  • Supports OData version 4.0
  • Requires JSON format support from the service
  • Should work on both Python 2.x and 3.x

Documentation

Available on readthedocs.org

Dependencies

  • requests >= 2.0
  • python-dateutil
  • rich >= 13.3.1

Demo

Northwind ODATA service

Reading data from the Northwind service.

import requests
from datetime import datetime

# you can only import this on the second run, the first run will create the package
# import generated 
from odata import ODataService
session = requests.Session()

url = 'http://services.odata.org/V4/Northwind/Northwind.svc/'
service = ODataService(
    url="http://services.odata.org/V4/Northwind/Northwind.svc/",
    session=session,
    base=generated.northwind.ReflectionBase,
    reflect_entities=True,
    reflect_output_package="generated.northwind")
OrderDetails = generated.northwind.Order_Details

q = service.query(generated.northwind.Customers)
q = q.filter(generated.northwind.Customers.ContactTitle.startswith('Sales'))
q = q.filter(generated.northwind.Customers.PostalCode == '68306')
data = q.first()

query = service.query(OrderDetails)
values = query.all()
values = query \
    .filter((OrderDetails.Order.Employee.HomePhone.contains("555")) | (OrderDetails.Order.Employee.City == "London")) \
    .filter(OrderDetails.Order.Employee.FirstName.lacks("Steven")) \
    .filter(OrderDetails.Order.OrderDate >= datetime(year=1990, month=5, day=1, hour=10, minute=10, second=59, tzinfo=pytz.UTC))\
    .expand(OrderDetails.Order, OrderDetails.Order.Employee) \
    .order_by(OrderDetails.Order.ShipCountry.asc()) \
    .limit(10) \
    .all()
for order_details in values:
    print(f"Order {order_details.OrderID}")
    service.values(order_details)
    service.values(order_details.Order)
    service.values(order_details.Order.Employee)
import datetime

Order = Service.entities['Order']
Employee = Service.entities['Employee']

empl = Service.query(Employee).first()

query = Service.query(Order)
query = query.filter(Order.ShipCity == 'Berlin')

for order in query:
    order.ShippedDate = datetime.datetime.utcnow() 
    order.Employee = empl
    Service.save(order)

TripPin ODATA service (v4)

OData V4 example with Enums.

import logging

import requests
import rich

# comment on first run so you get the generated package
import generated.trippin
from odata import ODataService

requests.packages.urllib3.disable_warnings()


def test_trippin(console):
    proxy = {'http': '', 'https': ''}

    session = requests.Session()

    session.trust_env = False
    session.verify = False
    session.proxies.update(proxy)

    service = ODataService(
        url="https://services.odata.org/v4/TripPinServiceRW",
        session=session,
        #base=generated.trippin.ReflectionBase,  # comment on first run
        #reflect_entities=True,
        reflect_output_package="generated.trippin")
    People = generated.trippin.People

    q = service.query(People)

    values = q.all()
    for value in values:
        console.rule(f"People {value.FirstName} {value.LastName}")
        service.values(value)


if __name__ == "__main__":
    logging.basicConfig(level="DEBUG")
    console = rich.console.Console()
    test_trippin(console)

Writing changes. Note that the real Northwind service is read-only and the data modifications do not work against it.

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

python_odata-0.7.0.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

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

python_odata-0.7.0-py3-none-any.whl (37.9 kB view details)

Uploaded Python 3

File details

Details for the file python_odata-0.7.0.tar.gz.

File metadata

  • Download URL: python_odata-0.7.0.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_odata-0.7.0.tar.gz
Algorithm Hash digest
SHA256 de055ff587cf88507ee9c994f789d5af1c5b109e388c5582c01094dd756f8b77
MD5 f1f037dc8bb04c5e60f4f1f2cac3d59d
BLAKE2b-256 70faa759dfe609e3ffc2671d4093ea5834b8f9e0e45901a2cdefbe72f5223ff9

See more details on using hashes here.

File details

Details for the file python_odata-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: python_odata-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 37.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_odata-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8914090f0845d0a6cf0449204f8cbd6e6db5e8767667eb257b79de3f588f1ac5
MD5 f9a27a748fc2c51d97cd0702941f5a0d
BLAKE2b-256 ec3f718e99309dbdd715049d56157ca3e57f823c13b89f7da3bc83dcd7986743

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