Skip to content

Instantly share code, notes, and snippets.

@els-pnw
Created January 26, 2012 13:51
Show Gist options
  • Save els-pnw/1682852 to your computer and use it in GitHub Desktop.
Save els-pnw/1682852 to your computer and use it in GitHub Desktop.
trying out ReST
#!/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')
@els-pnw
Copy link
Author

els-pnw commented Jan 26, 2012

current version of the code I get the following errors:

Started GET "/headpin//api/organizations/ACME_Corporation/environments?name=DEV" for 10.11.9.191 at Thu Jan 26 08:41:18 -0500 2012
Processing by Api::EnvironmentsController#index as HTML
Parameters: {"name"=>"DEV", "_json"=>nil, "organization_id"=>"ACME_Corporation"}
ActiveRecord::StatementInvalid: 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
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/connection_adapters/abstract_adapter.rb:207:in log' /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/connection_adapters/postgresql_adapter.rb:514:inexecute'
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/connection_adapters/postgresql_adapter.rb:1004:in `select_raw'
...

Started POST "/headpin//api/organizations/ACME_Corporation/systems" for 10.11.9.191 at Thu Jan 26 08:41:18 -0500 2012
Processing by ErrorsController#routing as HTML
Parameters: {"facts"=>{"distribution.name"=>"Fedora", "cpu.cpu_socket(s)"=>"1"}, "name"=>"restfulserver", "cp_type"=>"system", "organization_id"=>"ACME_Corporation"}
Rendered common/_config.html.haml (0.5ms)
Rendered layouts/_ajax_notices.haml (112.7ms)
Rendered layouts/_notification.haml (0.2ms)
Rendered common/_common_i18n.html.haml (0.5ms)
Rendered layouts/_header.haml (2.4ms)
Rendered layouts/_footer.haml (0.3ms)
Rendered common/404.html.haml within layouts/katello (122.6ms)
Completed 404 Not Found in 124ms (Views: 123.3ms | ActiveRecord: 0.0ms)

@els-pnw
Copy link
Author

els-pnw commented Jan 26, 2012

$ ./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 Manager

Subscription 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>

404 - That page doesn't exist or is missing!

You may have mistyped the address or the page may have moved.

Back

  • Copyright © 2012 Red Hat, Inc.
Subscription Asset Manager Version: Unknown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment