Skip to content

Instantly share code, notes, and snippets.

@odra
Last active March 30, 2020 20:32
Show Gist options
  • Save odra/e0437f22afe970837fd2406ae807df46 to your computer and use it in GitHub Desktop.
Save odra/e0437f22afe970837fd2406ae807df46 to your computer and use it in GitHub Desktop.
sample client
{
"swagger": "2.0",
"info": {
"license": {
"name": "GPLv3",
"url": "https://www.gnu.org/licenses/quick-guide-gplv3.en.html"
},
"title": "Fedora Account Service JSON API",
"version": "0.0.1"
},
"host": "fasjson.example.test",
"basePath": "/fasjson",
"schemes": ["http"],
"paths": {
"/v1/me": {
"get": {
"produces": [
"application/json"
],
"parameters": [],
"responses": {
"200": {
"description": "requester identity info",
"schema": {
"properties": {
"result": {
"$ref": "#/definitions/identity"
}
},
"type": "object"
}
},
"default": {
"description": "unexpected error",
"schema": {
"properties": {
"error": {
"$ref": "#/definitions/error"
}
},
"type": "object"
}
}
},
"operationId": "whoami"
}
}
},
"definitions": {
"error": {
"properties": {
"code": {
"type": "integer"
},
"data": {
"type": "object"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
],
"type": "object"
},
"group": {
"properties": {
"cn": {
"type": "string"
}
},
"type": "object"
},
"group_page": {
"properties": {
"data": {
"$ref": "#/definitions/groups"
},
"pagination": {
"properties": {
"current": {
"type": "integer"
},
"next": {
"type": "integer"
},
"previous": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
},
"groups": {
"items": {
"$ref": "#/definitions/group"
},
"type": "array"
},
"identity": {
"properties": {
"info": {
"type": "object"
},
"raw": {
"type": "string"
}
},
"type": "object"
},
"user": {
"properties": {
"creationts": {
"type": "string"
},
"givenname": {
"type": "string"
},
"gpgkeyids": {
"items": {
"type": "string"
},
"type": "array"
},
"ircnick": {
"type": "string"
},
"locale": {
"type": "string"
},
"locked": {
"type": "boolean"
},
"login": {
"type": "string"
},
"mails": {
"items": {
"type": "string"
},
"type": "array"
},
"surname": {
"type": "string"
},
"timezone": {
"type": "string"
}
},
"type": "object"
},
"user_ref": {
"properties": {
"uid": {
"type": "string"
}
},
"type": "object"
},
"user_ref_list": {
"items": {
"$ref": "#/definitions/user_ref"
},
"type": "array"
},
"user_ref_page": {
"properties": {
"data": {
"$ref": "#/definitions/user_ref_list"
},
"pagination": {
"properties": {
"current": {
"type": "integer"
},
"next": {
"type": "integer"
},
"previous": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
}
},
"x-components": {
"responses": {
"Error": {
"description": "unexpected error",
"schema": {
"properties": {
"error": {
"$ref": "#/definitions/error"
}
},
"type": "object"
}
}
}
}
}
from urllib.parse import urlparse
import requests
from requests_gssapi import HTTPSPNEGOAuth
from bravado import requests_client
from bravado.client import SwaggerClient
import gssapi
class HttpClient(requests_client.RequestsClient):
def __init__(self, principal, *args, **kwargs):
super(HttpClient, self).__init__(*args, **kwargs)
self.principal = principal
def authenticated_request(self, request_params):
name = gssapi.Name(self.principal, gssapi.NameType.kerberos_principal)
creds = gssapi.Credentials(name=name, usage='initiate')
request_params['auth'] = HTTPSPNEGOAuth()
return self.apply_authentication(requests.Request(**request_params))
class FasJsonClient(object):
def __init__(self, spec, principal):
self.principal = principal
self.http_client = HttpClient(principal)
self.api = SwaggerClient.from_spec(spec, http_client=self.http_client)
@classmethod
def from_url(cls, spec_url, base_url=None, principal=None):
res = requests.get(spec_url)
data = res.json()
return cls.from_spec(data, base_url=base_url, principal=principal)
@classmethod
def from_spec(cls, spec_data, base_url=None, principal=None):
if base_url:
parsed = urlparse(base_url)
spec_data['host'] = parsed.netloc
spec_data['basePath'] = parsed.path
spec_data['schemes'] = [parsed.scheme]
return cls(spec_data, principal=principal)
spec_url = 'https://gist.githubusercontent.com/odra/e0437f22afe970837fd2406ae807df46/raw/33d02d9465b5a6f4753e9f86f50dda71bde4ed97/spec.json'
def test1():
client = FasJsonClient.from_url(spec_url, principal='admin@EXAMPLE.TEST')
print(client.api.v1.whoami().response().result)
def test2():
client = FasJsonClient.from_url(spec_url, base_url='http://fasjson.example.test/fasjson', principal='admin@EXAMPLE.TEST')
print(client.api.v1.whoami().response().result)
test1()
test2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment