Commit 675a44a6 authored by Spencer Sutton's avatar Spencer Sutton
Browse files

Finished resetting back to ADO

parent 6d7c6d98
build
dist
osdu_api.egg-info
__pycache__
\ No newline at end of file
Apache 2.0
\ No newline at end of file
A package to interface with OSDU microservices
To install locally:
- Make sure you have setuptools and wheel installed `python3 -m pip install --user --upgrade setuptools wheel`
- Run `python setup.py sdist bdist_wheel`
- Make sure osdu-api isn't already installed `pip uninstall osdu-api`
- Run `python -m pip install <YOUR PATH TO PACKAGE>/dist/osdu_api-0.0.1-py3-none-any.whl` make sure to substitute your machine's path in that command
Example import after installing:
`from osdu_api.storage.record_client import RecordClient`
import sys, os
import importlib
import yaml # MIT license
import requests
from osdu_api.model.http_method import HttpMethod
'''
Base client that is meant to be extended by service specific clients
'''
class BaseClient:
'''
Base client gets initialized with configuration values and a bearer token
based on provider-specific logic
'''
def __init__(self):
self._parse_config()
self.bearer_token = self._get_bearer_token()
'''
The path to the logic to get a valid bearer token is dynamically injected based on
what provider and entitlements module name is provided in the configuration yaml
'''
def _get_bearer_token(self):
entitlements_client = importlib.import_module('osdu_api.provider.%s.%s' % (self.provider, self.entitlements_module_name))
return entitlements_client.get_bearer_token()
'''
Parses a yaml filed named osdu_api.yaml. All config values listed below are meant to
be required except URLs to specific services which may or may not be used depending
on the specific script
'''
def _parse_config(self):
config_file_location = os.path.join(sys.path[0], 'osdu_api.yaml')
with open(config_file_location, 'r') as config_file:
config = yaml.load(config_file)
self.data_partition_id = self._parse_config_value(config, 'data_partition_id', True)
self.storage_url = self._parse_config_value(config, 'storage_url', False)
self.search_url = self._parse_config_value(config, 'search_url', False)
self.provider = self._parse_config_value(config, 'provider', True)
self.entitlements_module_name = self._parse_config_value(config, 'entitlements_module_name', True)
'''
Used during parsing of the yaml config file. Will raise an exception if a required config
value is missing
'''
def _parse_config_value(self, config, config_name, is_required):
config_value = ''
try:
config_value = config[config_name]
except TypeError:
if(is_required):
raise Exception('Config value %s missing and is required' % config_name)
else:
print('Config value %s missing' % config_name)
return config_value
'''
Makes a request using python's built in requests library. Takes additional headers if
necessary
'''
def make_request(self, method: HttpMethod, url: str, data = '', add_headers = {}, params = {}):
headers = {
'content-type': 'application/json',
'data-partition-id': self.data_partition_id,
'Authorization': self.bearer_token
}
if (len(add_headers) > 0):
for key, value in add_headers:
headers[key] = value
response = object
if (method == HttpMethod.GET):
response = requests.get(url=url, params=params, headers=headers)
elif (method == HttpMethod.POST):
response = requests.post(url=url, params=params, data=data, headers=headers)
elif (method == HttpMethod.PUT):
response = requests.put(url=url, params=params, data=data, headers=headers)
return response
\ No newline at end of file
'''
Acl model mirroring what's found in core common
'''
class Acl:
def __init__(self, viewers: list, owners: list):
self.viewers = viewers
self.owners = owners
from enum import Enum
class HttpMethod(Enum):
GET = 0
POST = 1
PUT = 2
\ No newline at end of file
from osdu_api.model.legal_compliance import LegalCompliance
'''
Legal model mirroring what's found in core common
'''
class Legal:
def __init__(self, legaltags: list, other_relevant_data_countries: list, status: LegalCompliance):
self.legaltags = legaltags
self.other_relevant_data_countries = other_relevant_data_countries
self.status = status
def get_dict(self):
legal_dict = {}
legal_dict['legaltags'] = self.legaltags
legal_dict['otherRelevantDataCountries'] = self.other_relevant_data_countries
legal_dict['status'] = str(self.status)
return legal_dict
\ No newline at end of file
from enum import Enum
'''
LegalCompliance model mirroring what's found in core common
'''
class LegalCompliance(Enum):
incompliant = 0
compliant = 1
def __str__(self):
return self.name
from osdu_api.model.acl import Acl
from osdu_api.model.legal import Legal
from osdu_api.model.legal_compliance import LegalCompliance
from osdu_api.model.record_ancestry import RecordAncestry
'''
A record model mirroring what's found in core common
'''
class Record:
def __init__(self, id: str, version: int, kind: str, acl: Acl, legal: Legal, data: dict, ancestry: RecordAncestry,
meta: dict):
self.id = id
self.version = version
self.kind = kind
self.acl = acl
self.legal = legal
self.data = data
self.ancestry = ancestry
self.meta = meta
'''
Overloaded constructor meant to throw KeyError if any record values are missing
from the dict
'''
@classmethod
def from_dict(cls, record_dict: dict):
id = record_dict['id']
version = record_dict['version']
kind = record_dict['kind']
acl = Acl(record_dict['acl']['viewers'], record_dict['acl']['owners'])
legal = Legal(record_dict['legal']['legaltags'], record_dict['legal']['otherRelevantDataCountries'],
LegalCompliance[record_dict['legal']['status']])
data = record_dict['data']
meta = record_dict['meta']
parents = []
try:
parents = record_dict['ancestry']['parents']
except KeyError:
# warn the user that ancestry wasn't found, not essential attribute
print('Attribute "ancestry" is missing from dict being converted to record')
ancestry = RecordAncestry(parents)
return cls(id, version, kind, acl, legal, data, ancestry, meta)
def convert_to_dict(self):
record_converted = self.__dict__
record_converted['acl'] = self.acl.__dict__
record_converted['legal'] = self.legal.get_dict()
record_converted['ancestry'] = self.ancestry.__dict__
return record_converted
'''
RecordAncestry model mirroring what's found in core common
'''
class RecordAncestry:
def __init__(self, parents: list):
self.parents = parents
\ No newline at end of file
class AggregationResponse:
def __init__(self, key: str, count: int):
self.key = key
self.count = count
\ No newline at end of file
from osdu_api.model.search.coordinate import Coordinate
class ByBoundingBox:
def __init__(self, top_left: Coordinate, bottom_right: Coordinate):
self.top_left = top_left
self.bottom_right = bottom_right
\ No newline at end of file
from osdu_api.model.search.coordinate import Coordinate
class ByDistance:
def __init__(self, distance: float, coordinate: Coordinate):
self.distance = distance
self.coordinate = coordinate
\ No newline at end of file
class ByGeoPolygon:
def __init__(self, coordinates: list):
self.coordinates = coordinates
\ No newline at end of file
class Coordinate:
# python's built-in float has double precision
def __init__(self, latitude: float, longitude: float):
self.latitude = latitude
self.longitude = longitude
\ No newline at end of file
from osdu_api.model.search.sort_query import SortQuery
from osdu_api.model.search.spatial_filter import SpatialFilter
class QueryRequest:
def __init__(self, kind: str, limit: int, query: str, return_highlighted_fields: bool,
returned_fields: list, sort: SortQuery, query_as_owner: bool, spatial_filter: SpatialFilter,
from: int, aggregate_by: str):
self.kind = kind
self.limit = limit
self.query = query
self.return_highlighted_fields = return_highlighted_fields
self.returned_fields = returned_fields
self.sort = sort
self.query_as_owner = query_as_owner
self.spatial_filter = spatial_filter
self.from = from
self.aggregate_by = aggregate_by
\ No newline at end of file
class QueryResponse:
def __init__(self, results: list, aggregations: list):
self.results = results
self.aggregations = aggregations
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment