2022-02-21 15:07:04 +00:00
|
|
|
'''
|
|
|
|
Tutorial link: https://realpython.com/flask-connexion-rest-api-part-2/
|
2022-02-22 15:36:49 +00:00
|
|
|
https://github.com/realpython/materials/blob/master/flask-connexion-rest-part-2/version_1/people.py
|
2022-02-21 15:07:04 +00:00
|
|
|
Sqlalchemy version: 1.2.15
|
|
|
|
Python version: 3.7
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
2022-02-22 15:36:49 +00:00
|
|
|
# import sys, inspect
|
|
|
|
# currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
|
|
|
# parentdir = os.path.dirname(currentdir)
|
|
|
|
# sys.path.insert(0, parentdir)
|
2022-02-28 16:25:48 +00:00
|
|
|
# import requests
|
2022-02-22 15:36:49 +00:00
|
|
|
from sqlalchemy.orm import session
|
2022-02-28 16:25:48 +00:00
|
|
|
|
|
|
|
from gschliefgraben_glasfaser.models import ObservationSchema, Person, PersonSchema, Observation
|
|
|
|
from gschliefgraben_glasfaser.my_api import MyApi
|
2022-02-23 15:46:47 +00:00
|
|
|
from db.pg_models import create_pg_session
|
|
|
|
#from models import Person, PersonSchema
|
2022-02-21 15:07:04 +00:00
|
|
|
# response = requests.get('https://api.com/')
|
|
|
|
# print(response) # shows the response's HTTP status code
|
|
|
|
# print(response.json()) # shows the response's JSON response body, if it has one
|
|
|
|
# print(response.content) # get the data content of the response
|
2022-02-22 15:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-02-23 15:46:47 +00:00
|
|
|
''' main method '''
|
|
|
|
db_user = os.environ.get("POSTGIS_DBUSER")
|
|
|
|
print(db_user)
|
2022-02-22 15:36:49 +00:00
|
|
|
|
|
|
|
pg_session: session = create_pg_session()
|
2022-02-28 16:25:48 +00:00
|
|
|
# pg_person: Person = pg_session.query(Person).first()
|
|
|
|
observation: Observation = pg_session.query(Observation).first()
|
|
|
|
# print(pg_person)
|
2022-02-22 15:36:49 +00:00
|
|
|
|
|
|
|
# serialize db data to json
|
2022-02-28 16:25:48 +00:00
|
|
|
# person_schema = PersonSchema()
|
|
|
|
# dump_data = person_schema.dump(pg_person)
|
2022-02-25 15:05:31 +00:00
|
|
|
# print(dump_data)
|
2022-02-28 16:25:48 +00:00
|
|
|
# serialize db data to json
|
|
|
|
observation_schema = ObservationSchema()
|
|
|
|
dump_data = observation_schema.dump(observation)
|
|
|
|
print(dump_data)
|
2022-02-22 15:36:49 +00:00
|
|
|
|
2022-03-01 17:28:09 +00:00
|
|
|
# # deserialize to db model
|
2022-02-23 15:46:47 +00:00
|
|
|
# load_data: Person = person_schema.load(dump_data)
|
|
|
|
# print(load_data)
|
|
|
|
|
2022-02-25 15:05:31 +00:00
|
|
|
# request ortmann api
|
|
|
|
# token = 'eyJraWQiOiJlakFmX1MwMTBMU3doS0Zod05wZDQtQkZPYTM4cDRYRE1zU1hFa0lrRlhFIiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULkZRUHNCOWh5Snd6eEM5d3ZWelRvaTNpZVlMWlJiT3U4YzFCbWJWRGM1SFkiLCJpc3MiOiJodHRwczovL2Rldi01MjUwMDA2Lm9rdGEuY29tL29hdXRoMi9kZWZhdWx0IiwiYXVkIjoiYXBpOi8vZGVmYXVsdCIsImlhdCI6MTY0NTc4Mjg0NSwiZXhwIjoxNjQ1Nzg2NDQ1LCJjaWQiOiIwb2EyOWhzdGZ3RnFya1BrUDVkNyIsInNjcCI6WyJnc2NobGllZmdyYWJlbiJdLCJzdWIiOiIwb2EyOWhzdGZ3RnFya1BrUDVkNyJ9.c-pTs-3VJMnFO2SOqxOvsABAloprUmOjk6SO9J71NrgLj7claKZOMLZxRyUeSBLWCJFFNI3A6xMd4twEexjJdUR8UEM4U50srxr2p_enaMm1_jZTSt_76u6H05kwV-A2AOQPkx-Fxxaj_PDjT7w43Zlg6SUEoT11uGKR6KtxVYbclGtWgOR7wvH4NZav-P_EDjHwHxbk2kQSf7tBU1JbWl74Xt58gzv1t8VNtLYLICabRsuTNQUNiO7Y1rtUEav4ugf7WZMIY1cP_4rCupZrAFbxrnyprAuXA2x01Z9hbFmiaK0QDlrwHcCHL_1fKvj9uIbO5JeI1x81X6g7eAxQdA'
|
|
|
|
# response = requests.get('https://api.dgnss-sensors.com/gschliefgraben?sensors=("inclino1_14")',
|
|
|
|
# headers={
|
|
|
|
# 'Authorization': 'Bearer' + token,
|
|
|
|
# 'cache-control': 'no-cache',
|
|
|
|
# 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
# 'accept': 'application/json'
|
|
|
|
# },
|
|
|
|
# data='grant_type=client_credentials&scope=gschliefgraben')
|
|
|
|
# print(response)
|
|
|
|
token_api = os.environ.get("TOKEN_API")
|
2022-02-28 16:25:48 +00:00
|
|
|
test_api = MyApi(token_api)
|
|
|
|
data = test_api.getSensorData("inclino1_14")
|
2022-03-01 17:28:09 +00:00
|
|
|
observation_array = (data['FeatureCollection']['Features'][0]['geometry']['properties'][0])
|
|
|
|
print(observation_array)
|
2022-02-23 15:46:47 +00:00
|
|
|
# create(dump_data)
|
2022-03-01 17:28:09 +00:00
|
|
|
# # deserialize to db model
|
|
|
|
observation_schema = ObservationSchema(many=True)
|
|
|
|
observations: Observation = observation_schema.load(observation_array)
|
|
|
|
print(observations)
|
2022-02-22 15:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create(person_json: PersonSchema):
|
|
|
|
"""
|
|
|
|
This function creates a new person in the people structure
|
|
|
|
based on the passed-in person data
|
|
|
|
:param person: person to create in people structure
|
|
|
|
:return: 201 on success, 406 on person exists
|
|
|
|
"""
|
|
|
|
|
|
|
|
login = person_json.get('login')
|
|
|
|
#lname = person.get('lname')
|
2022-02-23 15:46:47 +00:00
|
|
|
db_session = create_pg_session()
|
2022-02-22 15:36:49 +00:00
|
|
|
|
|
|
|
# existing_person = Person.query \
|
|
|
|
# .filter(Person.login == login) \
|
|
|
|
# .one_or_none()
|
2022-02-25 15:05:31 +00:00
|
|
|
existing_person: bool = (
|
|
|
|
db_session.query(Person)
|
2022-02-23 15:46:47 +00:00
|
|
|
.filter(Person.login == login)
|
2022-02-22 15:36:49 +00:00
|
|
|
.one_or_none()
|
|
|
|
)
|
|
|
|
|
|
|
|
# Can we insert this person?
|
|
|
|
if existing_person is None:
|
2022-02-25 15:05:31 +00:00
|
|
|
# Create a person instance using the schema and the passed in person
|
2022-02-22 15:36:49 +00:00
|
|
|
schema = PersonSchema()
|
|
|
|
# deserialize to object
|
|
|
|
new_person: Person = schema.load(person_json)
|
|
|
|
|
2022-02-23 15:46:47 +00:00
|
|
|
# Add the person to the database
|
|
|
|
db_session.add(new_person)
|
|
|
|
db_session.commit()
|
2022-02-22 15:36:49 +00:00
|
|
|
|
|
|
|
# Serialize and return the newly created person in the response
|
|
|
|
data = schema.dump(new_person)
|
|
|
|
return data, 201
|
|
|
|
# Otherwise, nope, person exists already
|
|
|
|
else:
|
|
|
|
print(409, f'Person {login} exists already')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|