|
| 1 | +from splitapiclient.resources.flag_set import FlagSet |
| 2 | +from splitapiclient.util.exceptions import HTTPResponseError, \ |
| 3 | + UnknownApiClientError |
| 4 | +from splitapiclient.util.logger import LOGGER |
| 5 | +from splitapiclient.util.helpers import as_dict |
| 6 | + |
| 7 | + |
| 8 | +class FlagSetMicroClient: |
| 9 | + ''' |
| 10 | + ''' |
| 11 | + _endpoint = { |
| 12 | + 'list_initial': { |
| 13 | + 'method': 'GET', |
| 14 | + 'url_template': 'flag-sets?workspace_id={workspace_id}&limit=50', |
| 15 | + 'headers': [{ |
| 16 | + 'name': 'Authorization', |
| 17 | + 'template': 'Bearer {value}', |
| 18 | + 'required': True, |
| 19 | + }], |
| 20 | + 'query_string': [], |
| 21 | + 'response': True, |
| 22 | + }, |
| 23 | + 'list_next': { |
| 24 | + 'method': 'GET', |
| 25 | + 'url_template': 'flag-sets?workspace_id={workspace_id}&after={after}&limit=50', |
| 26 | + 'headers': [{ |
| 27 | + 'name': 'Authorization', |
| 28 | + 'template': 'Bearer {value}', |
| 29 | + 'required': True, |
| 30 | + }], |
| 31 | + 'query_string': [], |
| 32 | + 'response': True, |
| 33 | + }, |
| 34 | + 'get': { |
| 35 | + 'method': 'GET', |
| 36 | + 'url_template': 'flag-sets/{flag_set_id}', |
| 37 | + 'headers': [{ |
| 38 | + 'name': 'Authorization', |
| 39 | + 'template': 'Bearer {value}', |
| 40 | + 'required': True, |
| 41 | + }], |
| 42 | + 'query_string': [], |
| 43 | + 'response': True, |
| 44 | + }, |
| 45 | + 'create': { |
| 46 | + 'method': 'POST', |
| 47 | + 'url_template': 'flag-sets', |
| 48 | + 'headers': [{ |
| 49 | + 'name': 'Authorization', |
| 50 | + 'template': 'Bearer {value}', |
| 51 | + 'required': True, |
| 52 | + }], |
| 53 | + 'query_string': [], |
| 54 | + 'response': True, |
| 55 | + }, |
| 56 | + 'delete': { |
| 57 | + 'method': 'DELETE', |
| 58 | + 'url_template': ('flag-sets/{flagSetId}'), |
| 59 | + 'headers': [{ |
| 60 | + 'name': 'Authorization', |
| 61 | + 'template': 'Bearer {value}', |
| 62 | + 'required': True, |
| 63 | + }], |
| 64 | + 'query_string': [], |
| 65 | + 'response': True, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + def __init__(self, http_client): |
| 70 | + ''' |
| 71 | + Constructor |
| 72 | + ''' |
| 73 | + self._http_client = http_client |
| 74 | + |
| 75 | + def list(self, workspace_id): |
| 76 | + ''' |
| 77 | + Returns a list of flag_sets objects. |
| 78 | +
|
| 79 | + :returns: list of flag_sets objects |
| 80 | + :rtype: list(flag_sets) |
| 81 | + ''' |
| 82 | + final_list = [] |
| 83 | + afterMarker = 0 |
| 84 | + while True: |
| 85 | + if afterMarker==0: |
| 86 | + response = self._http_client.make_request( |
| 87 | + self._endpoint['list_initial'], |
| 88 | + workspace_id = workspace_id |
| 89 | + ) |
| 90 | + else: |
| 91 | + response = self._http_client.make_request( |
| 92 | + self._endpoint['list_next'], |
| 93 | + workspace_id = workspace_id, |
| 94 | + after = afterMarker |
| 95 | + ) |
| 96 | + for item in response['data']: |
| 97 | + final_list.append(as_dict(item)) |
| 98 | + if response['nextMarker'] is None: |
| 99 | + break |
| 100 | + else: |
| 101 | + afterMarker = response['nextMarker'] |
| 102 | + continue |
| 103 | + return [FlagSet(item, workspace_id, self._http_client) for item in final_list] |
| 104 | + |
| 105 | + def find(self, flag_set_name=None, workspace_id=None): |
| 106 | + ''' |
| 107 | + Search for flag_set in list of flag_sets objects. |
| 108 | +
|
| 109 | + :returns: flag_set object |
| 110 | + :rtype: flag_set |
| 111 | + ''' |
| 112 | + |
| 113 | + for item in self.list(workspace_id=workspace_id): |
| 114 | + if item.name==flag_set_name: |
| 115 | + return item |
| 116 | + LOGGER.error("flag_set Name does not exist") |
| 117 | + return None |
| 118 | + |
| 119 | + def get(self, flag_set_id=None): |
| 120 | + ''' |
| 121 | + get a flag_set |
| 122 | +
|
| 123 | + :param flag_set_id: flag_set id |
| 124 | + :returns: flag_set |
| 125 | + :rtype: flag_set |
| 126 | + ''' |
| 127 | + |
| 128 | + response = self._http_client.make_request( |
| 129 | + self._endpoint['get'], |
| 130 | + flag_set_id=flag_set_id |
| 131 | + ) |
| 132 | + return FlagSet(data=response, client=self._http_client) |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + def add(self, flag_set, workspace_id): |
| 138 | + ''' |
| 139 | + add a flag_set |
| 140 | +
|
| 141 | + :param flag_set: flag_set instance |
| 142 | + :returns: newly created flag_set |
| 143 | + :rtype: flag_set |
| 144 | + ''' |
| 145 | + data = as_dict(flag_set) |
| 146 | + data['workspace'] = { |
| 147 | + 'id': workspace_id, |
| 148 | + 'type': 'WORKSPACE' |
| 149 | + } |
| 150 | + response = self._http_client.make_request( |
| 151 | + self._endpoint['create'], |
| 152 | + body=data |
| 153 | + ) |
| 154 | + return FlagSet(response, workspace_id, self._http_client) |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + def delete(self, flag_set_id ): |
| 159 | + ''' |
| 160 | + delete a flag_set |
| 161 | +
|
| 162 | + :param flag_set id: |
| 163 | +
|
| 164 | + :returns: |
| 165 | + :rtype: True if successful |
| 166 | + ''' |
| 167 | + response = self._http_client.make_request( |
| 168 | + self._endpoint['delete'], |
| 169 | + flagSetId =flag_set_id, |
| 170 | + ) |
| 171 | + return response |
0 commit comments