Skip to content

Commit

Permalink
Merge pull request #55 from hivesolutions/joamag/basic_auth
Browse files Browse the repository at this point in the history
Support for basic_auth method
  • Loading branch information
joamag committed Jun 10, 2022
2 parents f15112e + d98e894 commit a121f20
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

*
* Support for `basic_auth()` method that generates a basic authorization string

### Changed

Expand Down
2 changes: 1 addition & 1 deletion src/appier/__init__.py
Expand Up @@ -109,7 +109,7 @@
from .geo import GeoResolver
from .git import Git
from .graph import Graph
from .http import file_g, get_f, get, post, put, delete, HTTPResponse
from .http import file_g, get_f, get, post, put, delete, patch, basic_auth, HTTPResponse
from .log import MemoryHandler, BaseFormatter, ThreadFormatter, DummyLogger, reload_format, rotating_handler,\
smtp_handler, in_signature
from .meta import Ordered, Indexed
Expand Down
5 changes: 5 additions & 0 deletions src/appier/http.py
Expand Up @@ -240,6 +240,11 @@ def patch(
**kwargs
)

def basic_auth(username, password = None):
if not password: password = username
authorization = _authorization(username, password)
return "Basic %s" % authorization

def _try_auth(auth_callback, params, headers = None):
if not auth_callback: raise
if headers == None: headers = dict()
Expand Down
8 changes: 7 additions & 1 deletion src/appier/test/http.py
Expand Up @@ -48,7 +48,13 @@ def setUp(self):
unittest.TestCase.setUp(self)
self.httpbin = appier.conf("HTTPBIN", "httpbin.org")

def test_parse_url(self):
def test_basic_auth(self):
result = appier.http.basic_auth("username", "password")

self.assertEqual(result, "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")
self.assertEqual(appier.legacy.is_string(result), True)

def test__parse_url(self):
url, scheme, host, authorization, params = appier.http._parse_url("http://hive.pt/")

self.assertEqual(url, "http://hive.pt:80/")
Expand Down

0 comments on commit a121f20

Please sign in to comment.