作为客户使用
这个库还可以作为任意OpenAPI 3的交互客户机
规格。例如,使用Linode’s OpenAPI 3 Specification作为参考:from openapi3 import OpenAPI
# load the spec file and read the yaml
with open('openapi.yaml') as f:
spec = yaml.safe_load(f.read())
# parse the spec into python - this will raise if the spec is invalid
api = OpenAPI(spec)
# call operations and receive result models
regions = api.call_getRegions()
# authenticate using a securityScheme defined in the spec's components.securtiySchemes
api.authenticate('personalAccessToken', my_token)
# call an operation that requires authentication
linodes = api.call_getLinodeInstances()
# call an opertaion with parameters
linode = api.call_getLinodeInstance(parameters={"linodeId": 123})
# the models returns are all of the same (generated) type
print(type(linode)) # openapi.schemas.Linode
type(linode) == type(linodes.data[0]) # True
# call an operation with a request body
new_linode = api.call_createLinodeInstance(data={"region":"us-east","type":"g6-standard-2"})
# the returned models is still of the correct type
type(new_linode) == type(linode) # True
http基本身份验证和http摘要身份验证的工作方式如下:# authenticate using a securityScheme defined in the spec's components.securtiySchemes
# Tuple with (username, password) as second argument
api.authenticate('basicAuth', ('username', 'password'))