This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
forked from akamai/httpie-edgegrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpie_edgegrid.py
56 lines (44 loc) · 1.7 KB
/
httpie_edgegrid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
edgegrid auth plugin for HTTPie.
"""
import sys
from httpie.plugins import AuthPlugin
from akamai.edgegrid import EdgeGridAuth, EdgeRc
import os
__version__ = '1.0.0'
__author__ = 'Kirsten Hunter'
__licence__ = 'Apache 2.0'
class HTTPieEdgeGridAuth(EdgeGridAuth):
def __init__(self, hostname, *args, **kwargs):
self.__hostname = hostname
super(HTTPieEdgeGridAuth, self).__init__(*args, **kwargs)
def __call__(self, r):
r = super(HTTPieEdgeGridAuth, self).__call__(r)
r.url = r.url.replace("http:","https:")
r.url = r.url.replace("localhost/",self.__hostname)
return super(HTTPieEdgeGridAuth, self).__call__(r)
class EdgeGridPlugin(AuthPlugin):
name = 'EdgeGrid auth'
auth_type = 'edgegrid'
description = ''
def get_auth(self, username, password):
rc_path = os.path.expanduser("~/.edgerc")
rc = EdgeRc(rc_path)
if not rc.has_section(username):
err_msg = "\nERROR: No section named '%s' was found in your .edgerc file\n" % username
err_msg += "ERROR: Please generate credentials for the script functionality\n"
err_msg += "ERROR: and run 'python gen_edgerc.py %s' to generate the credential file\n"
sys.stderr.write(err_msg)
sys.exit(1)
host=rc.get(username, 'host')
host=host.replace("https://", "")
host= host.replace ("/","")
host += "/"
auth = HTTPieEdgeGridAuth(
hostname = host,
client_token=rc.get(username, 'client_token'),
client_secret=rc.get(username, 'client_secret'),
access_token=rc.get(username, 'access_token'),
max_body=rc.getint(username, 'max_body')
)
return auth