|
| 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()) |
0 commit comments