I have existing REST APIs, written using Django Rest Framework and now due to some client requirements I have to expose some of them as SOAP web services.
I want to know how to go about writing a wrapper in python so that I can expose some of my REST APIs as SOAP web services. OR should I make SOAP web services separately and reuse code ?
I know this is an odd situation but any help would be greatly appreciated.
3 Answers
Answers 1
You can say, SOAP
and REST
are basically apples
and oranges
.
You basically need something, where you can consume the REST API's.
As I see, you have some options:
- Use a SOAP service separately running on another port (endpoint). For that I would say, use framework's like Spyne check out sample hello world
- Use the clients preferred way, either SOAP via WSGI or SOAP via HttpRPC
- Invoke the same REST API endpoints which you created via the methods in SOAP. We had used an internal api wrapper in one of application, which is as:
def wrap_internal_api_call(requests_api_method, uri, data, cookies=None, headers=None): return requests_api_method(uri, data=data, files=files, cookies=cookies, headers=headers)
How you can use this?
import requests from django.core.urlresolvers import reverse from django.conf import settings from spyne.service import Service from spyne.decorator import srpc from spyne.model import ByteArray, DateTime, Uuid, String, Integer, Integer8, \ ComplexModel, Array # This method will hit the internal API which is written in DJANGO REST FRAMEWORK def build_internal_uri(uri): return 'http://localhost:{0}{1}'.format(settings.INTERNAL_API_PORT, uri) class RequestHeader(ComplexModel): some_field = String class SomeService(Service): # Headers related doc # https://github.com/arskom/spyne/blob/68b9d5feb71b169f07180aaecfbe843d8ba500bf/doc/source/manual/06_metadata.rst#protocol-headers __in_header__ = RequestHeader @srpc(String, _returns=String) def echo_string(s): headers = ctx.in_header.some_field # Reverse url from the urls.py file local_order_fetch_url = build_internal_uri(reverse('website:order_details')) + '?order_id=' + order_id response = wrap_internal_api_call(requests.get, local_order_fetch_url, { 'data': 'sample_data' }, None, headers) return response['data'] # Some string data app = Application([SomeService], 'tns', in_protocol=HttpRpc(parse_cookie=True), out_protocol=HttpRpc())
Now there are some of examples which you can look into, being the Django configuration for making it available
Answers 2
Yes, you better follow @Nagaraj Tantri he has given you the best answer possible.
1) Use a SOAP service separately running on another port (endpoint). For that I would say, use framework's like Spyne check out sample hello world 2) Use the clients preferred way, either SOAP via WSGI or SOAP via HttpRPC 3) Invoke the same REST API endpoints which you created via the methods in SOAP. We had used an internal api wrapper in one of application.
Answers 3
Lets Discuss both the Approaches and their pros and cons
SOAP
- Reusing Same Code - if you are sure the code changes will not impact the two code flow ,it is good to go.
- Extension of Features - if you are sure that new feature extension will not impact other parts it is again best to go.
- Scalablity - if new API are part of same application and you are sure that it will be scalable with more load ,it is again a good option.
- Extension - if you are sure in future adding more API will not create a mess of code, it is again good to go for.
REST (my favourate and suggested way to go)
Answer for all the above question in case of rest is YES.
Few more additions benifits
Multiple device support ,universally accepted.
Your Call ,
Comments and critisicsm are most welcome
0 comments:
Post a Comment