Changeset 7

Show
Ignore:
Timestamp:
02/09/07 15:17:52 (2 years ago)
Author:
kevin
Message:

better formatting

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/README.txt

    r1 r7  
    1212------------ 
    1313 
    14 TurboGears gives you a plain HTTP with JSON return values API for your application for free. This isn't always what you want, though. Sometimes, you don't want to expose all of the data to the web that you need to render your templates. Maybe you need to support a protocol that names the function it's calling as part of what it POSTs such as SOAP or XML-RPC. 
     14TurboGears gives you a plain HTTP with JSON return values API for your 
     15application for free. This isn't always what you want, though. Sometimes, 
     16you don't want to expose all of the data to the web that you need to 
     17render your templates. Maybe you need to support a protocol that names the 
     18function it's calling as part of what it POSTs such as SOAP or XML-RPC. 
    1519 
    16 TGWebServices provides a super simple API for creating web services that are available via SOAP, HTTP->XML, and HTTP->JSON. The SOAP API generates WSDL automatically for your Python and even generates enough type information for statically typed languages (Java and C#, for example) to generate good client code on their end. 
     20TGWebServices provides a super simple API for creating web services that 
     21are available via SOAP, HTTP->XML, and HTTP->JSON. The SOAP API generates 
     22WSDL automatically for your Python and even generates enough type 
     23information for statically typed languages (Java and C#, for example) to 
     24generate good client code on their end. 
    1725 
    1826How easy is it? 
     
    2735            return num1 * num2 
    2836 
    29 With this at the root, SOAP clients can find the WSDL file at /soap/api.wsdl and POST SOAP requests to /soap/. HTTP requests to /multiply?num1=5&num2=20 will return an XML document with the result of 100. Add ?tg_format=json (or an HTTP Accept: text/javascript header) and you'll get JSON back. 
     37With this at the root, SOAP clients can find the WSDL file at 
     38/soap/api.wsdl and POST SOAP requests to /soap/. HTTP requests to 
     39/multiply?num1=5&num2=20 will return an XML document with the result of 
     40100. Add ?tg_format=json (or an HTTP Accept: text/javascript header) and 
     41you'll get JSON back. 
    3042 
    31 The great thing about this is that the code above looks like a '''normal Python function''' and doesn't know a thing about web services. 
     43The great thing about this is that the code above looks like a '''normal 
     44Python function''' and doesn't know a thing about web services. 
    3245 
    3346Prerequisites 
     
    4154* Easiest way to expose a web services API 
    4255* Supports SOAP, HTTP+XML, HTTP+JSON 
    43 * Outputs wrapped document/literal SOAP, which is the most widely compatible format 
    44 * Provides enough type information for statically typed languages to generate conveniently usable interfaces 
     56* Outputs wrapped document/literal SOAP, which is the most widely 
     57  compatible format 
     58* Provides enough type information for statically typed languages to 
     59  generate conveniently usable interfaces 
    4560* Can output instances of your own classes 
    4661* Works with TurboGears 1.0 
     
    7388 
    74891. Those are all of the imports that you'll likely need 
    75 2. The top level controller must subclass WebServicesRoot. This is important, because this provides the /soap URL that is required for SOAP access. 
     902. The top level controller must subclass WebServicesRoot. This is 
     91   important, because this provides the /soap URL that is required for 
     92   SOAP access. 
    76933. wsexpose is called with the type that is returned from the method. 
    77 4. wsvalidate is called with the types of the parameters. You can specify the types positionally (*skipping self*) or as keyword arguments to wsvalidate. 
    78 5. Unlike when you work with TurboGears proper, you do not need to return a dictionary. Just return the value directly. 
     944. wsvalidate is called with the types of the parameters. You can specify 
     95   the types positionally (*skipping self*) or as keyword arguments to 
     96   wsvalidate. 
     975. Unlike when you work with TurboGears proper, you do not need to return 
     98   a dictionary. Just return the value directly. 
    7999 
    80100Lists 
    81101----- 
    82102 
    83 When you wish to return an array of items, you can specify this by creating a list that contains one item: the type of the objects in the list:: 
     103When you wish to return an array of items, you can specify this by 
     104creating a list that contains one item: the type of the objects in the 
     105list:: 
    84106 
    85107    @wsexpose([str]) 
     
    87109        return ["A", "B", "C"] 
    88110 
    89 Since many web services are consumed by statically typed languages like Java, lists that are returned as SOAP arrays can only contain *one* type of object. 
     111Since many web services are consumed by statically typed languages like 
     112Java, lists that are returned as SOAP arrays can only contain *one* type 
     113of object. 
    90114 
    91115Custom Objects 
    92116-------------- 
    93117 
    94 You can return instances of classes that you create. Whenever you see the term "complex type", you can think "class". "Complex type" comes from XML Schema terminology that is used in declaring the properties of the returned type. 
     118You can return instances of classes that you create. Whenever you see the 
     119term "complex type", you can think "class". "Complex type" comes from XML 
     120Schema terminology that is used in declaring the properties of the 
     121returned type. 
    95122 
    96 Returning complex types is as easy as returning primitive types. However, you do need to take an extra step in declaring complex types that you wish to return. Here is an example:: 
     123Returning complex types is as easy as returning primitive types. However, 
     124you do need to take an extra step in declaring complex types that you wish 
     125to return. Here is an example:: 
    97126 
    98127    class FancyValue(object): 
     
    117146            return fv 
    118147 
    119 In this example, we've created a class called FancyValue that we want to return from a web service method. TGWebServices will only return properties of instances that:: 
     148In this example, we've created a class called FancyValue that we want to 
     149return from a web service method. TGWebServices will only return 
     150properties of instances that: 
    120151 
    121 * are declared at the class level (in the example above, name is defined as a string and age is defined as an int) 
     152* are declared at the class level (in the example above, name is defined 
     153  as a string and age is defined as an int) 
    122154* do not start with _ 
    123155* are not methods 
    124156 
    125 With these rules, it's easy to store whatever housekeeping data you need on your objects without exposing that data to the web service. 
     157With these rules, it's easy to store whatever housekeeping data you need 
     158on your objects without exposing that data to the web service. 
    126159 
    127 Once you've defined your class, you can specify it as a return value in wsexpose just as you would a builtin Python type. 
     160Once you've defined your class, you can specify it as a return value in 
     161wsexpose just as you would a builtin Python type. 
    128162 
    129 See the next section for information about *typedproperty* which appears in the example above. 
     163See the next section for information about *typedproperty* which appears 
     164in the example above. 
    130165 
    131 At this time, TGWebServices doesn't support instances and lists as input parameters. 
     166At this time, TGWebServices doesn't support instances and lists as input 
     167parameters. 
    132168 
    133169Extra Types You Can Return 
    134170-------------------------- 
    135171 
    136 The tgwebservices.runtime defines two additional types that you can return: unsigned and typedproperty. Python doesn't have an unsigned int type, but you can use the tgwebservices.runtime.unsigned class to tell the consumer of your service that you are returning an unsigned integer value. 
     172The tgwebservices.runtime defines two additional types that you can 
     173return: unsigned and typedproperty. Python doesn't have an unsigned int 
     174type, but you can use the tgwebservices.runtime.unsigned class to tell the 
     175consumer of your service that you are returning an unsigned integer value. 
    137176 
    138 typedproperty wraps the standard Python property function allowing you to specify what return type will be coming from your property's getter method. In the example in the previous section, the "computed" property will be a string. 
     177typedproperty wraps the standard Python property function allowing you to 
     178specify what return type will be coming from your property's getter 
     179method. In the example in the previous section, the "computed" property 
     180will be a string. 
    139181 
    140182Getting Help