You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.6KB

  1. #' Create base XML message
  2. #'
  3. #' Create the base XML message
  4. #'
  5. #' Creates the base XML message as an R list
  6. #'
  7. #' @return The base msg list object
  8. #' @export
  9. base_msg <- function()
  10. {
  11. msg <- list()
  12. msg$i2b2_request <- list()
  13. attr(msg$i2b2_request, "xmlns:i2b2") <- "http://www.i2b2.org/xsd/hive/msg/1.1/"
  14. attr(msg$i2b2_request, "xmlns:pm") <- "http://www.i2b2.org/xsd/cell/pm/1.1/"
  15. msg
  16. }
  17. #' Add the header to the message
  18. #'
  19. #' Add the header to the XML message
  20. #'
  21. #' Add the header to the XML base message created by base_msg
  22. #' base_msg can be piped into add_header
  23. #'
  24. #' @param msg The XML message to add the header to
  25. #' @param username The username to connect with
  26. #' @param password The password for the user
  27. #' @param domain The domain to act on
  28. #' @return The XML message list object
  29. #' @export
  30. add_header <- function(msg, username, password, domain = "")
  31. {
  32. if (domain == "")
  33. domain <- get_domain()$domain_id
  34. mh <- list()
  35. mh$i2b2_version_compatible <- list("1.1")
  36. mh$hl7_version_compatible <- list("2.4")
  37. mh$sending_application$application_name <- list("R2b2")
  38. mh$sending_application$application_version <- list("0.0.9000")
  39. mh$sending_facility$facility_name <- list("R")
  40. mh$receiving_application$application_name <- list("R2b2")
  41. mh$receiving_application$application_version <- list("0.0.9000")
  42. mh$receiving_facility$facility_name <- list("R")
  43. mh$datetime_of_message <- list(format(Sys.time(), "%FT%T%z"))
  44. mh$security$domain <- list(domain)
  45. mh$security$username <- list(username)
  46. mh$security$password <- list(password)
  47. mh$message_control_id$message_num <- list()
  48. mh$message_control_id$instance_num <- list("0")
  49. mh$processing_id$processing_id <- list("P")
  50. mh$processing_id$processing_mode <- list("I")
  51. mh$accept_acknowledgement_type <- list("AL")
  52. mh$application_acknowledgement_type <- list("AL")
  53. mh$country_code <- list("US")
  54. mh$project_id <- list("@")
  55. rh <- list()
  56. rh$result_waittime_ms <- list("180000")
  57. msg$i2b2_request$message_header <- mh
  58. msg$i2b2_request$request_header <- rh
  59. msg
  60. }
  61. #' Add the body to the message
  62. #'
  63. #' Add the body to the XML message
  64. #'
  65. #' Add the body to the XML message created by base_msg and passed through add_header
  66. #' base_msg can be piped into add_header and then into add_body to build a message
  67. #'
  68. #' @param msg The XML message to add the body to
  69. #' @param service The service to request in the body message
  70. #' @param attrib A list of XML attributes to add to the service tag
  71. #' @param ... Optionnaly named tags to add inside the body, with their value
  72. #' @return The XML message list object
  73. #' @export
  74. add_body <- function(msg, service, ..., attrib = NULL)
  75. {
  76. # Create param nodes
  77. params <- list(...) %>% purrr::map(list)
  78. mb <- list()
  79. mb[[service]] <- params
  80. # Set attributes
  81. if(!is.null(attrib))
  82. {
  83. names(attrib) %>%
  84. purrr::map2(attrib, function(name, attrib) {attr(mb[[service]], name) <<- attrib})
  85. }
  86. msg$i2b2_request$message_body <- mb
  87. msg
  88. }
  89. #' Send the message
  90. #'
  91. #' Send the XML message to an i2b2 cell
  92. #'
  93. #' Send the XML message built by base_msg %>% add_header %>% add_body
  94. #' to the specified cellurl
  95. #'
  96. #' @param msg The XML message as an R list
  97. #' @param cellurl The URL of the i2b2 cell to communicate with
  98. #' @return The XML return message as an httr::content() object
  99. #' @export
  100. send_msg <- function(msg, cellurl)
  101. {
  102. # Correct the base tag
  103. request <- msg %>%
  104. xml2::as_xml_document() %>%
  105. as.character %>%
  106. stringr::str_replace_all("i2b2_request", "i2b2:request")
  107. httr::POST(cellurl, body = request, httr::content_type("text/xml")) %>%
  108. httr::content()
  109. }