Skip to content

Instantly share code, notes, and snippets.

@manning-ncsa
Last active October 27, 2023 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manning-ncsa/f074e37aa66e09b2dc4fd4b10332efdb to your computer and use it in GitHub Desktop.
Save manning-ncsa/f074e37aa66e09b2dc4fd4b10332efdb to your computer and use it in GitHub Desktop.
This Gist is a sandbox for learning how to use the https://github.com/p1c2u/openapi-core Python library.

openapi-core sandbox

This Gist is a sandbox for learning how to use the https://github.com/p1c2u/openapi-core Python library.

  1. Clone this repo
  2. Build the Docker image
  3. Run python openapicore.py

Build and run the script in a container:

docker build . -t openapi-core-test
docker run --rm -it openapi-core-test python openapicore.py 

The version of openapi-core is 0.18.1:

$ docker run --rm -it openapi-core-test bash
root@f8f5b3882df6:/# python
Python 3.12.0 (main, Oct 12 2023, 09:45:59) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import openapi_core
>>> openapi_core.__version__
'0.18.1'
FROM python:3
RUN pip install openapi-core
COPY ./ ./
openapi: 3.0.0
info:
version: 1.0.0
title: Bulk viscosity module
contact:
email: devnull@ncsa.illinois.edu
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.txt'
description: "Test of the OpenAPI validator"
servers:
- url: "/"
description: "Dummy API server"
paths:
/input/eos.yaml:
put:
summary: Input equation of state for neutral npe matter
description: >
A particular equation of state generated by a compatible MUSES module.
operationId: putEoS
security: []
requestBody:
description: Equation of state and module configuration
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Equation_Of_State_with_Leptons'
responses:
'200':
description: Successful operation
'400':
description: Bad request. Invalid input provided.
'500':
description: Unexpected error. Run time error in calculation.
/output/Urca_rate.yaml:
get:
summary: Rate of proton to neutron conversion
description: >
The Urca rate per unit volume of proton to neutron conversion
operationId: getUrca
responses:
'200':
description: calculated Urca rate
content:
application/json:
schema:
$ref: '#/components/schemas/Tabulated_Rate_of_n_to_p'
'404':
description: Data not found.
'500':
description: Unexpected error.
components:
schemas:
Equation_Of_State_with_Leptons:
title: Equation of State including leptons
description: Equation of state for neutral npe matter tabulated on a grid of nB,T,YQ
type: array
items:
type: object
properties:
n_B:
$ref: '#/components/schemas/Baryon_Density'
T:
$ref: '#/components/schemas/Temperature'
Y_Q:
$ref: '#/components/schemas/Charge_Fraction'
e_B:
$ref: '#/components/schemas/Baryon_Energy_Density'
p_B:
$ref: '#/components/schemas/Baryon_Pressure'
s_B:
$ref: '#/components/schemas/Baryon_Entropy_Density'
n_n:
$ref: '#/components/schemas/Neutron_Density'
n_p:
$ref: '#/components/schemas/Proton_Density'
mu_n:
$ref: '#/components/schemas/Neutron_Chemical_Potential'
mu_p:
$ref: '#/components/schemas/Proton_Chemical_Potential'
M_n:
$ref: '#/components/schemas/Neutron_Effective_Mass'
M_p:
$ref: '#/components/schemas/Proton_Effective_Mass'
U_n:
$ref: '#/components/schemas/Neutron_Energy_Shift'
U_p:
$ref: '#/components/schemas/Proton_Energy_Shift'
mu_NQ:
$ref: '#/components/schemas/NegCharge_Chemical_Potential'
Tabulated_Rate_of_n_to_p:
title: table of n to p conversion rate
description: table of n to p conversion rate as a function of nB,T,YQ
type: array
items:
type: object
properties:
n_B:
$ref: '#/components/schemas/Baryon_Density'
T:
$ref: '#/components/schemas/Temperature'
Y_Q:
$ref: '#/components/schemas/Charge_Fraction'
rate_n_to_p:
$ref: '#/components/schemas/Rate_n_to_p'
Baryon_Density:
title: Baryon Density
description: "density of baryons in MeV^3"
type: number
format: float
default: 1200000.0
example: 1.0e6
minimum: 0
Temperature:
title: Temperature
description: "temperature in MeV"
type: number
format: float
default: 0
example: 1.0
minimum: 0
Charge_Fraction:
title: Charge Fraction
description: "ratio of electric charge density carried by baryons to baryon density"
type: number
format: float
default: 0
example: 0.2
minimum: -1
maximum: 1
Baryon_Energy_Density:
title: Baryon Energy Density
description: "density of energy carried by baryons in MeV^4"
type: number
format: float
default: 0
example: 1.0e8
minimum: 0
Baryon_Pressure:
title: Baryon Pressure
description: "pressure due to baryons in MeV^4"
type: number
format: float
default: 0
example: 1.0e8
minimum: 0
Rate_n_to_p:
title: Rate of n to p conversion
description: "Rate of n to p conversion in MeV^4"
type: number
format: float
default: 0
example: 1.0e-8
Baryon_Entropy_Density:
title: Baryon Entropy Density
description: "entropy density of baryons in MeV^3"
type: number
format: float
default: 100000.0
example: 1.0e6
minimum: 0
Proton_Density:
title: Proton Density
description: "density of protons in MeV^3"
type: number
format: float
default: 120000.0
example: 1.0e5
minimum: 0
Neutron_Density:
title: Neutron Density
description: "density of neutrons in MeV^3"
type: number
format: float
default: 1200000.0
example: 1.0e6
minimum: 0
Neutron_Chemical_Potential:
title: Neutron Chemical Potential
description: "relativistic chemical potential for neutrons in MeV"
type: number
format: float
default: 1000.0
example: 1000.0
Proton_Chemical_Potential:
title: Proton Chemical Potential
description: "relativistic chemical potential for protons in MeV"
type: number
format: float
default: 1000.0
example: 1000.0
NegCharge_Chemical_Potential:
title: Chemical Potential for Negative Electric Charge
description: "chemical potential for negative electric charge in MeV"
type: number
format: float
default: 1000.0
example: 1000.0
Neutron_Effective_Mass:
title: Neutron Effective Mass
description: "effective Dirac mass of neutrons in MeV"
type: number
format: float
default: 939.0
example: 939.0
minimum: 0
Proton_Effective_Mass:
title: Proton Effective Mass
description: "effective Dirac mass of protons in MeV"
type: number
format: float
default: 938.0
example: 938.0
minimum: 0
Neutron_Energy_Shift:
title: Neutron Energy Shift
description: "energy shift (averaged interaction potential) of neutrons in MeV"
type: number
format: float
default: 939.0
example: 939.0
Proton_Energy_Shift:
title: Proton Energy Shift
description: "energy shift (averaged interaction potential) of protons in MeV"
type: number
format: float
default: 938.0
example: 938.0
from openapi_core.contrib.requests import RequestsOpenAPIRequest
from openapi_core import validate_request, validate_response
from openapi_core import Spec
import json
import yaml
from openapi_core.testing import MockRequest
from openapi_core.testing import MockResponse
openapi_spec_file_path = 'openapi.yaml'
print(f'''Importing the OpenAPI specification file: "{openapi_spec_file_path}"''')
with open(openapi_spec_file_path) as spec_file:
spec = Spec.from_file(spec_file)
print(f'''Validating the API path "/input/eos.yaml"...''')
# data = [1,2,3]
data = [
{
"n_B": 1000000.0,
"T": 1.0,
"Y_Q": 0.2,
"e_B": 100000000.0,
"p_B": 100000000.0,
"s_B": 1000000.0,
"n_n": 1000000.0,
"n_p": 100000.0,
"mu_n": 1000.0,
"mu_p": 1000.0,
"M_n": 939.0,
"M_p": 938.0,
"U_n": 939.0,
"U_p": 938.0,
"mu_NQ": 1000.0
}
]
request = MockRequest(
host_url='/',
method="PUT",
path="/input/eos.yaml",
data=json.dumps(data)
)
result = validate_request(spec=spec, request=request)
print(yaml.dump(result))
print(f'''Validating the API path "/output/Urca_rate.yaml"...''')
data = [
{
"n_B": 1000000.0,
"T": 1.0,
"Y_Q": 0.2,
"rate_n_to_p": 1e-8
}
]
request = MockRequest(
host_url='/',
method="GET",
path="/output/Urca_rate.yaml",
)
response = MockResponse(
status_code=200,
data=json.dumps(data)
)
result = validate_response(spec=spec, request=request, response=response)
print(yaml.dump(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment