PyOData Documentation, Release 1.2.1
import pyodata
import requests
SERVICE_URL = 'http://services.odata.org/V2/Northwind/Northwind.svc/'
with open('/the/file/path.xml', 'rb') as mtd_file:
local_metadata = mtd_file.read()
northwind = pyodata.Client(SERVICE_URL, requests.Session(), metadata=local_metadata)
3.1.6 Dealing with errors during parsing metadata
In the case where you need to consume a service which has not fully valid metadata document and is not under your
control, you can configure the metadata parser to try to recover from detected problems.
Parser recovery measures include actions such as using a stub entity type if the parser cannot find a referenced entity
type. The stub entity type allows the parser to continue processing the given metadata but causes fatal errors when
accessed from the client.
Class config provides easy to use wrapper for all parser configuration. These are:
• XML namespaces
• Parser policies (how parser act in case of invalid XML tag). We now support three types of policies:
– Policy fatal - the policy raises exception and terminates the parser
– Policy warning - the policy reports the detected problem, executes a fallback code and then con-
tinues normally
– Policy ignore - the policy executes a fallback code without reporting the problem and then con-
tinues normally
Parser policies can be specified individually for each XML tag (See enum ParserError for more details). If no policy
is specified for the tag, the default policy is used.
For parser to use your custom configuration, it needs to be passed as an argument to the client.
import pyodata
from pyodata.v2.model import PolicyFatal, PolicyWarning, PolicyIgnore, ParserError,
˓→Config
import requests
SERVICE_URL = 'http://services.odata.org/V2/Northwind/Northwind.svc/'
namespaces = {
'edmx': 'customEdmxUrl.com',
'edm': 'customEdmUrl.com'
}
custom_config = Config(
xml_namespaces=namespaces,
default_error_policy=PolicyFatal(),
custom_error_policies={
ParserError.ANNOTATION: PolicyWarning(),
ParserError.ASSOCIATION: PolicyIgnore()
})
(continues on next page)
3.1. Initialization 9