Created
October 26, 2014 10:11
-
-
Save ohe/ccf6b117b76cba9b77af to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from contextlib import contextmanager | |
from functools import partial | |
from requests import request | |
from slugify import slugify | |
BASE="http://haltalk.herokuapp.com" | |
@contextmanager | |
def ignored(*exceptions): | |
try: | |
yield | |
except exceptions: | |
pass | |
# NOTE: haltalk was previously opened to the world, leading to a lots of shitty | |
# entries, some code below is only here to removes them | |
class HALClient(object): | |
@staticmethod | |
def request(endpoint): | |
return request("GET", BASE + endpoint).json() or {} | |
def __init__(self, endpoint="/"): | |
response = self.request(endpoint) | |
for key in response.get('_links', {}).keys(): | |
if key.startswith("ht:") : | |
if isinstance(response['_links'][key], dict): | |
with ignored(ValueError): # Useless if each entry is well-formated | |
setattr(self, key[3:], | |
partial(HALClient, response['_links'][key]['href'])) | |
if isinstance(response['_links'][key], list): | |
for each in response['_links'][key]: | |
with ignored(): # Useless if each entry is well-formated | |
title = each["title"] or "NONE" # Useless if each entry is well-formated | |
if isinstance(title, basestring): | |
key = slugify(title, separator="_") | |
if len(key) < 50: # Useless if each entry is well-formated | |
setattr(self, key, partial(HALClient, each['href'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment