summaryrefslogtreecommitdiffstats
path: root/tags/native-sca-1.0.incubating-M3-RC4/samples/RestCustomer/sample.customer.restclient/CustomerRestClient.py
blob: 340da8700d992fe714e70d720983c706943008d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#

# 
# This Python code is a simple sample that provides a Python 
# client for the REST Customer sample
 

from xml.etree import ElementTree as et
import sca

# Locate the customer resource service
customerResource = sca.locateservice("CustomerResource")

# Show how to invoke CRUD operations on the customer resource
# The CRUD operations translate to HTTP POST, GET, PUT and DELETE
# according to the REST pattern

customer = customerResource.retrieve("2345")
print "Rest - Retrieved customer " + et.tostring(customer)

customer = et.fromstring("""<customer xmlns="http://sample.customer"><id>1234</id><firstName>Jane</firstName><lastName>Doe</lastName></customer>""")
url = customerResource.create(customer)
print "Rest - Created customer " + url

customer = customerResource.retrieve("1234")
print "Rest - Retrieved customer " + et.tostring(customer)

customer = customerResource.retrieve(url)
print "Rest - Retrieved by url " + et.tostring(customer)

customer.find("{http://sample.customer}lastName").text="Smith"
customerResource.update("1234", customer)
print "Rest - Updated customer 1234"

customer = customerResource.retrieve("1234")
print "Rest - Retrieved customer " + et.tostring(customer)

customerResource.delete("1234")
print "Rest - Deleted customer 1234"

# Also show how to use REST binding to invoke remote commands
# using HTTP GET and XML over HTTP POST, the REST binding
# uses that command pattern when you don't declare a REST interface
# on your SCA reference

# Locate the customer command service
customerCommand = sca.locateservice("CustomerCommand")

# Invoke operations on the customer command service
customer = customerCommand.retrieve("2345")
print "Command  - Retrieved customer " + et.tostring(customer)

customer = et.fromstring("""<customer xmlns="http://sample.customer"><id>1234</id><firstName>Jane</firstName><lastName>Doe</lastName></customer>""")
url = customerCommand.create(customer)
print "Command  - Created customer " + url

customer = customerCommand.retrieve("1234")
print "Command  - Retrieved customer " + et.tostring(customer)

customer.find("{http://sample.customer}lastName").text="Smith"
customerCommand.update("1234", customer)
print "Command  - Updated customer 1234"

customer = customerCommand.retrieve("1234")
print "Command  - Retrieved customer " + et.tostring(customer)

customerCommand.delete("1234")
print "Command  - Deleted customer 1234"