Skip to content

Commit

Permalink
Merge pull request #3 from cdisc-org/AddRulesEndpoints
Browse files Browse the repository at this point in the history
Add rules endpoints
  • Loading branch information
nhaydel authored Feb 24, 2022
2 parents ca6e589 + c61b41e commit 8d5c653
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ get_cdashig - gets cdashig standard
get_terminology_package - get a ct package given the version
get_codelist_terms - returns an array of all terms in a codelist
get_codelist_term - returns the library representation of a codelist term
get_rule_catalogs - returns a list of links to all rule catalogs in the CDISC Library
get_rules_catalog - returns all rules in a single rules catalog.
get_rule - gets a rule definition given standard, version, and rule id
```
More info on CDISC Library API endpoints can be found here: https://www.cdisc.org/cdisc-library/api-documentation
Empty file added __init__.py
Empty file.
31 changes: 30 additions & 1 deletion cdisc_library_client/library_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests
import json
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from .custom_exceptions import ResourceNotFoundException
Expand Down Expand Up @@ -95,3 +94,33 @@ def get_codelist_term(self, version, codelist, term):
def get_qrs_instrument(self, instrument, version):
href = f"/mdr/qrs/instruments/{instrument}/versions/{version}"
return self.get_api_json(href)

def get_rule_catalogs(self):
"""
Returns an object containing a mapping of catalog titles to links to the catalogs of the form:
{
"sdtmig-3-4-cr": {
"href": "/mdr/rules/sdtmig/3-4",
"title": "sdtmig Conformance Rules v3-4",
"type": "Conformance Rules Package"
}...
}
"""
href = f"/mdr/rules"
response = self.get_api_json(href)
return response.get("_links", {}).get("catalogs", {})

def get_rules_catalog(self, standard: str, version: str):
"""
Returns an object containing a mapping of rule id to rule definitions
"""
href = f"/mdr/rules/{standard}/{version}"
response = self.get_api_json(href)
return response.get("rules", {})

def get_rule(self, standard: str, version: str, rule_id: str):
"""
Returns a rule definition given a standard, version and rule id.
"""
href = f"/mdr/rules/{standard}/{version}/rule/{rule_id}"
return self.get_api_json(href)
15 changes: 15 additions & 0 deletions examples/get_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
from cdisc_library_client import CDISCLibraryClient

if __name__ == "__main__":
api_key = os.environ.get("CDISC_LIBRARY_API_KEY")
client = CDISCLibraryClient(api_key=api_key)

catalogs = client.get_rule_catalogs()
assert catalogs
for catalog, link in catalogs.items():
href = link["href"]
standard = href.split("/")[-2]
version = href.split("/")[-1]
rules = client.get_rules_catalog(standard, version)
print(f"{catalog} has {len(rules.values())} rules")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.1.3'
__version__ = '0.1.4'

import setuptools

Expand Down
Empty file added tests/conftest.py
Empty file.
47 changes: 47 additions & 0 deletions tests/test_library_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import imp
from cdisc_library_client import CDISCLibraryClient
from unittest import mock

@mock.patch('cdisc_library_client.library_client.CDISCLibraryClient.get_api_json')
def test_get_rule_catalogs(mock_get_api_json):
client = CDISCLibraryClient(api_key="test")
mock_response = {
"_links": {
"catalogs": {
"sdtmig-3-4-cr": {
"href": "/mdr/rules/sdtmig/3-4",
"title": "sdtmig Conformance Rules v3-4",
"type": "Conformance Rules Package"
}
},
"self": {
"href": "/mdr/rules",
"title": "CDISC Library Rules Catalog List",
"type": "CDISC Library Product List"
}
}
}
mock_get_api_json.return_value = mock_response
rule_catalogs = client.get_rule_catalogs()
assert rule_catalogs == mock_response["_links"]["catalogs"]

@mock.patch('cdisc_library_client.library_client.CDISCLibraryClient.get_api_json')
def test_get_rules_catalog(mock_get_api_json):
client = CDISCLibraryClient(api_key="test")
mock_response = {
"_links": {
"self": {
"href": "/mdr/rules/sdtmig/3-4",
"title": "sdtmig Conformance Rules v3-4",
"type": "Conformance Rules Package"
}
},
"rules": {
"rule1": 10,
"rule2": 20
}
}
mock_get_api_json.return_value = mock_response
rules_catalog = client.get_rules_catalog("sdtmig", "3-4")
assert rules_catalog == mock_response["rules"]

0 comments on commit 8d5c653

Please sign in to comment.