#    Copyright (C) 2014  Luis Falcon <lfalcon@gnusolidario.org>
#    Copyright (C) 2014  GNU Solidario <health@gnusolidario.org>

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


About the FHIR Python Package
------------------------------

The FHIR goal is to implement the HL7 FHIR[1] Reference in Python

The project was born to provide the backend to the GNU Health[2] FHIR modules, but it should work in other EMRs and clients.

You can get the latest development version at the Mercurial server in Savannah[3]

1.- http://www.hl7.org/fhir
2.- http://health.gnu.org
3.- http://hg.savannah.gnu.org/hgweb/health/

Sample use :

# Search interactions
>>> from fhir import *

>>> query = RestfulFHIR().search('http://fhir.healthintersections.com.au/open','Patient','name=Jimmy')

>>> print query
<Response [200]>

>>> print query.text

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Search results for resource type Patient</title>
  <id>urn:uuid:dee712bf-9214-408a-b2b4-99d3afecfd</id>
  <link href="http://fhir.healthintersections.com.au/open/" rel="fhir-base" />
  <link href="http://fhir.healthintersections.com.au/open/Patient/_search?search-id=088dfce4-cbee-4c8d-af0b-89a3410a05&amp;name=Jimmy&amp;search-sort=_id" rel="self" />
  <updated>2014-03-09T00:42:08Z</updated>
  <totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">1</totalResults>
  <entry xmlns="http://www.w3.org/2005/Atom">
    <title>Patient "233" Version "1"</title>
    <id>http://fhir.healthintersections.com.au/open/Patient/233</id>
    <link href="http://fhir.healthintersections.com.au/open/Patient/233/_history/1" rel="self" />
    <updated>2014-02-13T00:45:57Z</updated>
    <author>
      <name>service</name>
    </author>
    <published>2014-03-09T00:42:08Z</published>
    <content type="text/xml">
      <Patient xmlns="http://hl7.org/fhir">
        <text>
          <status value="generated"/>
          <div xmlns="http://www.w3.org/1999/xhtml">Manning, Jimmy. MRN:&#x0A;            577552</div>
        </text>
        <identifier>
          <label value="SSN"/>
          <system value="https://github.com/projectcypress/cypress/patient"/>
          <value value="577552"/>
        </identifier>
        <name>
          <use value="official"/>
          <family value="Manning"/>
          <given value="Jimmy"/>
        </name>
        <gender>
          <coding>
            <system value="http://hl7.org/fhir/v3/AdministrativeGender"/>
            <code value="M"/>
            <display value="Male"/>
          </coding>
        </gender>
        <birthDate value="1975-06-07"/>
        <managingOrganization>
          <reference value="Organization/1"/>
        </managingOrganization>
        <active value="true"/>
      </Patient>
    </content>
    <summary type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">Manning, Jimmy. MRN:&#x0A;            577552</div>
    </summary>
  </entry>
</feed>
>>> 

######################################################################

# Read interaction #

>>> query = RestfulFHIR().read('http://fhir.healthintersections.com.au/open','Patient',2)
>>> print query.text

<?xml version="1.0" encoding="UTF-8"?>
<Patient xmlns="http://hl7.org/fhir">
  <text>
    <status value="generated"/>
    <div xmlns="http://www.w3.org/1999/xhtml">Everyman, Adam. SSN:&#x0A;            444333333</div>
  </text>
  <identifier>
    <label value="SSN"/>
    <system value="http://hl7.org/fhir/sid/us-ssn"/>
    <value value="444333333"/>
  </identifier>
  <name>
    <use value="official"/>
    <family value="Everyman"/>
    <given value="Adam"/>
  </name>
  <telecom>
    <system value="phone"/>
    <value value="555-555-2004"/>
    <use value="work"/>
  </telecom>
  <gender>
    <coding>
      <system value="http://hl7.org/fhir/v3/AdministrativeGender"/>
      <code value="M"/>
    </coding>
  </gender>
  <address>
    <use value="home"/>
    <line value="2222 Home Street"/>
  </address>
  <managingOrganization>
    <reference value="Organization/hl7"/>
  </managingOrganization>
  <active value="true"/>
</Patient>

If the resource ID does not exist, a 404 Response will be generated

Say we try to access the non-existant ID 210834

query = RestfulFHIR().read('http://fhir.healthintersections.com.au/open','Patient',210834)

>>> print query
<Response [404]>

>>> print query.text
<?xml version="1.0" encoding="UTF-8"?>
<OperationOutcome xmlns="http://hl7.org/fhir">
  <text>
    <status value="generated"/>
    <div xmlns="http://www.w3.org/1999/xhtml">
      <p>Resource Id "Patient/210834" does not exist</p>
    </div>
  </text>
  <issue>
    <severity value="error"/>
    <details value="Resource Id &quot;Patient/210834&quot; does not exist"/>
  </issue>
</OperationOutcome>


