Rails POST 请求练习融合 API

Rails POST request to Practice Fusion API

我正在使用 Practice Fusion API 接收挂单。我正在尝试编写一个 POST 请求来确认在 GET 请求期间收到的已下载订单。一旦 Practice Fusion 收到 POST 请求,挂单将被删除。

我正在使用的宝石:

休息客户端

simple_hl7 --用于hl7解析

这是我的工作 GET 请求:

def download_pf_orders
  download_count = 0
  uri = "#{PF_TEST_PENDING_API_URL}"
  rest_resource = RestClient::Resource.new(uri, PF_TEST_USERNAME, PF_TEST_PASSWORD)

  begin
    response = rest_resource.get(accept: 'application/json')
    json = JSON.parse(response)

    json.each do |data|

      sequence = data['SequenceNumber']
      puts "### Last Sequence Number: #{sequence}"

      PfOrder.create(
        sequence_number: data['SequenceNumber'],
        message_guid: data['MessageGuid'],
        hl7_document: data['Hl7Document']
        )
      download_count += 1
    end
  rescue => e
    puts "### Status Code: #{e.response.code} ###"
  end
  puts "### Downloaded Orders: #{download_count} ###"
end

这是 Practice Fusion 概述的 POST 请求标准:

Sample HL7 ACK Message

Given a retrieved order message with the following MSH segment:

MSH|^~\&|PracticeFusion|ClientID|||20130930225002+0000||OML^O21^OML_O21|a783a 5d7-c9b2-42e9-abb1-a1b473079512|P|2.5.1|||AL|NE|||||ELINCS_MT-OML-1_1.0

The expected HL7 ACK message would contain the following MSH and MSA segments:

MSH|^~\&|PracticeFusion|VendorCode||ClientID|20130930225002+0000||ACK^ELINCS^ ACK_ELINCS|MessageControlID|P|2.5.1||||||||| ELINCS_MT-ACK-1_1.0

MSA|CA|a783a5d7-c9b2-42e9-abb1-a1b473079512

***As indicated by the color coordination in the sample order and acknowledgement message above, your ACK logic should populate MSH-6 Client ID of the acknowledgement using the MSH-4 Client ID value of the associated order, and the MSA-2 value using the control ID in MSH-10 of the order message.

***The ‘Vendor Code’ value populated in MSH-4 of your acknowledgement message is a unique value assigned by Practice Fusion. Please contact your implementation resource for this value if it has not previously been provided.

下面是一些提供的 C# 示例代码来完成此操作:

/// <summary>
/// Post an HL7 Order Acknowledgement message back to PracticeFusion to signal
acceptance...
/// </summary>
static void AcknowledgeOrder(string hl7AckMessage)
{
  var request = (HttpWebRequest)
  WebRequest.Create(ConfigurationManager.AppSettings["OrderAcknowledgementUri"]);
  request.PreAuthenticate = true;
  request.Credentials = new
  NetworkCredential(ConfigurationManager.AppSettings["PF.ApiUserName"],
   ConfigurationManager.AppSettings["PF.ApiPassword"]);
  request.Method = WebRequestMethods.Http.Post;
  request.ContentType = "application/x-www-form-urlencoded";
  using (var writer = new StreamWriter(request.GetRequestStream()))
  {
   writer.Write(hl7AckMessage);
 }
}

我想通了。此方法将成功 POST 返回,但仍需要考虑是否已成功发布订单。

def acknowledge_pf_order
  hl7_message = PfOrder.all
  hl7_message.each do |msg|
    clean_msg = msg.hl7_document.gsub(/\r/, '')

    msh_segment = clean_msg.gsub(MSH)
    msh_segment.each do |seg|
      parsed_msh = SimpleHL7::Message.parse(seg)

      parsed_msh.msh[4] = 'yourVendorCode'
      parsed_msh.msh[6] = 'yourClientID'
      parsed_msh.msh[9][1] = 'ACK'
      parsed_msh.msh[9][2] = 'ELINCS'
      parsed_msh.msh[9][3] = 'ACK_ELINCS'
      parsed_msh.msh[21] = 'ELINCS_MT-ACK-1_1.0'

      message_id = parsed_msh.msh[10]
      ack_message = parsed_msh.to_hl7

      msa_segment = "\nMSA|CA|#{message_id.to_s}"

      payload = ack_message << msa_segment

      uri = "#{PF_TEST_ACK_API_URL}"
      rest_resource = RestClient::Resource.new(uri, PF_TEST_USERNAME, PF_TEST_PASSWORD)

      begin
        response = rest_resource.post payload, content_type: 'text/plain'
        puts "### POST Status Code: #{response.code} ###"
      rescue Exception => e
        puts "### POST Status Code: #{e.response.code} ###"
      end
    end
  end
end