Wednesday, August 7, 2013

PythonWebAppInterfaceSalesforceRESTAPI

This implementation is a simple Python Web application that authenticates the user to Force.com via Oauth 2.0, then creates, reads, updates, and deletes Force.com records via the new API. It interfaces  with the Force.com REST API.

Need to install:
  • Gunicorn Web Server – Django has default HTTP server, but Gunicorn is more production-ready embedded webserver.
  • Django Rest Framework – Provides endpoint for client to interface. Provides Web browseable API, out of the box Authentication policies including Oauth2.
  • Python-Requests library – Get content, add in caching
  • Python-Logging module
  • OpenSSH-server – authentication
  • Virtualenv - Create a new virtual env using virtualenv to make sure package configuration is keep nicely isolated from any other projects.
  • A Force.com account on EE with password never expired option
  • Configure Remote Access in Salesforce – Generates clientId, clientSecret to use in python request.

Salesforce Object Design and Restful API

  • Partner_Locator__c and Partner_Locator_Country__c has a master to detail relationship.
  • Expose Apex classes as REST Web Services via annotations @RestResource
  • Apex Rest supports two formats for representations of resources: JSON and XML


Web Service Authorization with Oauth

  • Oauth allows Partner Locator web app as the client operate on behalf of a user, without revealing that user’s credentials, such as username and password, to the client. The client first sends the user to an authorization server (login.salesforce.com), which authenticates the user, obtains the user’s authorization, and issues an access token which the client can use in interacting with a resource server (instance url returned in response).

Coding and Configuration Specifics:


  • requests.post : Requests is an Apache2 Licensed HTTP library. Requests makes integration with web services seamless. There is no need to manually add query strings to URLs, or to form-encode POST data. Keep-alive and HTTP connection pooling are 100% automatic.
  • @api_view(['GET']) : Use Django Response class @api_view function for views that return Response objects to ensure that the view can perform content negotiation and select the appropriate renderer for the response, before it is returned from the view.
  • @cache_page(60) : caching the output of individual views. Django.views.decorators.cache defines a cache_page decorator that will automatically cache the view’s response.
  • rest_framework.renderers.JSONPRenderer : A list or tuple of parser classes, that determines the default set of renderers that may be used when returning a Response object. Django REST framework provides JSONP support to get around cross-domain browser access issue.
  • from django.conf.urls import patterns, url : URLConf is a simple mapping between URL patterns (simple regular expressions) to Python functions (views).


No comments: