Skip to content

Commit

Permalink
Initial set up space restclient
Browse files Browse the repository at this point in the history
  • Loading branch information
fanglinfang committed Nov 4, 2021
1 parent 5094839 commit e1a691e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 50 deletions.
29 changes: 5 additions & 24 deletions uw_space/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# Copyright 2021 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

"""
This is the interface for interacting with the Attesttation Web Service.
"""
import json
import logging
from restclients_core.exceptions import DataFailureException
from uw_space.dao import SPACE_DAO
from uw_space.models import Facility
from uw_space.utils import str_to_datetime

by_code_path = "/space/v1/facility.json?facility_code={}"
by_number_path = "/space/v1/facility/{}.json"
logger = logging.getLogger(__name__)


class Facility(object):
class Facilities(object):

def __init__(self):
self.dao = SPACE_DAO()
Expand Down Expand Up @@ -44,9 +40,9 @@ def search_by_number(self, facility_number):
{'url': url, 'status': response.status, 'data': response.data})
if response.status != 200:
raise DataFailureException(url, response.status, response.data)
return self.__process_map_json(json.loads(response.data))
return Facility.from_json(json.loads(response.data))

def __process_json(self, jdata):
def __process_json(self, json_data):
objs = []
facilitys = json_data.get("Facilitys")
for facility in facilitys:
Expand All @@ -57,21 +53,6 @@ def __process_json(self, jdata):
if building_resp.status != 200:
raise DataFailureException(
url, building_resp.status, building_resp.data)
obj = self.__process_map_json(json.loads(building_resp.data))
objs.append(obj)
objs.append(
Facility.from_json(json.loads(building_resp.data)))
return objs

def __process_map_json(self, jdata):
obj = Facility()
obj.facility_code = jdata.get("FacilityCode")
obj.facility_number = jdata.get("AggregateFacilityNumber")
obj.name = jdata.get("LongName")
cpoint = jdata.get("CenterPoint")
if cpoint:
obj.latitude = cpoint.get("Latitude")
obj.longitude = cpoint.get("Longitude")
site_json = jdata.get("Site")
if site_json:
obj.site = site_json.get("Description")
obj.last_updated = str_to_datetime(jdata.get("ModifiedDate"))
return obj
35 changes: 30 additions & 5 deletions uw_space/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,54 @@

import json
from restclients_core import models
from uw_space.utils import date_to_str
from uw_space.utils import date_to_str, str_to_datetime


class Facility(models.Model):
facility_code = models.CharField(max_length=32)
facility_number = models.CharField(max_length=32)
code = models.CharField(max_length=32)
last_updated = models.DateTimeField()
latitude = models.CharField(max_length=64)
longitude = models.CharField(max_length=64)
name = models.CharField(max_length=128)
number = models.CharField(max_length=32)
type = models.CharField(max_length=32)
site = models.CharField(max_length=96)

def __init__(self, *args, **kwargs):
super(Facility, self).__init__(*args, **kwargs)

@staticmethod
def from_json(json_data):
obj = Facility()
obj.code = json_data.get("FacilityCode")
obj.number = json_data.get("FacilityNumber")
obj.last_updated = str_to_datetime(json_data.get("ModifiedDate"))
obj.name = json_data.get("LongName")

cpoint = json_data.get("CenterPoint")
if cpoint:
obj.latitude = cpoint.get("Latitude")
obj.longitude = cpoint.get("Longitude")
site_json = json_data.get("Site")

if site_json:
obj.site = site_json.get("Description")

ftype = json_data.get("FacilityType")
if ftype:
obj.type = ftype.get("Description")
return obj

def json_data(self):
return {
"facility_code": self.facility_code,
"facility_number": self.facility_number,
"code": self.code,
"last_updated": date_to_str(self.last_updated),
"latitude": self.latitude,
"longitude": self.longitude,
"name": self.name,
"number": self.number,
"site": self.site,
"type": self.type,
}

def __str__(self):
Expand Down
10 changes: 7 additions & 3 deletions uw_space/tests/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@


@fdao_space_override
class TestSpace(TestCase):
class TestSpaceDao(TestCase):

def test_auth_header(self, mock_get_auth_token):
headers = SPACE_DAO()._custom_headers("GET", "/", {}, "")
def test_dao(self):
dao = SPACE_DAO()
self.assertEqual(dao.service_name(), "space")
self.assertTrue(
dao.service_mock_paths()[0].endswith(
"uw-restclients-space/uw_space/resources"))
40 changes: 40 additions & 0 deletions uw_space/tests/test_facilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2021 UW-IT, University of Washington
# SPDX-License-Identifier: Apache-2.0

from unittest import TestCase
from restclients_core.exceptions import DataFailureException
from uw_space import Facilities
from uw_space.utils import fdao_space_override

data = {
'code': 'MEB',
'last_updated': '2021-03-27 04:53:09',
'latitude': '47.653693',
'longitude': '-122.304747',
'name': 'Mechanical Engineering Building',
'number': '1347',
'site': 'Seattle Main Campus',
'type': 'Building'
}


@fdao_space_override
class TestSpace(TestCase):
def test_search_by_code(self):
fac = Facilities().search_by_code("MEB")
self.assertEqual(len(fac), 1)
self.assertEqual(fac[0].json_data(), data)

self.assertRaises(
DataFailureException,
Facilities().search_by_code, "None"
)

def test_search_by_number(self):
fac = Facilities().search_by_number("1347")
self.assertEqual(fac.json_data(), data)

self.assertRaises(
DataFailureException,
Facilities().search_by_number, "0"
)
17 changes: 0 additions & 17 deletions uw_space/tests/test_init.py

This file was deleted.

2 changes: 1 addition & 1 deletion uw_space/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
RESTCLIENTS_SPACE_DAO_CLASS='Mock')


def str_to_utc_datetime(s):
def str_to_datetime(s):
return parse(s) if (s is not None and len(s)) else None


Expand Down

0 comments on commit e1a691e

Please sign in to comment.