コード例 #1
0
    def add_printer(
            self, printer_name: str, url: str,
            user: Optional[str]) -> Tuple[AddPrinterResult, Optional[Dict]]:
        octorest_client = None

        try:
            octorest_client = OctoRest(url=url)
        except TypeError:
            return (AddPrinterResult.BAD_URL, None)

        (result, api_key) = (None, None)

        try:
            (result,
             api_key) = octorest_client.try_get_api_key('MahaPrinting', user)
        except requests.exceptions.ConnectionError:
            return (AddPrinterResult.BAD_URL, None)

        if result == WorkflowAppKeyRequestResult.WORKFLOW_UNSUPPORTED:
            return (AddPrinterResult.WORKFLOW_UNSUPPORTED, None)
        if result == WorkflowAppKeyRequestResult.TIMED_OUT:
            return (AddPrinterResult.API_KEY_REQUEST_TIMED_OUT, None)
        if result == WorkflowAppKeyRequestResult.NOPE:
            return (AddPrinterResult.API_KEY_REQUEST_DENIED, None)
        if result == WorkflowAppKeyRequestResult.GRANTED:
            printer_info = self._add_printer_no_url_validation(
                printer_name, url, api_key)

            return (AddPrinterResult.SUCCESS, printer_info)

        raise NotImplementedError(
            "An unsupported WorkflowAppKeyRequestResult value was encountered: "
            + result.name)
コード例 #2
0
def main():
    url = "http://octopi.local"
    user = "******"
    
    client = None

    try:
        client = OctoRest(url=url)
    except TypeError:
        raise NotImplementedError() # Decide what should happen now

    (result, api_key) = (None, None)

    try:
        (result, api_key) = client.try_get_api_key('my-app', user)
    except ConnectionError:
        raise NotImplementedError() # Decide what should happen now. Suggestion - tell the user the OctoPrint server is unreachable and that he should check the URL entered

    if result == WorkflowAppKeyRequestResult.WORKFLOW_UNSUPPORTED:
        raise NotImplementedError() # Decide what should happen now. Suggestion - fall back to asking the user to manually enter a valid API key.
    elif result == WorkflowAppKeyRequestResult.TIMED_OUT: # The user took too long to approve the API key request
        raise NotImplementedError() # Decide what should happen now
    elif result == WorkflowAppKeyRequestResult.NOPE: # The request has been denied
        raise NotImplementedError() # Decide what should happen now
    elif result == WorkflowAppKeyRequestResult.GRANTED:
        client.load_api_key(api_key) # You have to load the API key before sending any requests to the OctoPrint server
        pass # At this point you can use the client for whatever you wish
    
    raise NotImplementedError() # Decide what should happen now