Coretex
coretex.networking.network_object.NetworkObject Class Reference
Inheritance diagram for coretex.networking.network_object.NetworkObject:
coretex.codable.codable.Codable

Public Member Functions

bool __eq__ (self, object __o)
 
int __hash__ (self)
 
bool refresh (self, Optional[Dict[str, Any]] jsonObject=None)
 
bool update (self, Dict[str, Any] parameters)
 
bool delete (self)
 
Optional[Self] create (cls, Dict[str, Any] parameters)
 
List[Self] fetchAll (cls, Optional[List[str]] queryParameters=None, int pageSize=DEFAULT_PAGE_SIZE)
 
Optional[Self] fetchById (cls, int objectId, Optional[List[str]] queryParameters=None)
 
- Public Member Functions inherited from coretex.codable.codable.Codable
Dict[str, Any] encode (self)
 
None onDecode (self)
 
Self decode (cls, Dict[str, Any] encodedObject)
 

Detailed Description

    Base class for every Coretex.ai entity representation in Python

    Properties
    ----------
    id : int
        id of object
    isDeleted : bool
        boolean value that represents if object will be shown or not

Definition at line 31 of file network_object.py.

Member Function Documentation

◆ __eq__()

bool coretex.networking.network_object.NetworkObject.__eq__ (   self,
object  __o 
)
    Checks if the NetworkObjects which have id property
    defined are equal

    Parameter
    ---------
    __o : object
object to which we are comparing self

    Returns
    -------
    bool -> True if ids are present and equal, False in any other case

Definition at line 64 of file network_object.py.

64  def __eq__(self, __o: object) -> bool:
65  """
66  Checks if the NetworkObjects which have id property
67  defined are equal
68 
69  Parameter
70  ---------
71  __o : object
72  object to which we are comparing self
73 
74  Returns
75  -------
76  bool -> True if ids are present and equal, False in any other case
77  """
78 
79  # check if object parent class matches
80  if isinstance(__o, NetworkObject):
81  return self.id == __o.id
82 
83  return NotImplemented
84 

◆ __hash__()

int coretex.networking.network_object.NetworkObject.__hash__ (   self)
    Calculates hash of the object in a non-randomized manner

    Returns
    -------
    int -> hash of all the items defined on the self.__dict__ object

Definition at line 85 of file network_object.py.

85  def __hash__(self) -> int:
86  """
87  Calculates hash of the object in a non-randomized manner
88 
89  Returns
90  -------
91  int -> hash of all the items defined on the self.__dict__ object
92  """
93 
94  return hash(tuple(sorted(self.__dict__.items())))
95 

◆ create()

Optional[Self] coretex.networking.network_object.NetworkObject.create (   cls,
Dict[str, Any]  parameters 
)
    Creates the entity linked to this class on Coretex backend

    Parameters
    ----------
    parameters : Dict[str, Any]
parameters which will be sent as request body

    Returns
    -------
    Optional[Self] -> created object if request was successful, None otherwise

Definition at line 167 of file network_object.py.

167  def create(cls, parameters: Dict[str, Any]) -> Optional[Self]:
168  """
169  Creates the entity linked to this class on Coretex backend
170 
171  Parameters
172  ----------
173  parameters : Dict[str, Any]
174  parameters which will be sent as request body
175 
176  Returns
177  -------
178  Optional[Self] -> created object if request was successful, None otherwise
179  """
180 
181  response = networkManager.genericJSONRequest(
182  endpoint=cls._endpoint(),
183  requestType=RequestType.post,
184  parameters=parameters
185  )
186 
187  if response.hasFailed():
188  return None
189 
190  return cls.decode(response.json)
191 

◆ delete()

bool coretex.networking.network_object.NetworkObject.delete (   self)
    Sends a DELETE request to Coretex backend

    Returns
    -------
    bool -> True if request was successful, False otherwise

Definition at line 150 of file network_object.py.

150  def delete(self) -> bool:
151  """
152  Sends a DELETE request to Coretex backend
153 
154  Returns
155  -------
156  bool -> True if request was successful, False otherwise
157  """
158 
159  if self.isDeleted:
160  return False
161 
162  return not networkManager.genericDelete(
163  f"{self.__class__._endpoint()}/{self.id}"
164  ).hasFailed()
165 

◆ fetchAll()

List[Self] coretex.networking.network_object.NetworkObject.fetchAll (   cls,
Optional[List[str]]   queryParameters = None,
int   pageSize = DEFAULT_PAGE_SIZE 
)
    Fetches all entities from Coretex backend which match
    the given predicate

    Parameters
    ----------
    queryParameters : Optional[List[str]]
query parameters (predicate) which will be appended to URL (Not required)
    pageSize : int
Specified page size (entity count) which will be fetched, default = 100

    Returns
    -------
    List[Self] -> list of all fetched entities

Definition at line 193 of file network_object.py.

193  def fetchAll(cls, queryParameters: Optional[List[str]] = None, pageSize: int = DEFAULT_PAGE_SIZE) -> List[Self]:
194  """
195  Fetches all entities from Coretex backend which match
196  the given predicate
197 
198  Parameters
199  ----------
200  queryParameters : Optional[List[str]]
201  query parameters (predicate) which will be appended to URL (Not required)
202  pageSize : int
203  Specified page size (entity count) which will be fetched, default = 100
204 
205  Returns
206  -------
207  List[Self] -> list of all fetched entities
208  """
209 
210  if queryParameters is None:
211  queryParameters = [f"page_size={pageSize}"]
212  else:
213  queryParameters.append(f"page_size={pageSize}")
214 
215  formattedQueryParameters = "&".join(queryParameters)
216  endpoint = f"{cls._endpoint()}?{formattedQueryParameters}"
217 
218  response = networkManager.genericJSONRequest(endpoint, RequestType.get)
219 
220  if response.hasFailed():
221  return []
222 
223  objects: List[Self] = []
224 
225  for obj in response.json:
226  objects.append(cls.decode(obj))
227 
228  return objects
229 

◆ fetchById()

Optional[Self] coretex.networking.network_object.NetworkObject.fetchById (   cls,
int  objectId,
Optional[List[str]]   queryParameters = None 
)
    Fetches a single entity with the matching id

    Parameters
    ----------
    objectId : int
id of the object which is fetched
    queryParameters : Optional[List[str]]
query parameters (predicate) which will be appended to URL (Not required)

    Returns
    -------
    Optional[Self] -> fetched object if request was successful, None otherwise

Definition at line 231 of file network_object.py.

231  def fetchById(cls, objectId: int, queryParameters: Optional[List[str]] = None) -> Optional[Self]:
232  """
233  Fetches a single entity with the matching id
234 
235  Parameters
236  ----------
237  objectId : int
238  id of the object which is fetched
239  queryParameters : Optional[List[str]]
240  query parameters (predicate) which will be appended to URL (Not required)
241 
242  Returns
243  -------
244  Optional[Self] -> fetched object if request was successful, None otherwise
245  """
246 
247  endpoint = f"{cls._endpoint()}/{objectId}"
248  if queryParameters is not None:
249  formattedQueryParameters = "&".join(queryParameters)
250  endpoint = f"{endpoint}?{formattedQueryParameters}"
251 
252  response = networkManager.genericJSONRequest(endpoint, RequestType.get)
253  if response.hasFailed():
254  return None
255 
256  return cls.decode(response.json)

◆ refresh()

bool coretex.networking.network_object.NetworkObject.refresh (   self,
Optional[Dict[str, Any]]   jsonObject = None 
)
    Updates objects fields to a provided value if set, otherwise
    fetches the object from the API and updates the values
    using the fetched object

    Parameters
    ----------
    jsonObject : Optional[Dict[str, Any]]
A serialized json object to which the values should be updated, if provided

    Returns
    -------
    bool -> True if the update was successful, False otherwise

Definition at line 96 of file network_object.py.

96  def refresh(self, jsonObject: Optional[Dict[str, Any]] = None) -> bool:
97  """
98  Updates objects fields to a provided value if set, otherwise
99  fetches the object from the API and updates the values
100  using the fetched object
101 
102  Parameters
103  ----------
104  jsonObject : Optional[Dict[str, Any]]
105  A serialized json object to which the values should be updated, if provided
106 
107  Returns
108  -------
109  bool -> True if the update was successful, False otherwise
110  """
111 
112  # Update from json if it exists
113  if jsonObject is not None:
114  self._updateFields(jsonObject)
115  return True
116 
117  # Fetch from server otherwise
118  obj = self.__class__.fetchById(self.id)
119  if obj is None:
120  return False
121 
122  for key, value in obj.__dict__.items():
123  self.__dict__[key] = value
124 
125  return True
126 

◆ update()

bool coretex.networking.network_object.NetworkObject.update (   self,
Dict[str, Any]  parameters 
)
    Sends a PUT request to Coretex backend

    Parameters
    ----------
    parameters : Dict[str, Any]
parameters which will be sent as request body

    Returns
    -------
    bool -> True if request was successful, False otherwise

Definition at line 127 of file network_object.py.

127  def update(self, parameters: Dict[str, Any]) -> bool:
128  """
129  Sends a PUT request to Coretex backend
130 
131  Parameters
132  ----------
133  parameters : Dict[str, Any]
134  parameters which will be sent as request body
135 
136  Returns
137  -------
138  bool -> True if request was successful, False otherwise
139  """
140 
141  if self.isDeleted:
142  return False
143 
144  return not networkManager.genericJSONRequest(
145  endpoint = f"{self.__class__._endpoint()}/{self.id}",
146  requestType = RequestType.put,
147  parameters = parameters
148  ).hasFailed()
149 

The documentation for this class was generated from the following file: