Skip to content

Commit

Permalink
Set user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
kulinacs committed Jun 28, 2019
1 parent 8e76a45 commit f6bd9ea
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Hack the Box
============

A Python wrapper to interact with hackthebox.eu
An unofficial Python wrapper to interact with hackthebox.eu
47 changes: 23 additions & 24 deletions htb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
import requests

__version__ = '1.0.0'

class HTBAPIError(Exception):
"""Raised when API fails"""
pass
Expand All @@ -12,12 +14,14 @@ class HTB:
Hack the Box API Wrapper
:attr api_key: API Key used for authenticated queries
:attr user_agent: The User-Agent to be used with all requests
"""

BASE_URL = 'https://www.hackthebox.eu/api'

def __init__(self, api_key):
def __init__(self, api_key, user_agent='Python HTB Client/{}'.format(__version__)):
self.api_key = api_key
self.headers = {'User-Agent': user_agent}

@staticmethod
def _validate_response(response):
Expand All @@ -31,27 +35,25 @@ def _validate_response(response):
raise HTBAPIError("success != 1")
return response

@classmethod
def _get(cls, path: str) -> dict:
def _get(self, path: str) -> dict:
"""
Helper function to get an API endpoint and validate the response
:params cls: the HTB class
:params self: the HTB object
:params path: the path to get including leading forward slash
:returns: the response dict from the endpoint
"""
return HTB._validate_response(requests.get(cls.BASE_URL + path).json())
return HTB._validate_response(requests.get(self.BASE_URL + path, headers=self.headers).json())

@classmethod
def _post(cls, path: str, data: dict = None) -> dict:
def _post(self, path: str, data: dict = None) -> dict:
"""
Helper function to get an API endpoint and validate the response
:params cls: the HTB class
:params self: the HTB object
:params path: the path to get including leading forward slash
:returns: the response dict from the endpoint
"""
return HTB._validate_response(requests.post(cls.BASE_URL + path, data=data).json())
return HTB._validate_response(requests.post(self.BASE_URL + path, data=data, headers=self.headers).json())

def _auth(self, path: str) -> str:
"""
Expand All @@ -63,18 +65,16 @@ def _auth(self, path: str) -> str:
"""
return "{}?api_token={}".format(path, self.api_key)

@classmethod
def global_stats(cls) -> dict:
def global_stats(self) -> dict:
"""
Returns current stats about Hack the Box
:params cls: the HTB class
:returns: global stats dict
"""
return cls._post('/stats/global')
return self._post('/stats/global')

@classmethod
def overview_stats(cls) -> dict:
def overview_stats(self) -> dict:
"""
Returns overview stats about Hack the Box
Expand All @@ -83,18 +83,17 @@ def overview_stats(cls) -> dict:
:params cls: the HTB class
:returns: overview stats dict
"""
return requests.get(cls.BASE_URL + '/stats/overview').json()
return requests.get(self.BASE_URL + '/stats/overview', headers=self.headers).json()

@classmethod
def daily_owns(cls, count: int = 30) -> dict:
def daily_owns(self, count: int = 30) -> dict:
"""
Returns the number of owns and total number of users after the last COUNT days
:params cls: the HTB class
:params count: the number of days to get data from
:returns: daily owns dict
"""
return cls._post('/stats/daily/owns/{}'.format(count))
return self._post('/stats/daily/owns/{}'.format(count))

def list_conversations(self) -> dict:
"""
Expand All @@ -105,7 +104,7 @@ def list_conversations(self) -> dict:
:params self: HTB object in use
:returns: conversations dict
"""
return requests.post(self.BASE_URL + self._auth('/conversations/list/')).json()
return requests.post(self.BASE_URL + self._auth('/conversations/list/'), headers=self.headers).json()

def vpn_freeslots(self) -> dict:
"""
Expand Down Expand Up @@ -134,7 +133,7 @@ def connection_status(self) -> dict:
:params self: HTB object in use
:returns: connection_status dict
"""
return requests.post(self.BASE_URL + self._auth('/users/htb/connection/status/')).json()
return requests.post(self.BASE_URL + self._auth('/users/htb/connection/status/'), headers=self.headers).json()

def fortress_connection_status(self) -> dict:
"""
Expand All @@ -145,7 +144,7 @@ def fortress_connection_status(self) -> dict:
:params self: HTB object in use
:returns: fortress_connection_status dict
"""
return requests.post(self.BASE_URL + self._auth('/users/htb/fortress/connection/status/')).json()
return requests.post(self.BASE_URL + self._auth('/users/htb/fortress/connection/status/'), headers=self.headers).json()

def switch_vpn(self, lab: str) -> dict:
"""
Expand All @@ -161,7 +160,7 @@ def switch_vpn(self, lab: str) -> dict:
if lab not in ("usfree", "eufree", "usvip", "euvip", "euvipbeta"):
raise HTBAPIError("invalid lab")
else:
return requests.post(self.BASE_URL + self._auth('/labs/switch/{}/'.format(lab))).json()
return requests.post(self.BASE_URL + self._auth('/labs/switch/{}/'.format(lab)), headers=self.headers).json()

def get_machines(self) -> dict:
"""
Expand All @@ -170,7 +169,7 @@ def get_machines(self) -> dict:
:params self: HTB object in use
:returns: machines dict
"""
return requests.get(self.BASE_URL + self._auth('/machines/get/all/')).json()
return requests.get(self.BASE_URL + self._auth('/machines/get/all/'), headers=self.headers).json()

def get_machine(self, mid: int) -> dict:
"""
Expand All @@ -180,7 +179,7 @@ def get_machine(self, mid: int) -> dict:
:params mid: Machine ID
:returns: machine dict
"""
return requests.get(self.BASE_URL + self._auth('/machines/get/{}/'.format(mid))).json()
return requests.get(self.BASE_URL + self._auth('/machines/get/{}/'.format(mid)), headers=self.headers).json()

def own_machine_user(self, mid: int, hsh: str, diff: int) -> bool:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='htb',
version='0.4.0',
version='1.0.0',

description='Hack the Box API',
long_description=long_description,
Expand Down
12 changes: 9 additions & 3 deletions tests/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ def test___init__():
assert client.api_key == api_key

def test_global_stats():
assert HTB.global_stats()['success'] == '1'
api_key = 'test'
client = HTB(api_key)
assert client.global_stats()['success'] == '1'

def test_overview_stats():
assert len(HTB.overview_stats()) == 3
api_key = 'test'
client = HTB(api_key)
assert len(client.overview_stats()) == 3

def test_daily_owns():
assert HTB.daily_owns()['success'] == '1'
api_key = 'test'
client = HTB(api_key)
assert client.daily_owns()['success'] == '1'

0 comments on commit f6bd9ea

Please sign in to comment.