Repository: smicallef/spiderfoot
File: modules/sfp_abstractapi.py (Python)

Code before:

        return self.parseAPIResponse(res)

    def parseAPIResponse(self, res):
        if res['code'] == '204':
            self.sf.debug("No geolocation information for target")
            return None

        if res['code'] == "400":
            self.sf.error("Bad Request")
            self.errorState = True
            return None

        if res['code'] == "401":
            self.sf.error("Unauthorized. Invalid abstractapi API key")
            self.errorState = True
            return None

        if res['code'] == '422':
            self.sf.error("Usage quota reached. Insufficient API Credit")
            self.errorState = True
            return None

        if res['code'] == '429':
            self.sf.error("Too many requests")
            self.errorState = True
            return None

        if res['code'] == '500' or res['code'] == '503':
            self.sf.error("abstractapi service is unavailable")
            self.errorState = True
            return None

        if res['code'] != '200':
            self.sf.error("Failed to retrieve data from abstractapi")
            self.errorState = True
            return None

        if res['content'] is None:
            return None

        try:
            data = json.loads(res['content'])
        except Exception as e:
            self.sf.debug(f"Error processing JSON response: {e}")
            return None

        return data

    # Handle events sent to this module
    def handleEvent(self, event):

Review comment (bug):
Not sure this should cause `errorState` to be `True` since it indicates one throttled request, not that all future requests will fail (that's the only condition under which `errorState` should be `True`.

Code after:

        return self.parseAPIResponse(res)

    def parseAPIResponse(self, res):
        if res['code'] == '204':
            self.sf.debug("No geolocation information for target")
            return None

        if res['code'] == "400":
            self.sf.error("Bad Request")
            self.errorState = True
            return None

        if res['code'] == "401":
            self.sf.error("Unauthorized. Invalid abstractapi API key")
            self.errorState = True
            return None

        if res['code'] == '422':
            self.sf.error("Usage quota reached. Insufficient API Credit")
            self.errorState = True
            return None

        if res['code'] == '429':
            self.sf.error("Too many requests")
            return None

        if res['code'] == '500' or res['code'] == '503':
            self.sf.error("abstractapi service is unavailable")
            self.errorState = True
            return None

        if res['code'] != '200':
            self.sf.error("Failed to retrieve data from abstractapi")
            self.errorState = True
            return None

        if res['content'] is None:
            return None

        try:
            data = json.loads(res['content'])
        except Exception as e:
            self.sf.debug(f"Error processing JSON response: {e}")
            return None

        return data

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType