Django endpoint to accept xml Feed - django

I am looking to make an endpoint which would expect an Outbound message from Say SalesForce. The end point would consume the data (most likey update DB with the data). I was hoping to have some questions answered/clarified.
1) would django rest framework be the way to go?
2) since i dont have much control, I believe the auth-key/token etc would come in as a query string param. Does rest framework allow authentication this way?
Thanks for you time and help.
Regards
Tanmay

Most definitely. As long as you know the format of the incoming data from SalesForce, maybe JSON?
1) DRF would most definitely be the way to go. You can easily write an endpoint that accepts a POST request and handles the data preprocessing before saving it to DB
2) Yes, you can do so. You can take a look at writing a Custom Authentication class for that endpoint here.

Related

Create Django RESTful service for execute an algorithm

I need to implement a Django RESTful service where I can run an algorithm and get the result.
On the server I need to have a CSV file with the records for the algorithm. A service, let's say /train will allow me to train a Random Forest with the data in the CSV file, and finally another service /predict will receive the parameters and send me the result. The problem is that I have this running as a script on my computer and I don't know how to structure it for a web application.
I have already done RESTful APIs in Django but this problem is different I think.
I won't need models?
What about the serializers?
My idea is to send a GET request to /predict with the parameters needed for the Random Forest and return the algorithm result.
Any suggestions? Or a public repo with a similar problem?
let say you have
train_view for '/train' with POST request.
result_view for /predict with GET request
Do you need models ?
I think you need that since in request /predict you are going to apply logic on the data you have given in request /train, so create model.
Do you need serializers
Since you have model, you can write modelserializer

ember-data save and find with postgrest

ember-data understands json-api natively ,if we have to integrate ember-data and its save() and find() methods with postgrest style REST calls, where do we need to do changes ?
Do we need to modify client side in ember or some server side logic for mapping with ember-data requirements.
So postgrest REST api calls look like these to get films and their title and competition.name from related table ->
http://localhost:3001/film?select=title,competition{name}
http://localhost:3001/users?select=email::text&id=eq.2&sign_in_count=eq.16
There are two questions here:
Is it better to transform to/from JSON API at the server or the client?
If transforming in the client, where do transforms to/from JSON API occur?
Is it better to transform to/from JSON API at the server or the client?
The first question is really a matter of preference. I personally prefer having the server emit and accept JSON API format because it allows you to ship fewer lines of JavaScript to the client and there's a tendency for multiple clients to communicate with the same server, so standardizing that makes for faster client application development.
For example, you might have two Ember clients (one general user-facing, one admin-facing), an iOS client, and perhaps another server all requesting to your PostgREST server.
That said, you can also think of the format that PostgREST uses as its own spec and have all the clients adhere to that.
If transforming in the client, where do transforms to/from JSON API occur?
Which brings us to question 2: How do you make Ember Data communicate with a server that does not use the JSON API standard?
This occurs in two places: The Adapter and the Serializer.
The Adapter translates requests for data into the appropriate URL where the data can be found (or added) and initiates requests.
So, "give me the photo with the ID of 22" (store.find('photo', 2)), will ask the adapter (assuming Photo #2 isn't already loaded), "hey, someone wants Photo #2, go fetch it please".
The Adapter will make the request and hand the response over to its Serializer.
The Serializer is responsible for translating the data that comes back into a format that Ember Data understands: JSON API.
Both Adapter and Serializer have methods you can implement to customize their behaviors.
In your case with PostgREST, the best places to start would be implementing your own findRecord on the Adapter and implementing your own normalizeResponse on the Serializer.
The docs for both explain the actions you need to take and what type of value you should return from each method.
These are two of the most basic interfaces. They don't provide a lot of functionality out of the box, but will help you become familiar with how these two objects interact.
Once you're comfortable with this basic interaction, check out the sample RestAdapter and RestSerializer for ideas on how to rely on some of the conventions Adapters and Serializers have to offer to clean up any custom code you've written.

How to Return cached JSON data through HTTPResponse in Django?

Need some advice, sorry if this violates the policy of asking questions.
I am working on a project in Django-rest and have below requirement:
'A' is a Django-rest API which is getting consumed by users.
'B' is separate from 'A' and using some open source APIs it gets data and do some processing on it and save it into Redis cache. The cache gets updated after every 8 hours.
Now, I want a specific API call of 'A', like v1/trending-api, to get that cached data from 'B' and serve to end users.
Problem:
How do I access cached data(in JSON) of other API setup and serve it as an HTTPResponse?
Any reference would be really appreciated.
Sounds like your API method 'A' should return data currently available in the Redis cache. 'B' sounds like it would be a separate cronjob to which would fetch data to populate Redis and not be exposed through the django-based REST API. Both have read / write access to the Redis instance correct?
Regarding other APIs, you can always proxy (i.e "user <-> django <-> other-api" all over http)

How to log django requests and responses?

How can I log the requests and responses that the Django server handles?
I have a Django server application has tables and relations. Many tables are exposed through several API endpoints. There are also several custom functions that take GET/POST requests with params and run a function in python and return a result.
As the server admin, I would like to log all the incoming requests and the outgoing responses for each of these requests. It is essential to me that I capture the GET/POST params made with each requests and the data that is sent out with each response. What would be the best way for me to implement this?
I would suggest you DON'T do this through Django, all you're going to do is add to the request work load. You're going to make your application slower, all so you can have it in "one place", remember, "the right tool for the job".
I highly suggest you log to a standard logger file/syslog, and then use something like GrayLog/Loggly/Splunk/Logstash/etc. -- you're going to thank me in the long run, you're not going to grind your Django server and/or DB to the ground.
If you think I'm kidding, think of this: you're more likely going to put this in a middleware right? BEFORE auth, right? If I wanted to make your life a living hell all I have to do is while true; do curl https://yourdomain.com/doesnt-need-to-exist &; done i'm going to fill your DB up in a couple of days, not to mention degrade your service as a whole. Do everyone a favor and don't use the db, specially a prod db to store logs
django-wiretap is a package that stores the requests and responses in the database via models.

RESTful interfaces in Django

I'm willing to build a restful service using Django, I'm coming form RoR background and facing some problems that could be defined using the following questions:
What package do you recommend to use to have RESTful interfaces?
Is there a way to make nested resources like a post HTTP request to /posts/post_id/comments that adds a new comment ?
Is there a way to add some extra actions out of the CRUD set, like having extra method called notify on Post resource that works on post HTTP request.
Cheers,
1) Check out django piston.
2) Yes, you set it up in your urls list.
3) Yes, this is straightforward to do in your view.
Django Piston:
http://bitbucket.org/jespern/django-piston/wiki/Home
I would say that you can do a lot just by implementing your own views that present theirselfs in a RESTful way.
But, there is a project called piston that seems to be exactly what you're looking for: "A mini-framework for Django for creating RESTful APIs".