R/search entity function.R

Defines functions search_entity

Documented in search_entity

#' Function to use the Search API endpoint of Crunchbase API
#' 
#' Searches for an entity. You need to specify which entity e.g. organization, person, etc. it is and provide a json body. See helper functions make_order and make_query for this.
#'
#'@param path What are you searching for? Default: organizations. Other options include:  organizations
#'@param body The JSON body that specifies the query, order, limit etc.
#'@return JSON response
#'
#' @author Layla Rohkohl, \email{byehity@gmail.com}
#'
#' @examples
#' ipos_data <- search(path = "ipos", body = predefinedBody)
#'
#' @import httr
#' @import jsonlite
#' @export
#'
search_entity <- function(path = "organizations", body) {
  
  # Check API Key
  API_KEY <- Sys.getenv("API_KEY")
  if (API_KEY == "") {
    stop(
      "Please set your Crunchbase API Key with the setAPIKey(). Please note that the basic access is not sufficient."
    )
  }
  
  # Construct url
  url <- paste0("https://api.crunchbase.com/api/v4/searches/", path, "?user_key=", API_KEY)
  
  # Make POST request
  response <- httr::POST(url, 
                   body = jsonlite::toJSON(body), #This is your request json
                   encode = "json")
  
  if (response$status_code == 200) {
    # Parse it into readable content
    data <- fromJSON(rawToChar(response$content)) 
    
    # Return data
    data  
  } else {
    print(response$status_code)
  }
  
  
}
Lyrohk/cruncher documentation built on Dec. 17, 2021, 1:17 a.m.