Good day
I am trying to find out if it is possible to have DreamFactory copy user details to another Mysql database when a user registers?
I have a mysql db running in AWS ( which is setup as a service in DF) and when a user registers, I also want some of the users details to get stored in my AWS db for some reporting functionality.
I thought maybe at first I could just use a Federated table within Mysql within AWS doesn’t support this so I am just looking for an alternative solution.
Any help is appreciated
Gavin
There are examples on github that should give you what you need to do this. Unfortunately they still haven’t merged my example to the main repository…
1 Like
Thanks a lot.
I’ve modified the script to suit my needs (for a small test) although when registering a new user I don’t see anything appear in the destination table. I can manually insert the same record using API DOCS, so much be something I’ve done in the python script
This is what I’m using:
POST /api/v2/user/register triggers script user.register.post.post_process
# This script runs AFTER user is registered
# The purpose of this script is to get the dreamfactory user id of the user that was just registered
# and then use that as a primary key for a custom "user data" table
import bunch
# set up headers
options = {}
options['headers'] = {}
options['headers']['X-DreamFactory-Api-Key'] = platform.session.api_key
options['headers']['X-DreamFactory-Session-Token'] = platform.session.session_token
# get records using internal URL; call api.get on the user session that was just
# created (this script will only work in post process!)
url = 'user/session'
result = platform.api.get(url)
data = result.read()
jsonData = bunchify(json.loads(data))
if 'error' in jsonData:
raise ValueError(jsonData.error.message)
# save the data from api.get on user/session to local variables
User_OID = str(jsonData.id)
UserName = jsonData.first_name
Email = jsonData.email
# create a simple json object that will be inserted into the custom user data table
post_data = '{"resource": [{"User_OID": "' + User_OID + '","UserName": "' + UserName + '","Email": "' + Email + '"}]}'
# create a record using internal URL; call api.post with the post data that was built above, data will
# be posted to custom UserData table
url = 'https://my_server:444/api/v2/aws/_table/dim_user?fields=*'
result = platform.api.post(url, post_data)
data = result.read()
jsonData = bunchify(json.loads(data))
if 'error' in jsonData:
raise ValueError(jsonData.error.message)
elif 'resource' not in jsonData or len(jsonData.resource) != 1:
raise ValueError('Unexpected response from url = ' + url)
I’ve tried using aws/_table/dim_user?fields=* as the url with no luck either.
From what I know DF will use the user rights of the user that has just registered even if you were to supply a different API key(bug???) so you need to make sure that your user has rights to do POST to your AWS service.
This can be very confusing to new people because they test things in API docs and don’t realize that all API calls from API docs are being done from an admin account that basically has full access. When you test it from your application you will use an API key that has restrictions. The security is difficult to understand and I have been using DF for over a year.
1 Like
Thanks,
I’ve checked that the user role has access to post to the AWS service via API and Script but no luck. Is there a way that you are away of to get out log out from these scripts?
You can check the wiki for how to do logging. I have not really used it because it’s a pain to use on a production server.
I noticed that your post_data
is the same as the example. Keep in mind for this to work your AWS table has to have the exact same columns that are declared in the post_data
json you want to insert.
I setup a test table dim_user with the 3 columns in from the example (all varchar(50)) just to test, and once working I can capture the actual data I needed
Thanks for all your help
So a quick follow up. I’ve still not found a solution, but after changing the logs and clearing the cache in PHP, I could see the error that the module “bunch” could not be found, so I commented that line out and now get:
{‘script_result’: {‘error’: “‘dict’ object has no attribute ‘session’”}, ‘resource’: ‘’, ‘request’: {‘parameters’: {‘login’: ‘True’}, ‘payload’: {‘first_name’: ‘ggg’, ‘last_name’: ‘ddd’, ‘display_name’: ‘ddd’, ‘email’: 'ya$
so, a little close, but still not quite there.
You need bunch library to easily handle JSON with python.
See the readme about using python scripts on github.
Thanks again, found this shortly after posting and it fixed the import issue.
Just the {‘script_result’: {‘error’: “‘dict’ object has no attribute ‘session’”} } to go