Skip to content

Commit

Permalink
Fix absolute_uri and explicit protocol definition
Browse files Browse the repository at this point in the history
Addresses second part of issue #44
  • Loading branch information
econchick committed Sep 12, 2015
1 parent 6e50336 commit 5558c95
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
26 changes: 25 additions & 1 deletion ramlfications/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ def get_resource(attribute):
"""Returns ``attribute`` defined at the resource level, or ``None``."""
return raw_data.get(attribute, {})

def get_parent(attribute):
if parent:
return getattr(parent, attribute, {})
return {}

def get_resource_type(attribute):
"""Returns ``attribute`` defined in the resource type, or ``None``."""
if type_() and root.resource_types:
Expand Down Expand Up @@ -967,14 +972,31 @@ def path():

def absolute_uri():
"""Set resource's absolute URI path."""
return root.base_uri + path()
uri = root.base_uri + path()
proto = protocols()
if proto:
uri = uri.split("://")
if len(uri) == 2:
uri = uri[1]
if root.protocols:
_proto = list(set(root.protocols) & set(proto))
# if resource protocols and root protocols share a protocol
# then use that one
if _proto:
uri = _proto[0].lower() + "://" + uri
# if no shared protocols, use the first of the resource
# protocols
else:
uri = proto[0].lower() + "://" + uri
return uri

def protocols():
"""Set resource's supported protocols."""
trait_protocols = get_trait("protocols")
r_type_protocols = get_resource_type("protocols")
m_protocols = get_method("protocols")
r_protocols = get_resource("protocols")
parent = get_parent("protocols")
if m_protocols:
return m_protocols
elif r_type_protocols:
Expand All @@ -983,6 +1005,8 @@ def protocols():
return trait_protocols
elif r_protocols:
return r_protocols
elif parent:
return parent
return [root.base_uri.split(":")[0].upper()]

def headers():
Expand Down
28 changes: 28 additions & 0 deletions tests/data/examples/protocols.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#%RAML 0.8
title: Spotify Web API Demo - Simple Tree
version: v1
baseUri: https://api.spotify.com/{version}
mediaType: application/json
/tracks:
displayName: several-tracks
protocols: ["HTTP"]
get:
description: |
[Get Several Tracks](https://developer.spotify.com/web-api/get-several-tracks/)
queryParameters:
ids:
displayName: Spotify Track IDs
type: string
description: A comma-separated list of IDs
required: true
example: '7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ,2takcwOaAZWiXQijPHIx7B'
/{id}:
displayName: track
uriParameters:
id:
type: string
displayName: Spotify Track ID
example: 1zHlj4dQ8ZAtrayhuDDmkY
get:
description: |
[Get a Track](https://developer.spotify.com/web-api/get-track/)
31 changes: 30 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,10 @@ def test_resource_properties(resources):
assert resources[1].parent.name == "/widgets"
assert resources[1].path == "/widgets/{id}"

abs_uri = "https://{subdomain}.example.com/v1/{communityPath}/widgets/{id}"
abs_uri = "http://{subdomain}.example.com/v1/{communityPath}/widgets/{id}"
assert resources[1].absolute_uri == abs_uri
assert resources[1].media_type == "application/xml"
assert resources[1].protocols == ["HTTP"]

assert resources[2].is_ == ["paged"]
assert resources[2].media_type == "application/xml"
Expand Down Expand Up @@ -1071,6 +1072,34 @@ def test_resource_inherited_no_overwrite(inherited_resources):
assert second_resp.body[0].example == example


@pytest.fixture(scope="session")
def resource_protocol():
raml_file = os.path.join(EXAMPLES, "protocols.raml")
loaded_raml = load_file(raml_file)
config = setup_config(EXAMPLES + "test-config.ini")
config['validate'] = False
return pw.parse_raml(loaded_raml, config)


def test_overwrite_protocol(resource_protocol):
# if a resource explicitly defines a protocol, *that*
# should be reflected in the absolute URI
api = resource_protocol
assert api.protocols == ["HTTPS"]
assert api.base_uri == "https://api.spotify.com/v1"

res = api.resources
assert len(res) == 2

first = res[0]
second = res[1]

assert first.display_name == "several-tracks"
assert first.protocols == ["HTTP"]
assert second.display_name == "track"
assert second.protocols == ["HTTP"]


#####
# Test Includes parsing
#####
Expand Down

0 comments on commit 5558c95

Please sign in to comment.