PKu qGeZü¾ramlient/utils.py# -*- coding: utf-8 -*- """ Utility functions used in ramlient. """ try: # python 2.x from urlparse import urlparse except ImportError: # python 3.x from urllib.parse import urlparse import requests def download_file(url): """ Downloads a file from the specified URL. :param str url: The URL to the file to be downloaded :returns: the downloaded file's content :rtype: str """ response = requests.get(url) if response.status_code is not 200: return None return response.text def is_url(url): """ Check if given URL is a valid URL. :param str url: The url to validate :returns: if the url is valid or not :rtype: bool """ return urlparse(url).scheme != "" PKU.qGiÌ´MMramlient/request.py# -*- coding: utf-8 -*- """ Module which provides functionality to make requests to the API from the path of RAML nodes. """ import requests from .exceptions import UnsupportedHTTPMethodError SUPPORTED_METHODS = ["OPTIONS", "CONNECT", "TRACE", "PATCH", "DELETE", "GET", "POST", "PUT"] def prepare_request(node): """ Prepare request to node's API route :param Node node: the RAML node object """ if node.resource.method.upper() not in SUPPORTED_METHODS: raise UnsupportedHTTPMethodError(node.resource.method) def request(**kwargs): """ Make request to node's API route with the given keyword arguments """ # TODO: check if kwargs are valid response = requests.request(node.resource.method, node.path) return response return request PKY.qGŒáFMMramlient/exceptions.py# -*- coding: utf-8 -*- """ Module with all ramlient specific Exceptions. """ class RamlientError(Exception): """ Base Exception for all ramlient based Exceptions """ pass class ResourceNotFoundError(RamlientError): """ Exception which is raised if a certain resource does not exist """ def __init__(self, resource_path): super(ResourceNotFoundError, self).__init__("No such Resource found: {0}".format(resource_path)) class UnsupportedHTTPMethodError(RamlientError): """ Exception which is raised if a certain HTTP method is not supported """ def __init__(self, method): super(UnsupportedHTTPMethodError, self).__init__("No such HTTP method: {0}".format(method)) class UnsupportedResourceMethodError(RamlientError): """ Exception which is raised if the certain resource does not support the certain method """ def __init__(self, resource_path, method): super(UnsupportedResourceMethodError, self).__init__("Resource '{0}' does not support method '{1}'".format(resource_path, method)) PK§1qGü5ÁÁramlient/core.py# -*- coding: utf-8 -*- """ Core functionality for providing python interface to RAML API. """ import codecs import ramlfications from collections import namedtuple from . import utils from .request import SUPPORTED_METHODS, prepare_request from .exceptions import ResourceNotFoundError, UnsupportedResourceMethodError NodeParameter = namedtuple("NodeParameter", ["resource", "parameter"]) class Node(object): """ Wrapper for the RAML ResourceNode """ def __init__(self, client, resource): self.client = client self.resource = resource def __repr__(self): return "".format(repr(self.resource)) def __getattr__(self, attr): """ Accessing sub node """ if attr.upper() in SUPPORTED_METHODS: self._patch_resource(attr) return prepare_request(self) resource = self.client.get_resource(self.resource.path + "/", attr) if not resource: raise ResourceNotFoundError(self.resource.path + "/" + attr) if hasattr(resource, "parameter"): return lambda x: ParameterizedNode(self.client, resource.resource, {resource.parameter: x}) else: return Node(self.client, resource) @property def path(self): """ Returns the RAML resource node's path """ return self.resource.path def _patch_resource(self, method): """ Patch the current RAML ResourceNode by the resource with the correct method if it exists If the resource with the specified method does not exist an exception is raised. :param str method: the method of the resource :raises UnsupportedResourceMethodError: if resource does not support the method """ resource = self.client.get_resource("", self.resource.path, method) if not resource: raise UnsupportedResourceMethodError(self.resource.path, method) self.resource = resource class ParameterizedNode(Node): """ Wrapper for the RAML ResourceNode combined with the values of the dynamic part from the URL """ def __init__(self, client, resource, parameter): super(ParameterizedNode, self).__init__(client, resource) self.parameter = parameter def __repr__(self): return "".format(self.resource, self.parameter) @property def path(self): """ Returns the RAML resource node's path """ return self.resource.path.format(**self.parameter) class Client(object): """ RAML client """ def __init__(self, ramlfile): self.ramlfile = ramlfile self.raml = None self.parse_raml() def parse_raml(self): """ Parse RAML file """ if utils.is_url(self.ramlfile): raml = utils.download_file(self.ramlfile) else: with codecs.open(self.ramlfile, "rb", encoding="utf-8") as raml_f: raml = raml_f.read() loader = ramlfications.loads(raml) config = ramlfications.setup_config(None) self.raml = ramlfications.parse_raml(loader, config) def __getattr__(self, attr): """ Access ResourceNode from RAML """ resource = self.get_resource("/", attr) if not resource: raise ResourceNotFoundError(attr) return Node(self, resource) def get_resource(self, base_resource_path, resource_path, method=None): """ Gets a resource by it's path and optional by it's method This method does not care about the supported resource methods unless it is specified. :param str resource_path: The path of the resource :param str method: The method of the path. :returns: the resource if it exists or None :rtype: ResourceNode """ basic_path = base_resource_path + resource_path dynamic_path = base_resource_path + "{" + resource_path + "}" for resource in self.raml.resources: method_matched = method is None or resource.method == method if resource.path == basic_path and method_matched: return resource if resource.path == dynamic_path and method_matched: return NodeParameter(resource=resource, parameter=resource_path) return None PKR2qGp3#‘¥¥ramlient/__init__.py# -*- coding: utf-8 -*- """ The name `ramlient` is combined from `RAML` and `client`. As the name says, `ramlient` automatically creates a python client for APIs providing a RAML file. """ __VERSION__ = "0.0.1" __LICENSE__ = "MIT" __AUTHOR__ = "Timo Furrer" __AUTHOR_EMAIL__ = "tuxtimo@gmail.com" __DESCRIPTION__ = "foo" __URL__ = "" __DOWNLOAD_URL__ = "" from .core import Client, Node, ParameterizedNode PKÕ3qG^-Ò (ramlient-0.0.1.dist-info/DESCRIPTION.rstUNKNOWN PKÕ3qGV°sÃjj&ramlient-0.0.1.dist-info/metadata.json{"classifiers": ["Development Status :: 4 - Beta", "Environment :: Console", "Intended Audience :: Developers", "Intended Audience :: Education", "Intended Audience :: Other Audience", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: MacOS :: MacOS X", "Operating System :: OS Independent", "Operating System :: Microsoft :: Windows", "Operating System :: Microsoft :: Windows :: Windows 7", "Operating System :: Microsoft :: Windows :: Windows XP", "Operating System :: POSIX", "Operating System :: POSIX :: Linux", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation", "Topic :: Education :: Testing", "Topic :: Software Development", "Topic :: Software Development :: Testing"], "extensions": {"python.details": {"contacts": [{"email": "tuxtimo@gmail.com", "name": "Timo Furrer", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}}}, "extras": [], "generator": "bdist_wheel (0.26.0)", "license": "MIT", "metadata_version": "2.0", "name": "ramlient", "platform": "Linux", "run_requires": [{"requires": ["ramlfications (>=0.1.8)", "requests"]}], "summary": "foo", "version": "0.0.1"}PKÕ3qG_À &ramlient-0.0.1.dist-info/top_level.txtramlient PKÕ3qGìndªnnramlient-0.0.1.dist-info/WHEELWheel-Version: 1.0 Generator: bdist_wheel (0.26.0) Root-Is-Purelib: true Tag: py2-none-any Tag: py3-none-any PKÕ3qGDLÞœœ!ramlient-0.0.1.dist-info/METADATAMetadata-Version: 2.0 Name: ramlient Version: 0.0.1 Summary: foo Home-page: UNKNOWN Author: Timo Furrer Author-email: tuxtimo@gmail.com License: MIT Platform: Linux Platform: Windows Platform: MAC OS X Classifier: Development Status :: 4 - Beta Classifier: Environment :: Console Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Education Classifier: Intended Audience :: Other Audience Classifier: License :: OSI Approved :: MIT License Classifier: Natural Language :: English Classifier: Operating System :: MacOS :: MacOS X Classifier: Operating System :: OS Independent Classifier: Operating System :: Microsoft :: Windows Classifier: Operating System :: Microsoft :: Windows :: Windows 7 Classifier: Operating System :: Microsoft :: Windows :: Windows XP Classifier: Operating System :: POSIX Classifier: Operating System :: POSIX :: Linux Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: Implementation Classifier: Topic :: Education :: Testing Classifier: Topic :: Software Development Classifier: Topic :: Software Development :: Testing Requires-Dist: ramlfications (>=0.1.8) Requires-Dist: requests UNKNOWN PKÕ3qGä„þAooramlient-0.0.1.dist-info/RECORDramlient/__init__.py,sha256=de_gurcpToZ1MPZG2E2lOgsaCZ6S4yrH0_NBBOjDHtQ,421 ramlient/core.py,sha256=dqqGmj117sJST6IW0nAVEhgXSGDtMpXSxiRqHgzj8vA,4545 ramlient/exceptions.py,sha256=uZNS88MZSq5rik2fo-tzUX0C0XHl_W9K5vOGG3L3GCw,1101 ramlient/request.py,sha256=daWpjTJfV4jvYz93GIfxv-bpP8nJZJzyfnovdwEBGx0,845 ramlient/utils.py,sha256=9TKSOELKn802pjPcSdH5F5XK8rB-t1Fhx2-wE1RsV-A,783 ramlient-0.0.1.dist-info/DESCRIPTION.rst,sha256=OCTuuN6LcWulhHS3d5rfjdsQtW22n7HENFRh6jC6ego,10 ramlient-0.0.1.dist-info/METADATA,sha256=h1Lfa5EZYKa2EL-oyfuTkKBvx3avL-aC6O-xd5lGWYU,1436 ramlient-0.0.1.dist-info/RECORD,, ramlient-0.0.1.dist-info/WHEEL,sha256=GrqQvamwgBV4nLoJe0vhYRSWzWsx7xjlt74FT0SWYfE,110 ramlient-0.0.1.dist-info/metadata.json,sha256=GYTTBX_kR-pucSsK8UhoTj6lZjBOcZr0OrvkVib6wF8,1386 ramlient-0.0.1.dist-info/top_level.txt,sha256=YbdrwNwPEE9uPMV__giq49qT6fiaijJD-I60qapn7Vw,9 PKu qGeZü¾ramlient/utils.pyPKU.qGiÌ´MM>ramlient/request.pyPKY.qGŒáFMM¼ramlient/exceptions.pyPK§1qGü5ÁÁ= ramlient/core.pyPKR2qGp3#‘¥¥,ramlient/__init__.pyPKÕ3qG^-Ò (ramlient-0.0.1.dist-info/DESCRIPTION.rstPKÕ3qGV°sÃjj&Sramlient-0.0.1.dist-info/metadata.jsonPKÕ3qG_À &%ramlient-0.0.1.dist-info/top_level.txtPKÕ3qGìndªnnN%ramlient-0.0.1.dist-info/WHEELPKÕ3qGDLÞœœ!ø%ramlient-0.0.1.dist-info/METADATAPKÕ3qGä„þAooÓ+ramlient-0.0.1.dist-info/RECORDPK */