hackforwesternmass / lightupthemap

Light Up The Map was started by a group of Western Mass moms who were fed up with the slow pace of climate legislation. Our children's futures and health will not wait. We knew we needed to activate more people and more Representatives, and fast.
https://www.lightupthemap.com/
0 stars 1 forks source link

Create an API POST endpoint to receive create `light` node requests from js. #11

Closed serundeputy closed 6 years ago

serundeputy commented 7 years ago

We can use the restui module to create the endpoint to accept POST requests.

This method was successful for a GET request:

but POST requests are failing with either 500 or 400:

geoff@yep lightupthemap (rest-create-lights) $ 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 '{"_links":{"type":{"href":"https://lightupthemap.lndo.site/rest/type/node/light"}},"field_action":[{"value":"73"}], ,"type":[{"target_id":"light"}]}'
HTTP/1.1 400 Bad Request
Age: 0
Cache-Control: must-revalidate, no-cache, private
Content-Language: en
Content-Length: 26
Content-Type: application/json
Date: Mon, 04 Sep 2017 23:04:19 GMT
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Server: nginx
Set-Cookie: XDEBUG_SESSION=www-data; expires=Tue, 05-Sep-2017 00:04:18 GMT; Max-Age=3600; path=/
Set-Cookie: XDEBUG_SESSION=www-data; expires=Tue, 05-Sep-2017 00:04:18 GMT; Max-Age=3600; path=/
Via: 1.1 varnish-v4
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Generator: Drupal 8 (https://www.drupal.org)
X-Host: lightupthemap.lndo.site
X-Pantheon-Environment: lando
X-Pantheon-Phpreq: yes
X-Pantheon-Site: TBD
X-Ua-Compatible: IE=edge
X-Url: /entity/node?_format=json
X-Varnish: 5079201

{"message":"Syntax error"}geoff@yep lightupthemap (rest-create-lights) $

but I think that might reduce to getting the correct json payload drupal expects to create content of type light.

serundeputy commented 7 years ago

@a-pasquale Here is the endpoint URL for the POST request: https://lightupthemap.lndo.site/entity/node?_format=json

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:

a-pasquale commented 7 years ago

@rick02840 we should also be storing the location (lat, long) for each light? What data attribute would that be?

rick02840 commented 7 years ago

@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

rick02840 commented 7 years ago

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

serundeputy commented 7 years ago

We can merge @rick02840 's branch github-issue-3into 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.

a-pasquale commented 7 years ago

@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?

serundeputy commented 7 years ago

@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.

rick02840 commented 7 years ago

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.

rick02840 commented 7 years ago

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.

rick02840 commented 7 years ago

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.

a-pasquale commented 7 years ago

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.

rick02840 commented 7 years ago

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"?

rick02840 commented 7 years ago

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.

a-pasquale commented 6 years ago

This is working