how to design rest api which handle offline data - python

I have multiple api which we have provided to android developers.
Like :
1) Creating Business card API
2) Creating Contacts API
So these api working fine when app is online. So our requirement is to handle to create business card and contacts when app is offline.
We are following steps but not sure:-
1) Android developer store the business card when app offline and send this data to server using separate offline business card api when app comes online.
2) Same we do for creating contacts offline using offline contact api.
My problem is I want do in one api call to send all data to server and do operation.
Is this approach will right?? Also please suggest what is the best approach to handle offline data. Also how to handle syncing data when app would come online??
Please let me know if I could provide more information.

I'm confused as to how you're approaching this. My understanding is that when the app is offline you want to "queue up" any API requests that are sent.
Your process seems fine however without knowing the terms around the app being "offline" it's hard to understand if this best.
Assuming you're meaning the server(s) holding the application are offline you're correct you want a process in the android app that will store the request until the application becomes online. However, this can be dangerous for end users. They should be receiving a message on the application being offline and to "try again later" as it were. The fear being they submit a request for x new contacts to be queued and then re-submit not realizing the application was offline.
I would suggest you have the android app built to either notify the user of the app being down or provide some very visible notification that requests are queued locally on their phone until the application becomes available and for them to view/modify/delete said locally cached requests until the application becomes available. When the API becomes available a notification can be set for users to release their queue on their device.

Related

Google Assistant API, controlling a light switch connected to Google Home

I have machine A that just cranks out .png files. It gets synced to machine B and I view it on machine B.
Sometimes machine A crashes for whatever reason and stops doing the scheduled jobs, which means then files on machine B will be old.
I want machine B to run a script to see if the file is older than 1 day, and if it is, then reset the power switch on machine A, so that it can be cold booted. The switch is connected to Google Home but understand I have to use the Assistant API.
I have installed the google-assistant-sdk[samples] package. Can someone show me some code on how to query and return all devices then flip the switch on and off on that device?
Unfortunately, many smart home companies are building products for consumers, not developers. Google's SDK is letting developers stream consumer voice requests to their servers and turning that into actions. Gosund, similarly, is only interested in API access for Amazon and Google. They're API is probably not documented for public use.
To answer your specific question, if you want to use the Google Assistant SDK, you would name your switch something like "Server A Switch" and record a short clip of you saying "Turn off Server A Switch" and "Turn on Server A Switch" and send those two google. The way google matches the requests with your particular account is through OAuth2 tokens, which google will give you in exchange for valid sign in credentials.
If Gosund works with Google Assistant, it has a standard OAuth2 server endpoint as well as a Google Assistant compliant API endpoint. I only recommend this if you want to have some fun reverse engineering it.
In your Google Assistant app, if you try adding the Gosund integration, the first screen popup is the url endpoint where you can exchange valid Gosund account credentials for a one-time code which you can then exchange for OAuth2 access and refresh tokens. With the access token you can theoretically control your switch. The commands you'll want to send are standardized by Google. However, you'll have to figure out where to send them. The best bet here is probably to email their developers.
Are you familiar with OAuth2? If not, I don't recommend doing any of the above.
Your other option is to prevent Server A from hardware crashes. This is what I recommend as the least amount of work. You should start with a server that never crashes, keep it that way and add stuff on top of it. If you only have two servers, they should be able to maintain many months of uptime. Run your scheduled jobs using cron or systemctl and have a watchdog that restarts the job when it detects an error. If your job is crashing the server maybe put it in a VM like docker or something, which gives you neat auto-restart capabilities off the bat.
Another hacky thing you can do is schedule your gosund plug to turn off and on once a day through their consumer UI or app, or at whatever frequency you feel like is most optimal.
The google-assistant-sdk is intended for processing audio requests.
From the doc:
Your project captures an utterance (a spoken audio request, such as What's on my calendar?), sends it to the Google Assistant, and receives a spoken audio response in addition to the raw text of the utterance.
While you could use that with some recorded phrases it makes more sense to connect to the switch directly or use a service like IFTTT. What kind of switch is it?

The architecture of a real-time web chat app

I would to create a real-time web chat application using web.py in python. The problem is that I don't know how to 'architect' or design the such an app.
The way I'm thinking to implement this app is the following:
a user logs into the app.
the app connects to a controller that has a push service to push new messages and a queue service to store the new messages.
when the user sends a message, the app sends the message with an ajax call to the controller and the controller stores the message in a queue.
then the controller sends the messages in the queue to the destination user by its push service.
However I see this is a very poor design since I see a lot of ajax requests being sent here. I really don't know if there are better designs or architectures for such a service. So can you please point me toward the correct design for a real-time chat app?
Alex,
This is an understandable question, I recently thought about it when I was building my own messaging application. This is the way I broke down the app's functionality:
User registration
User authentication
Adding a new friend by username
Approving a friend
Messaging with a friend in list (Of course)
Shows online and offline users
Runs a background service in order to get messages even when the application is closed.
Uses notification area when a new message is received.
Quiting the application(kills the background service)
A few things I realized after building this application was:
The back-end architecture was a simple mixture of a simple CRUD application with pub/sub functionality. You can read up more on pub/sub systems here. Here's a simple chat application built using Ruby on Rails. You can look at it for reference, it's very well architected.
You should think about the last steps listed in the above functionality as much in the beginning of this app as you do in the end. If you architect it well in the beginning, the final steps will just fall into place! :-)
If you want to learn about concurrency and do something really cool, I suggest trying to implement some of the frameworks discussed here.
Please let me know if you have any questions!

Socket Programming for chat on Google App Server

Disclaimer: I am a novice programmer
I am currently following a tutorial: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server
To build a chat application on the iPhone using socket servers. For other purposes, I am using Google App Engine to maintain the backend of my app and hold onto other pieces of data. It only makes sense to have all my backend code located in one area so I was wondering whether Google App Engine will support my socket Programming as there seems to be quite a few restrictions as such: https://developers.google.com/appengine/docs/python/sockets/#limitations-and-restrictions
In fact it almost looks as if there are too many restrictions, however google on the page said that there are "Libraries that import socket, such as poplib or nntplib, and that don't violate the limitations and restrictions listed below, should work without modification." meaning that there are things that I can do to modify my work to allow it to work on the Google App Engine.
My Question: Is it possible to use my learning of socket programming to maintain a backend for my chat on the google app engine? If there is, how do I modify my file if I need to. If there isn't, what app server should I look into so that I can at least hold my chat backend on another server if not at google app engine. If you think that I should take another method altogether to implement chat in my iPhone app, I would love to hear that as well. Thank you for your input.
I think you shall not open the socket yourself, you should use APNS on iPhone and Google Cloud Messaging on Android, so it's not your app that will open (send keepalives, reopen when closed, reopen when connectivity change, etc...) the TCP socket. Also you'll be able to receive data (be spawned) when even if your app is closed.
Received messages (from APNS/GCM) can contains the actual data, or simply be a "Hey, you may go check for messages on the server". To send message you may simply use an HTTP request.

get amount of used resources in app engine

I am thinking about implementing resource throttling in my application in google app engine.
My idea is checking whether I am running out of resources (for example, bandwidth) and disabling part of the website, using the final part of the available daily traffic to inform the user that the site is running in a "resources saving" mode.
I read the GAE documentation, but I just found that if I run out of traffic, it directly returns HTTP 403.
Is there a way to make my python application aware of the used resources and to try not to be so rude with my users?
Unfortunately this is not possible, there is no API that you can use for this.
Looking at the App Engine roadmap there is no such feature coming along any time soon.
The only thing i can recommend is you sign up for billing and recieve the 50$ free quota, it's here till 31 october. You can enable billing and disable it and keep the free 50$!
Hope this helped.

RESTful API across multiple users

I am somewhat new to RESTful APIs.
I'm trying to implement a python system that will control various tasks across multiple computers, with one computer acting as the controller.
I would like all these tasks to be divided amongst multiple users (ex. task foo runs as user foo, and task bar runs as user bar) while handling all requests with a central system. The central system should also act as a simple web server and be able to server basic pages for status purposes.
It it possible to have each user register a "page" with a central server for the API and have the server pass all requests to the programs (probably written in Python)?
Sure, you just need the clients to POST their notifications URL to the server, so that the server can then POST them back with the requests. These are called Webhooks by some people.
Also see RESTful Webhooks.
Yes. Keep in mind that being RESTful is merely a way to organize your web application's URL's in a standard way. You can build your web application to do whatever you want.

Categories