Closed serundeputy closed 6 years ago
@a-pasquale Here is the endpoint URL for the POST request: https://lightupthemap.lndo.site/entity/node?_format=json
https://lightupthemap.lndo.site
this is my local app URL change that to your local or prod etc.Here is the correct payload format:
{
"langcode": [
{
"value": "en"
}
],
"type": [
{
"target_id": "light",
"target_type": "node_type"
}
],
"title": [
{
"value": "I CARE DEEPLY ABOUT THE CLIMATE by admin on 2017-09-04"
}
],
"default_langcode": [
{
"value": true
}
],
"body": [
{
"value": "<p>geoff is the best<\/p>\r\n",
"format": "basic_html",
"summary": ""
}
],
"field_action": [
{
"target_id": 73,
"target_type": "node"
}
]
}
Here is the CURL request that hits the endpoint:
curl -k --include \
--request POST \
--header 'Content-type: application/json' \
--header 'X-CSRF-Token: bgh-LKXMtUBVEVpuh6v4mXzS_QTlGK6890ZNoXMX0BM' \
https://lightupthemap.lndo.site/entity/node?_format=json \
--data-binary '{
"langcode": [
{
"value": "en"
}
],
"type": [
{
"target_id": "light",
"target_type": "node_type"
}
],
"title": [
{
"value": "I CARE DEEPLY ABOUT THE CLIMATE by admin on 2017-09-04"
}
],
"default_langcode": [
{
"value": true
}
],
"body": [
{
"value": "<p>geoff is the best<\/p>\r\n",
"format": "basic_html",
"summary": ""
}
],
"field_action": [
{
"target_id": 73,
"target_type": "node"
}
]
}'
This part of the request:
--header 'X-CSRF-Token: bgh-LKXMtUBVEVpuh6v4mXzS_QTlGK6890ZNoXMX0BM'
is generated on the fly. So your javascript will need to request one of those before sending the POST request. Here is the endpoint to hit to get that token: https://lightupthemap.lndo.site/rest/session/token
swap out the lndo
URL with the URL you need.
TODO:
restui
module to codelight
s; commit to coderestui
config to code@rick02840 we should also be storing the location (lat, long) for each light? What data attribute would that be?
@a-pasquale Yes, but first, attached is info I think you need for the action content type. This is also in branch github-issue-3. I will get you info on light content type separately. for andy info on action.zip
Attached is the template file for the display of a light, with fields added for location, rep and senator. But you need more than this I think. This also in branch github-issue-3 now. Gotta head to work, but will get back to it soon. Maybe not tonight as I have meeting in Boston, but soon as I can. node--light.html.twig.zip
We can merge @rick02840 's branch github-issue-3
into the branch for this isssue 11-rest-create-lights
; then we can make a GET request for a light
and that will give us the new json
structure required.
GET:
curl -k https://lightupthemap.lndo.site/node/308?_format=json
we'll need to swap out that nid = 308
for one that has the new fields populated on it.
@serundeputy Lando instructions worked great - nice tool! Are the changes you made to the database configuration trivial or worth downloading a new version of the db?
@a-pasquale if you are on the branch 11-rest-create-lights
and issue the command:
lando drush cim
you'll get that new config into your DB.
After you merge github-issue-3
into 11-rest-create-lights
you will need to run lando drush cim
again to pick up more config changes, including the addition of the location, rep and senator fields to the light content type.
Since this work is pretty major rework, I created a copy of the master branch called rework
and a Pantheon environment for same: https://rework-light-up-the-map.pantheonsite.io (htaccess: ligthtup/map). You can merge whatever you like into rework
and either myself or Geoff can deploy to Pantheon. Andy if you want to be able to deploy also, get a free account and Pantheon and let me know the email address you used.
Just checking in to say I ended up having meetings every night this week so have not had a chance to do more. Will get back on it this weekend.
I merged the branches and got the config changes but haven't had a chance to look into the additional fields needed for the POST request to the endpoint. I should have some time on Sunday.
I merged my branch github-issue-3
into this branch and pushed to Github. Notes about what is in the github-issue-3
branch are below.
_That branch has config for adding location, representative and senator fields to the light content type. Also has config for changing the view that provides json to the map to use the light location rather than the user location. That view is: admin/structure/views/view/map_lightsjson and output can be seen here: https://www.lightupthemap.com/json-lights
Note that with this branch, and the live database, there will be no lights showing on the map because none of the lights created to date have a location. Remember, the live site uses the user location for the light location, not a light location.
So... things we need to deal with include:
a) update all the lights that exist to have a location equal to the each light's user location
b) when this is all done, will the light location field have logic to "get the location from the logged-in user, if there is one"?
c) how can we associate a light with a user later if the light is created when user is not logged-in or if the user does not exist yet? One thought: email address? Email is unique and is on the user entity and could be put onto the light entity.
d) how to associate a light with state rep and senator? Like location, that also comes from the user on the current live site. Maybe logic: "get the rep and senator from the logged-in user, if there is one"?
This morning, commit to branch 11-rest-create-lights
: _"added field_representative_text and field_senatortext to the user and to the light content type - they are formatted text fields length = 1024 - so they should be able to accept html if needed - can make longer if needed"
Import config here: /admin/config/development/configuration
or with drush cim
.
This is working
We can use the
restui
module to create the endpoint to accept POST requests.This method was successful for a GET request:
curl -k https://lightupthemap.lndo.site/node/308?_format=json
but POST requests are failing with either
500
or400
:but I think that might reduce to getting the correct
json
payload drupal expects to create content of typelight
.