faset over fra Z3950 til google books
This commit is contained in:
229
python/gdata/health/__init__.py
Normal file
229
python/gdata/health/__init__.py
Normal file
@@ -0,0 +1,229 @@
|
||||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright 2009 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Contains extensions to Atom objects used with Google Health."""
|
||||
|
||||
__author__ = 'api.eric@google.com (Eric Bidelman)'
|
||||
|
||||
import atom
|
||||
import gdata
|
||||
|
||||
|
||||
CCR_NAMESPACE = 'urn:astm-org:CCR'
|
||||
METADATA_NAMESPACE = 'http://schemas.google.com/health/metadata'
|
||||
|
||||
|
||||
class Ccr(atom.AtomBase):
|
||||
"""Represents a Google Health <ContinuityOfCareRecord>."""
|
||||
|
||||
_tag = 'ContinuityOfCareRecord'
|
||||
_namespace = CCR_NAMESPACE
|
||||
_children = atom.AtomBase._children.copy()
|
||||
|
||||
def __init__(self, extension_elements=None,
|
||||
extension_attributes=None, text=None):
|
||||
atom.AtomBase.__init__(self, extension_elements=extension_elements,
|
||||
extension_attributes=extension_attributes, text=text)
|
||||
|
||||
def GetAlerts(self):
|
||||
"""Helper for extracting Alert/Allergy data from the CCR.
|
||||
|
||||
Returns:
|
||||
A list of ExtensionElements (one for each allergy found) or None if
|
||||
no allergies where found in this CCR.
|
||||
"""
|
||||
try:
|
||||
body = self.FindExtensions('Body')[0]
|
||||
return body.FindChildren('Alerts')[0].FindChildren('Alert')
|
||||
except:
|
||||
return None
|
||||
|
||||
def GetAllergies(self):
|
||||
"""Alias for GetAlerts()."""
|
||||
return self.GetAlerts()
|
||||
|
||||
def GetProblems(self):
|
||||
"""Helper for extracting Problem/Condition data from the CCR.
|
||||
|
||||
Returns:
|
||||
A list of ExtensionElements (one for each problem found) or None if
|
||||
no problems where found in this CCR.
|
||||
"""
|
||||
try:
|
||||
body = self.FindExtensions('Body')[0]
|
||||
return body.FindChildren('Problems')[0].FindChildren('Problem')
|
||||
except:
|
||||
return None
|
||||
|
||||
def GetConditions(self):
|
||||
"""Alias for GetProblems()."""
|
||||
return self.GetProblems()
|
||||
|
||||
def GetProcedures(self):
|
||||
"""Helper for extracting Procedure data from the CCR.
|
||||
|
||||
Returns:
|
||||
A list of ExtensionElements (one for each procedure found) or None if
|
||||
no procedures where found in this CCR.
|
||||
"""
|
||||
try:
|
||||
body = self.FindExtensions('Body')[0]
|
||||
return body.FindChildren('Procedures')[0].FindChildren('Procedure')
|
||||
except:
|
||||
return None
|
||||
|
||||
def GetImmunizations(self):
|
||||
"""Helper for extracting Immunization data from the CCR.
|
||||
|
||||
Returns:
|
||||
A list of ExtensionElements (one for each immunization found) or None if
|
||||
no immunizations where found in this CCR.
|
||||
"""
|
||||
try:
|
||||
body = self.FindExtensions('Body')[0]
|
||||
return body.FindChildren('Immunizations')[0].FindChildren('Immunization')
|
||||
except:
|
||||
return None
|
||||
|
||||
def GetMedications(self):
|
||||
"""Helper for extracting Medication data from the CCR.
|
||||
|
||||
Returns:
|
||||
A list of ExtensionElements (one for each medication found) or None if
|
||||
no medications where found in this CCR.
|
||||
"""
|
||||
try:
|
||||
body = self.FindExtensions('Body')[0]
|
||||
return body.FindChildren('Medications')[0].FindChildren('Medication')
|
||||
except:
|
||||
return None
|
||||
|
||||
def GetResults(self):
|
||||
"""Helper for extracting Results/Labresults data from the CCR.
|
||||
|
||||
Returns:
|
||||
A list of ExtensionElements (one for each result found) or None if
|
||||
no results where found in this CCR.
|
||||
"""
|
||||
try:
|
||||
body = self.FindExtensions('Body')[0]
|
||||
return body.FindChildren('Results')[0].FindChildren('Result')
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
class ProfileEntry(gdata.GDataEntry):
|
||||
"""The Google Health version of an Atom Entry."""
|
||||
|
||||
_tag = gdata.GDataEntry._tag
|
||||
_namespace = atom.ATOM_NAMESPACE
|
||||
_children = gdata.GDataEntry._children.copy()
|
||||
_attributes = gdata.GDataEntry._attributes.copy()
|
||||
_children['{%s}ContinuityOfCareRecord' % CCR_NAMESPACE] = ('ccr', Ccr)
|
||||
|
||||
def __init__(self, ccr=None, author=None, category=None, content=None,
|
||||
atom_id=None, link=None, published=None, title=None,
|
||||
updated=None, text=None, extension_elements=None,
|
||||
extension_attributes=None):
|
||||
self.ccr = ccr
|
||||
gdata.GDataEntry.__init__(
|
||||
self, author=author, category=category, content=content,
|
||||
atom_id=atom_id, link=link, published=published, title=title,
|
||||
updated=updated, extension_elements=extension_elements,
|
||||
extension_attributes=extension_attributes, text=text)
|
||||
|
||||
|
||||
class ProfileFeed(gdata.GDataFeed):
|
||||
"""A feed containing a list of Google Health profile entries."""
|
||||
|
||||
_tag = gdata.GDataFeed._tag
|
||||
_namespace = atom.ATOM_NAMESPACE
|
||||
_children = gdata.GDataFeed._children.copy()
|
||||
_attributes = gdata.GDataFeed._attributes.copy()
|
||||
_children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [ProfileEntry])
|
||||
|
||||
|
||||
class ProfileListEntry(gdata.GDataEntry):
|
||||
"""The Atom Entry in the Google Health profile list feed."""
|
||||
|
||||
_tag = gdata.GDataEntry._tag
|
||||
_namespace = atom.ATOM_NAMESPACE
|
||||
_children = gdata.GDataEntry._children.copy()
|
||||
_attributes = gdata.GDataEntry._attributes.copy()
|
||||
|
||||
def GetProfileId(self):
|
||||
return self.content.text
|
||||
|
||||
def GetProfileName(self):
|
||||
return self.title.text
|
||||
|
||||
|
||||
class ProfileListFeed(gdata.GDataFeed):
|
||||
"""A feed containing a list of Google Health profile list entries."""
|
||||
|
||||
_tag = gdata.GDataFeed._tag
|
||||
_namespace = atom.ATOM_NAMESPACE
|
||||
_children = gdata.GDataFeed._children.copy()
|
||||
_attributes = gdata.GDataFeed._attributes.copy()
|
||||
_children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [ProfileListEntry])
|
||||
|
||||
|
||||
def ProfileEntryFromString(xml_string):
|
||||
"""Converts an XML string into a ProfileEntry object.
|
||||
|
||||
Args:
|
||||
xml_string: string The XML describing a Health profile feed entry.
|
||||
|
||||
Returns:
|
||||
A ProfileEntry object corresponding to the given XML.
|
||||
"""
|
||||
return atom.CreateClassFromXMLString(ProfileEntry, xml_string)
|
||||
|
||||
|
||||
def ProfileListEntryFromString(xml_string):
|
||||
"""Converts an XML string into a ProfileListEntry object.
|
||||
|
||||
Args:
|
||||
xml_string: string The XML describing a Health profile list feed entry.
|
||||
|
||||
Returns:
|
||||
A ProfileListEntry object corresponding to the given XML.
|
||||
"""
|
||||
return atom.CreateClassFromXMLString(ProfileListEntry, xml_string)
|
||||
|
||||
|
||||
def ProfileFeedFromString(xml_string):
|
||||
"""Converts an XML string into a ProfileFeed object.
|
||||
|
||||
Args:
|
||||
xml_string: string The XML describing a ProfileFeed feed.
|
||||
|
||||
Returns:
|
||||
A ProfileFeed object corresponding to the given XML.
|
||||
"""
|
||||
return atom.CreateClassFromXMLString(ProfileFeed, xml_string)
|
||||
|
||||
|
||||
def ProfileListFeedFromString(xml_string):
|
||||
"""Converts an XML string into a ProfileListFeed object.
|
||||
|
||||
Args:
|
||||
xml_string: string The XML describing a ProfileListFeed feed.
|
||||
|
||||
Returns:
|
||||
A ProfileListFeed object corresponding to the given XML.
|
||||
"""
|
||||
return atom.CreateClassFromXMLString(ProfileListFeed, xml_string)
|
263
python/gdata/health/service.py
Normal file
263
python/gdata/health/service.py
Normal file
@@ -0,0 +1,263 @@
|
||||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright 2009 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""HealthService extends GDataService to streamline Google Health API access.
|
||||
|
||||
HealthService: Provides methods to interact with the profile, profile list,
|
||||
and register/notices feeds. Extends GDataService.
|
||||
|
||||
HealthProfileQuery: Queries the Google Health Profile feed.
|
||||
|
||||
HealthProfileListQuery: Queries the Google Health Profile list feed.
|
||||
"""
|
||||
|
||||
__author__ = 'api.eric@google.com (Eric Bidelman)'
|
||||
|
||||
|
||||
import atom
|
||||
import gdata.health
|
||||
import gdata.service
|
||||
|
||||
|
||||
class HealthService(gdata.service.GDataService):
|
||||
|
||||
"""Client extension for the Google Health service Document List feed."""
|
||||
|
||||
def __init__(self, email=None, password=None, source=None,
|
||||
use_h9_sandbox=False, server='www.google.com',
|
||||
additional_headers=None, **kwargs):
|
||||
"""Creates a client for the Google Health service.
|
||||
|
||||
Args:
|
||||
email: string (optional) The user's email address, used for
|
||||
authentication.
|
||||
password: string (optional) The user's password.
|
||||
source: string (optional) The name of the user's application.
|
||||
use_h9_sandbox: boolean (optional) True to issue requests against the
|
||||
/h9 developer's sandbox.
|
||||
server: string (optional) The name of the server to which a connection
|
||||
will be opened.
|
||||
additional_headers: dictionary (optional) Any additional headers which
|
||||
should be included with CRUD operations.
|
||||
kwargs: The other parameters to pass to gdata.service.GDataService
|
||||
constructor.
|
||||
"""
|
||||
service = use_h9_sandbox and 'weaver' or 'health'
|
||||
gdata.service.GDataService.__init__(
|
||||
self, email=email, password=password, service=service, source=source,
|
||||
server=server, additional_headers=additional_headers, **kwargs)
|
||||
self.ssl = True
|
||||
self.use_h9_sandbox = use_h9_sandbox
|
||||
|
||||
def __get_service(self):
|
||||
return self.use_h9_sandbox and 'h9' or 'health'
|
||||
|
||||
def GetProfileFeed(self, query=None, profile_id=None):
|
||||
"""Fetches the users Google Health profile feed.
|
||||
|
||||
Args:
|
||||
query: HealthProfileQuery or string (optional) A query to use on the
|
||||
profile feed. If None, a HealthProfileQuery is constructed.
|
||||
profile_id: string (optional) The profile id to query the profile feed
|
||||
with when using ClientLogin. Note: this parameter is ignored if
|
||||
query is set.
|
||||
|
||||
Returns:
|
||||
A gdata.health.ProfileFeed object containing the user's Health profile.
|
||||
"""
|
||||
if query is None:
|
||||
projection = profile_id and 'ui' or 'default'
|
||||
uri = HealthProfileQuery(
|
||||
service=self.__get_service(), projection=projection,
|
||||
profile_id=profile_id).ToUri()
|
||||
elif isinstance(query, HealthProfileQuery):
|
||||
uri = query.ToUri()
|
||||
else:
|
||||
uri = query
|
||||
|
||||
return self.GetFeed(uri, converter=gdata.health.ProfileFeedFromString)
|
||||
|
||||
def GetProfileListFeed(self, query=None):
|
||||
"""Fetches the users Google Health profile feed.
|
||||
|
||||
Args:
|
||||
query: HealthProfileListQuery or string (optional) A query to use
|
||||
on the profile list feed. If None, a HealthProfileListQuery is
|
||||
constructed to /health/feeds/profile/list or /h9/feeds/profile/list.
|
||||
|
||||
Returns:
|
||||
A gdata.health.ProfileListFeed object containing the user's list
|
||||
of profiles.
|
||||
"""
|
||||
if not query:
|
||||
uri = HealthProfileListQuery(service=self.__get_service()).ToUri()
|
||||
elif isinstance(query, HealthProfileListQuery):
|
||||
uri = query.ToUri()
|
||||
else:
|
||||
uri = query
|
||||
|
||||
return self.GetFeed(uri, converter=gdata.health.ProfileListFeedFromString)
|
||||
|
||||
def SendNotice(self, subject, body=None, content_type='html',
|
||||
ccr=None, profile_id=None):
|
||||
"""Sends (posts) a notice to the user's Google Health profile.
|
||||
|
||||
Args:
|
||||
subject: A string representing the message's subject line.
|
||||
body: string (optional) The message body.
|
||||
content_type: string (optional) The content type of the notice message
|
||||
body. This parameter is only honored when a message body is
|
||||
specified.
|
||||
ccr: string (optional) The CCR XML document to reconcile into the
|
||||
user's profile.
|
||||
profile_id: string (optional) The profile id to work with when using
|
||||
ClientLogin. Note: this parameter is ignored if query is set.
|
||||
|
||||
Returns:
|
||||
A gdata.health.ProfileEntry object of the posted entry.
|
||||
"""
|
||||
if body:
|
||||
content = atom.Content(content_type=content_type, text=body)
|
||||
else:
|
||||
content = body
|
||||
|
||||
entry = gdata.GDataEntry(
|
||||
title=atom.Title(text=subject), content=content,
|
||||
extension_elements=[atom.ExtensionElementFromString(ccr)])
|
||||
|
||||
projection = profile_id and 'ui' or 'default'
|
||||
query = HealthRegisterQuery(service=self.__get_service(),
|
||||
projection=projection, profile_id=profile_id)
|
||||
return self.Post(entry, query.ToUri(),
|
||||
converter=gdata.health.ProfileEntryFromString)
|
||||
|
||||
|
||||
class HealthProfileQuery(gdata.service.Query):
|
||||
|
||||
"""Object used to construct a URI to query the Google Health profile feed."""
|
||||
|
||||
def __init__(self, service='health', feed='feeds/profile',
|
||||
projection='default', profile_id=None, text_query=None,
|
||||
params=None, categories=None):
|
||||
"""Constructor for Health profile feed query.
|
||||
|
||||
Args:
|
||||
service: string (optional) The service to query. Either 'health' or 'h9'.
|
||||
feed: string (optional) The path for the feed. The default value is
|
||||
'feeds/profile'.
|
||||
projection: string (optional) The visibility of the data. Possible values
|
||||
are 'default' for AuthSub and 'ui' for ClientLogin. If this value
|
||||
is set to 'ui', the profile_id parameter should also be set.
|
||||
profile_id: string (optional) The profile id to query. This should only
|
||||
be used when using ClientLogin.
|
||||
text_query: str (optional) The contents of the q query parameter. The
|
||||
contents of the text_query are URL escaped upon conversion to a URI.
|
||||
Note: this parameter can only be used on the register feed using
|
||||
ClientLogin.
|
||||
params: dict (optional) Parameter value string pairs which become URL
|
||||
params when translated to a URI. These parameters are added to
|
||||
the query's items.
|
||||
categories: list (optional) List of category strings which should be
|
||||
included as query categories. See gdata.service.Query for
|
||||
additional documentation.
|
||||
"""
|
||||
self.service = service
|
||||
self.profile_id = profile_id
|
||||
self.projection = projection
|
||||
gdata.service.Query.__init__(self, feed=feed, text_query=text_query,
|
||||
params=params, categories=categories)
|
||||
|
||||
def ToUri(self):
|
||||
"""Generates a URI from the query parameters set in the object.
|
||||
|
||||
Returns:
|
||||
A string containing the URI used to retrieve entries from the Health
|
||||
profile feed.
|
||||
"""
|
||||
old_feed = self.feed
|
||||
self.feed = '/'.join([self.service, old_feed, self.projection])
|
||||
|
||||
if self.profile_id:
|
||||
self.feed += '/' + self.profile_id
|
||||
self.feed = '/%s' % (self.feed,)
|
||||
|
||||
new_feed = gdata.service.Query.ToUri(self)
|
||||
self.feed = old_feed
|
||||
return new_feed
|
||||
|
||||
|
||||
class HealthProfileListQuery(gdata.service.Query):
|
||||
|
||||
"""Object used to construct a URI to query a Health profile list feed."""
|
||||
|
||||
def __init__(self, service='health', feed='feeds/profile/list'):
|
||||
"""Constructor for Health profile list feed query.
|
||||
|
||||
Args:
|
||||
service: string (optional) The service to query. Either 'health' or 'h9'.
|
||||
feed: string (optional) The path for the feed. The default value is
|
||||
'feeds/profile/list'.
|
||||
"""
|
||||
gdata.service.Query.__init__(self, feed)
|
||||
self.service = service
|
||||
|
||||
def ToUri(self):
|
||||
"""Generates a URI from the query parameters set in the object.
|
||||
|
||||
Returns:
|
||||
A string containing the URI used to retrieve entries from the
|
||||
profile list feed.
|
||||
"""
|
||||
return '/%s' % ('/'.join([self.service, self.feed]),)
|
||||
|
||||
|
||||
class HealthRegisterQuery(gdata.service.Query):
|
||||
|
||||
"""Object used to construct a URI to query a Health register/notice feed."""
|
||||
|
||||
def __init__(self, service='health', feed='feeds/register',
|
||||
projection='default', profile_id=None):
|
||||
"""Constructor for Health profile list feed query.
|
||||
|
||||
Args:
|
||||
service: string (optional) The service to query. Either 'health' or 'h9'.
|
||||
feed: string (optional) The path for the feed. The default value is
|
||||
'feeds/register'.
|
||||
projection: string (optional) The visibility of the data. Possible values
|
||||
are 'default' for AuthSub and 'ui' for ClientLogin. If this value
|
||||
is set to 'ui', the profile_id parameter should also be set.
|
||||
profile_id: string (optional) The profile id to query. This should only
|
||||
be used when using ClientLogin.
|
||||
"""
|
||||
gdata.service.Query.__init__(self, feed)
|
||||
self.service = service
|
||||
self.projection = projection
|
||||
self.profile_id = profile_id
|
||||
|
||||
def ToUri(self):
|
||||
"""Generates a URI from the query parameters set in the object.
|
||||
|
||||
Returns:
|
||||
A string containing the URI needed to interact with the register feed.
|
||||
"""
|
||||
old_feed = self.feed
|
||||
self.feed = '/'.join([self.service, old_feed, self.projection])
|
||||
new_feed = gdata.service.Query.ToUri(self)
|
||||
self.feed = old_feed
|
||||
|
||||
if self.profile_id:
|
||||
new_feed += '/' + self.profile_id
|
||||
return '/%s' % (new_feed,)
|
Reference in New Issue
Block a user