-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from njgheorghita/http-backend
Http backend
- Loading branch information
Showing
16 changed files
with
264 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from urllib import parse | ||
|
||
import requests | ||
|
||
from ethpm.backends.base import BaseURIBackend | ||
from ethpm.constants import RAW_GITHUB_AUTHORITY | ||
from ethpm.utils.uri import is_valid_github_uri | ||
from ethpm.validation import validate_uri_contents | ||
|
||
|
||
class GithubOverHTTPSBackend(BaseURIBackend): | ||
""" | ||
Base class for all URIs pointing to a content-addressed Github URI. | ||
""" | ||
|
||
def can_resolve_uri(self, uri: str) -> bool: | ||
return is_valid_github_uri(uri) | ||
|
||
def can_translate_uri(self, uri: str) -> bool: | ||
""" | ||
GithubOverHTTPSBackend uri's must resolve to a valid manifest, | ||
and cannot translate to another content-addressed URI. | ||
""" | ||
return False | ||
|
||
def fetch_uri_contents(self, uri: str) -> bytes: | ||
parsed_uri = parse.urlparse(uri) | ||
validation_hash = parsed_uri.fragment | ||
http_uri = f"{parsed_uri.scheme}://{parsed_uri.netloc}{parsed_uri.path}" | ||
response = requests.get(http_uri) | ||
response.raise_for_status() | ||
validate_uri_contents(response.content, validation_hash) | ||
return response.content | ||
|
||
@property | ||
def base_uri(self) -> str: | ||
return RAW_GITHUB_AUTHORITY |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from urllib import parse | ||
|
||
from eth_utils import is_text | ||
|
||
from ethpm.constants import INTERNET_SCHEMES, RAW_GITHUB_AUTHORITY | ||
|
||
|
||
def is_valid_github_uri(uri: str) -> bool: | ||
""" | ||
Return a bool indicating whether or not the URI is a valid Github URI. | ||
Valid Github URIs *must*: | ||
- Have 'http' or 'https' scheme | ||
- Have 'raw.githubusercontent.com' authority | ||
- Have any path (*should* include a commit hash in path) | ||
- Have ending fragment containing the keccak hash of the uri contents | ||
ex. 'https://raw.githubusercontent.com/user/repo/commit_hash/path/to/manifest.json#content_hash' | ||
""" | ||
if not is_text(uri): | ||
return False | ||
parse_result = parse.urlparse(uri) | ||
path = parse_result.path | ||
scheme = parse_result.scheme | ||
authority = parse_result.netloc | ||
content_hash = parse_result.fragment | ||
|
||
if not path or not scheme or not content_hash: | ||
return False | ||
|
||
if scheme not in INTERNET_SCHEMES: | ||
return False | ||
|
||
if authority != RAW_GITHUB_AUTHORITY: | ||
return False | ||
return True |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
|
||
from ethpm import Package | ||
from ethpm.backends.http import GithubOverHTTPSBackend | ||
from ethpm.constants import RAW_GITHUB_AUTHORITY | ||
from ethpm.exceptions import ValidationError | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"uri", | ||
( | ||
"https://raw.githubusercontent.com/ethpm/ethpm-spec/3945c47dedb04930ee12c0281494a1b5bdd692a0/examples/owned/1.0.0.json#01cbc2a69a9f86e9d9e7b87475e2ba2619404dc8d6ee3cb3a8acf3176c2cace1", # noqa: E501 | ||
"https://raw.githubusercontent.com/ethpm/ethpm-spec/3945c47dedb04930ee12c0281494a1b5bdd692a0/examples/owned/1.0.0.json#0x01cbc2a69a9f86e9d9e7b87475e2ba2619404dc8d6ee3cb3a8acf3176c2cace1", # noqa: E501 | ||
), | ||
) | ||
def test_github_over_https_backend_fetch_uri_contents(uri, owned_contract, w3): | ||
# these tests may occassionally fail CI as a result of their network requests | ||
backend = GithubOverHTTPSBackend() | ||
assert backend.base_uri == RAW_GITHUB_AUTHORITY | ||
# integration with Package.from_uri | ||
owned_package = Package.from_uri(uri, w3) | ||
assert owned_package.name == "owned" | ||
|
||
|
||
def test_github_over_https_backend_raises_error_with_invalid_content_hash(w3): | ||
invalid_uri = "https://raw.githubusercontent.com/ethpm/ethpm-spec/3945c47dedb04930ee12c0281494a1b5bdd692a0/examples/owned/1.0.0.json#01cbc2a69a9f86e9d9e7b87475e2ba2619404dc8d6ee3cb3a8acf3176c2ca111" # noqa: E501 | ||
with pytest.raises(ValidationError): | ||
Package.from_uri(invalid_uri, w3) |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.