Created
January 26, 2012 13:51
-
-
Save els-pnw/1682852 to your computer and use it in GitHub Desktop.
trying out ReST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import base64 | |
import httplib | |
import urllib | |
import locale | |
import os | |
import mimetypes | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
def process_response(response): | |
response_body = response.read() | |
#try: | |
# response_body = json.loads(response_body, encoding='utf-8') | |
#except: | |
# pass | |
return (response.status, response_body, response.getheaders()) | |
def https_connection(host, port=443): | |
return httplib.HTTPSConnection(host, port) | |
def build_url(path, queries=()): | |
path_prefix = '/headpin/api' | |
path = '/'.join((path_prefix, path)) | |
path = urllib.quote(str(path)) | |
queries = urllib.urlencode(queries) | |
if queries: | |
path = '?'.join((path, queries)) | |
return path | |
def prepare_body(body, multipart): | |
content_type = 'application/json' | |
#if multipart: | |
# content_type, body = self._encode_multipart_formdata(body) | |
#elif not isinstance(body, (type(None), Bytes, file)): | |
body = json.dumps(body) | |
return (content_type, body) | |
def request(method, path, queries=(), body=None, multipart=False, customHeaders={}): | |
username = 'admin' | |
password = 'admin' | |
headers = {} | |
raw = ':'.join((username, password)) | |
encoded = base64.encodestring(raw)[:-1] | |
headers['Authorization'] = 'Basic ' + encoded | |
connection = https_connection('cubert.usersys.redhat.com') | |
url = build_url(path,queries) | |
content_type, body = prepare_body(body, multipart) | |
headers['content-type'] = content_type | |
headers['content-length'] = str(len(body) if body else 0) | |
print (url) | |
connection.request(method, url, body=body, headers=dict(headers.items() + customHeaders.items())) | |
return process_response(connection.getresponse()) | |
def GET(path, queries=(), customHeaders={}): | |
return request('GET', path, queries, customHeaders=customHeaders) | |
def POST(path, body, multipart=False, customHeaders={}): | |
return request('POST', path, body=body, multipart=multipart, customHeaders=customHeaders) | |
def register(name, org): | |
#path = "environments/166/systems" | |
path = "organizations/%s/systems" % org | |
sysdata = { | |
"name" : name, | |
"cp_type" : "system", | |
"facts" : { | |
"distribution.name": "Fedora", | |
"cpu.cpu_socket(s)": "1"} | |
} | |
return POST(path, sysdata)[1] | |
def environment_by_name(orgId, envName): | |
#path = "organizations/ACME_Corporation/environments" | |
path = "organizations/%s/environments" % orgId | |
envs = GET(path, {"name": envName})[1] | |
if len(envs) > 0: | |
return envs[0] | |
else: | |
return None | |
envs = environment_by_name('ACME_Corporation', 'DEV') | |
print envs | |
register('restfulserver', 'ACME_Corporation') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ ./resttest.py
=====> THIS IS THE BODY
null
<========
/headpin/api/organizations/ACME_Corporation/environments?name=DEV
PGError: ERROR: column environments._json does not exist
LINE 1: ...ronments" WHERE "environments"."name" = 'DEV' AND "environme...
^
: SELECT "environments".* FROM "environments" WHERE "environments"."name" = 'DEV' AND "environments"."_json" IS NULL AND "environments"."organization_id" = 1
P
=====> THIS IS THE BODY
{"facts": {"distribution.name": "Fedora", "cpu.cpu_socket(s)": "1"}, "cp_type": "system", "name": "restfulserver"}
<========
/headpin/api/organizations/ACME_Corporation/systems
<title> ## Subscription Asset ManagerSubscription Management
</title> <script src="/headpin/assets/common.js?1326912901" type="text/javascript"></script> <script> // </script> <script> // </script> <script src="/headpin/assets/notices.js?1326912901" type="text/javascript"></script> <script type="text/javascript"> // </script>Subscription Asset Manager
404 - That page doesn't exist or is missing!
You may have mistyped the address or the page may have moved.
Back