Skip to content

Commit b504b24

Browse files
committed
Merge pull request #48 from spotify/issue44
Fix absolute_uri and explicit protocol definition
2 parents 6e50336 + 5558c95 commit b504b24

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

ramlfications/parser.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,11 @@ def get_resource(attribute):
884884
"""Returns ``attribute`` defined at the resource level, or ``None``."""
885885
return raw_data.get(attribute, {})
886886

887+
def get_parent(attribute):
888+
if parent:
889+
return getattr(parent, attribute, {})
890+
return {}
891+
887892
def get_resource_type(attribute):
888893
"""Returns ``attribute`` defined in the resource type, or ``None``."""
889894
if type_() and root.resource_types:
@@ -967,14 +972,31 @@ def path():
967972

968973
def absolute_uri():
969974
"""Set resource's absolute URI path."""
970-
return root.base_uri + path()
975+
uri = root.base_uri + path()
976+
proto = protocols()
977+
if proto:
978+
uri = uri.split("://")
979+
if len(uri) == 2:
980+
uri = uri[1]
981+
if root.protocols:
982+
_proto = list(set(root.protocols) & set(proto))
983+
# if resource protocols and root protocols share a protocol
984+
# then use that one
985+
if _proto:
986+
uri = _proto[0].lower() + "://" + uri
987+
# if no shared protocols, use the first of the resource
988+
# protocols
989+
else:
990+
uri = proto[0].lower() + "://" + uri
991+
return uri
971992

972993
def protocols():
973994
"""Set resource's supported protocols."""
974995
trait_protocols = get_trait("protocols")
975996
r_type_protocols = get_resource_type("protocols")
976997
m_protocols = get_method("protocols")
977998
r_protocols = get_resource("protocols")
999+
parent = get_parent("protocols")
9781000
if m_protocols:
9791001
return m_protocols
9801002
elif r_type_protocols:
@@ -983,6 +1005,8 @@ def protocols():
9831005
return trait_protocols
9841006
elif r_protocols:
9851007
return r_protocols
1008+
elif parent:
1009+
return parent
9861010
return [root.base_uri.split(":")[0].upper()]
9871011

9881012
def headers():

tests/data/examples/protocols.raml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#%RAML 0.8
2+
title: Spotify Web API Demo - Simple Tree
3+
version: v1
4+
baseUri: https://api.spotify.com/{version}
5+
mediaType: application/json
6+
/tracks:
7+
displayName: several-tracks
8+
protocols: ["HTTP"]
9+
get:
10+
description: |
11+
[Get Several Tracks](https://developer.spotify.com/web-api/get-several-tracks/)
12+
queryParameters:
13+
ids:
14+
displayName: Spotify Track IDs
15+
type: string
16+
description: A comma-separated list of IDs
17+
required: true
18+
example: '7ouMYWpwJ422jRcDASZB7P,4VqPOruhp5EdPBeR92t6lQ,2takcwOaAZWiXQijPHIx7B'
19+
/{id}:
20+
displayName: track
21+
uriParameters:
22+
id:
23+
type: string
24+
displayName: Spotify Track ID
25+
example: 1zHlj4dQ8ZAtrayhuDDmkY
26+
get:
27+
description: |
28+
[Get a Track](https://developer.spotify.com/web-api/get-track/)

tests/test_parser.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,10 @@ def test_resource_properties(resources):
612612
assert resources[1].parent.name == "/widgets"
613613
assert resources[1].path == "/widgets/{id}"
614614

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

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

10731074

1075+
@pytest.fixture(scope="session")
1076+
def resource_protocol():
1077+
raml_file = os.path.join(EXAMPLES, "protocols.raml")
1078+
loaded_raml = load_file(raml_file)
1079+
config = setup_config(EXAMPLES + "test-config.ini")
1080+
config['validate'] = False
1081+
return pw.parse_raml(loaded_raml, config)
1082+
1083+
1084+
def test_overwrite_protocol(resource_protocol):
1085+
# if a resource explicitly defines a protocol, *that*
1086+
# should be reflected in the absolute URI
1087+
api = resource_protocol
1088+
assert api.protocols == ["HTTPS"]
1089+
assert api.base_uri == "https://api.spotify.com/v1"
1090+
1091+
res = api.resources
1092+
assert len(res) == 2
1093+
1094+
first = res[0]
1095+
second = res[1]
1096+
1097+
assert first.display_name == "several-tracks"
1098+
assert first.protocols == ["HTTP"]
1099+
assert second.display_name == "track"
1100+
assert second.protocols == ["HTTP"]
1101+
1102+
10741103
#####
10751104
# Test Includes parsing
10761105
#####

0 commit comments

Comments
 (0)