Page MenuHomePhabricator

[backend][get_tools.py] Code improvements
Closed, ResolvedPublic

Description

  • The code is using a hard-coded URL for the tool API endpoint. Storing the API endpoint in a config file or env variable would make it easier to switch between test and production endpoints.
  • It's good practice to include error handling and logging in production code. E.g. consider using try-except blocks to catch exceptions and log them (and other info useful for debugging) to stout.
  • You can use the 'requests' library's built-in status code checking, which raises an exception on error status codes. For example, you could replace if response.status_code == 200: with response.raise_for_status().
  • Consider creating a Class for dealing with all logic related to communicating with Toolhub, something like
class ToolhubClient:

    def __init__(self, endpoint, user_info):
        self.endpoint = endpoint
        self.user_info = user_info
        self.headers = {'User-Agent': f'Toolhunt API - {user_info}'}

    def get_all(self):
        """Get data on all Toolhub tools"""
        ...

    def get(self, tool_name):
        """Get data on a single tool"""
        ...

    def put(self, tool_data):
        """Edit a tool"""
        ...

Event Timeline

Regarding environmental variables, prod vs. dev environments, etc.: right now I've put a lot of this sort of thing in the config file, inside the Base class:

  TOOLHUB_ACCESS_TOKEN_URL = os.getenv(
    "TOOLHUB_ACCESS_TOKEN_URL",
    default="https://toolhub-demo.wmcloud.org/o/token/"
)

Would it be better practice to set the value of TOOLHUB_ACCESS_TOKEN_URL twice, once in the DevelopmentConfig class (TOOLHUB_ACCESS_TOKEN_URL = "https://toolhub-demo.wmcloud.org/o/token/") and again in ProductionConfig? (The same would apply, of course, for the Toolhub API endpoint.)

This seems like the way to handle it, as it would make it easier to switch between prod and dev environments, but DRY has been hammered into my head so many times that I feel compelled to ask permission, hah.

NicoleLBee changed the task status from Open to In Progress.Feb 21 2023, 6:40 AM
NicoleLBee claimed this task.

I've opened a draft PR to address this task: https://github.com/wikimedia/toolhunt/pull/31

I've opened the PR for review. I have yet to add proper error handling, which is a particular issue with the current PUT route as there's a lot that could go wrong, but I have done some significant restructuring already and feel that the PR is already getting pretty unwieldy.

I wasn't going to add more to this, but I couldn't stop myself from tossing in a basic randomizer to /api/tasks, so that a GET request won't just return the same set of 10. (It's a code improvement!)

Like so much of my work, this PR gradually spiraled out of control. Fortunately, it has now been merged. There's always more refactoring to be done, but the job is finished... for now.