0

I have an API call that comes in one of three responses:

The first type of response:
   [[1]]
    Response [https://api.secure.com/v4/people/111548751]
      Date: 2022-09-07 01:28
      Status: 200
      Content-Type: application/json; charset=utf-8
      Size: 2.16 kB
    {
      "Id": 111548751,
      "firstName": "Jeff",
      "lastName": "Smith",
      "middleName": null,
      "suffix": null,
      "title": null,
      "sourceFileTitle": null,
      "sourceFileFirstName": null,
      "sourceFileMiddleName": null,
    ...

The second type of response:

[[1]]
Response [https://api.secure.com/v4/people/findOrCreate/]
  Date: 2022-09-07 01:29
  Status: 201
  Content-Type: application/json; charset=utf-8
  Size: 58 B
{
  "Id": 111634535,
  "status": "UnmatchedStored"

And finally, the third response:

[[1]]
Response [https://api.secure.com/v4/people/findOrCreate/]
  Date: 2022-09-07 05:38
  Status: 500
  Content-Type: application/json; charset=utf-8
  Size: 164 B
{
  "errors": [
    {
      "code": "INTERNAL_SERVER_ERROR",
      "text": "An unknown error occurred",
      "referenceCode": "A-7731DC-490283"
    }
  ]

Right now, these responses are stored in a list response called get_data. I have some code sets up that extracts three pieces of data from all of these responses that looks like this:

data_log <- get_data %>% 
  map_df(extract, c("url", "date", "status_code"))

This will be a table that produces those three values from every response. But what I'm also trying to do is add the field Id from all of the responses that contain the value (like the first two ones). If it doesn't contain a value for Id (like the last response), the output would just say "No ID"

wizkids121
  • 634
  • 1
  • 9
  • 22
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 07 '22 at 14:56
  • Maybe `get_data %>% map_chr(~pluck(content(.), "Id", .default="No ID"))` will get your the ID column which you can bind to your data.frame. – MrFlick Sep 07 '22 at 15:00
  • @MrFlick What other information would you need me to provide? It's an API call, so I can't provide the exact specifics without giving away security credentials. – wizkids121 Sep 07 '22 at 15:08
  • 1
    If there is no publicly available API that can be used for testing, then we can't really verify that any possible solution will for sure work. You could create a dummy plumber API in R that would replicate the behavior that you wish to test. At least that way testing would be possible. – MrFlick Sep 07 '22 at 15:27

0 Answers0