Skip to content

Commit 9bec3f8

Browse files
committed
Abstracting CRUD operations to a base APIEndpoint class
1 parent bc63ca1 commit 9bec3f8

File tree

9 files changed

+117
-40
lines changed

9 files changed

+117
-40
lines changed

gophish/api/__init__.py

Whitespace-only changes.

gophish/api/api.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import requests
2+
3+
'''
4+
api.py
5+
6+
Base API endpoint class that abstracts basic CRUD operations.
7+
'''
8+
9+
class APIEndpoint:
10+
"""
11+
Represents an API endpoint for Gophish, containing common patterns
12+
for CRUD operations.
13+
"""
14+
def __init__(self, api, endpoint=None):
15+
""" Creates an instance of the APIEndpoint class.
16+
17+
Args:
18+
api - Gophish.client - The authenticated REST client
19+
endpoint - str - The URL path to the resource endpoint
20+
"""
21+
self.api = api
22+
self.endpoint = endpoint
23+
24+
def get(self, cls, resource_id=None):
25+
""" Gets the details for one or more resources by ID
26+
27+
Args:
28+
cls - gophish.models.Model - The resource class
29+
resource_id - str - The endpoint (URL path) for the resource
30+
31+
Returns:
32+
One or more instances of cls parsed from the returned JSON
33+
"""
34+
35+
endpoint = self.endpoint
36+
37+
if resource_id:
38+
endpoint = '{}/{}'.format(endpoint, resource_id)
39+
40+
response = self.api.execute("GET", endpoint)
41+
if not response.ok:
42+
return Error.parse(response.json())
43+
44+
if resource_id:
45+
return cls.parse(response.json())
46+
47+
return [cls.parse(resource) for resource in response.json()]
48+
49+
def post(self, cls, resource):
50+
""" Creates a new instance of the resource.
51+
52+
Args:
53+
cls - gophish.models.Model - The resource class
54+
55+
"""
56+
response = self.api.execute("POST", self.endpoint, json=json.dumps(resource))
57+
58+
if not response.ok:
59+
return Error.parse(response.json())
60+
61+
return cls.parse(response.json())
62+
63+
def put(self, cls, resource):
64+
""" Edits an existing resource
65+
66+
Args:
67+
cls - gophish.models.Model - The resource class
68+
resource - gophish.models.Model - The resource instance
69+
"""
70+
71+
endpoint = self.endpoint
72+
73+
if resource.id:
74+
endpoint = '{}/{}'.format(endpoint, resource.id)
75+
76+
response = self.api.execute("PUT", endpoint, json=resource.as_json())
77+
78+
if not respose.ok:
79+
return Error.parse(response.json())
80+
81+
return cls.parse(response.json())
82+
83+
def delete(self, cls, resource_id):
84+
""" Deletes an existing resource
85+
86+
Args:
87+
cls - gophish.models.Model - The resource class
88+
resource_id - int - The resource ID to be deleted
89+
"""
90+
91+
endpoint = '{}/{}'.format(self.endpoint, resource_id)
92+
93+
response = self.api.execute("DELETE", endpoint)
94+
95+
if not response.ok:
96+
return Error.parse(response.json())
97+
98+
return cls.parse(response.json())

gophish/api/campaigns.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import json
2+
3+
from gophish.error import Error
4+
from gophish.models import Campaign
5+
from gophish.api import APIEndpoint
6+
7+
8+
class API(APIEndpoint):
9+
def __init__(self, api, endpoint='/campaigns'):
10+
""" Creates a new instance of the campaigns API """
11+
super(API, self).__init__(api, endpoint=endpoint)
12+
13+
def summary(campaign_id=None):
14+
""" Returns the summary of one or more campaigns. """
15+
raise NotImplementedError
16+
17+
def results(campaign_id):
18+
""" Returns just the results for a given campaign """
19+
raise NotImplementedError
File renamed without changes.
File renamed without changes.
File renamed without changes.

gophish/campaigns.py

Lines changed: 0 additions & 40 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)