Skip to content

Commit

Permalink
Remove unnecessary logic for specifying client version.
Browse files Browse the repository at this point in the history
  • Loading branch information
paul121 committed Mar 5, 2022
1 parent 1bad238 commit 8285090
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 17 deletions.
4 changes: 1 addition & 3 deletions backend/app/app/routers/api_v2/endpoints/farms.py
Expand Up @@ -55,10 +55,8 @@ def get_all_farm_info(
data[farm.id] = farm.info
else:

# Determine the correct version
version = 2 if len(farm.token.access_token) > 60 else 1
try:
farm_client = get_farm_client(db=db, farm=farm, version=version)
farm_client = get_farm_client(db=db, farm=farm)
except ClientError as e:
data[farm.id] = str(e)

Expand Down
3 changes: 1 addition & 2 deletions backend/app/app/routers/api_v2/endpoints/utils.py
Expand Up @@ -219,8 +219,7 @@ def authorize_farm(

# Reconnect to the farmOS server and update farm info.
try:
version = 2 if len(new_token.access_token) > 60 else 1
farm_client = get_farm_client(db=db, farm=farm, version=version)
farm_client = get_farm_client(db=db, farm=farm)

response = farm_client.info()
# Set the info depending on v1 or v2.
Expand Down
12 changes: 1 addition & 11 deletions backend/app/app/routers/utils/farms.py
Expand Up @@ -222,7 +222,7 @@ def handle_ping_farms(db: Session, settings):


# Create a farmOS.py client.
def get_farm_client(db, farm, version=2):
def get_farm_client(db, farm):
client_id = settings.AGGREGATOR_OAUTH_CLIENT_ID
client_secret = settings.AGGREGATOR_OAUTH_CLIENT_SECRET

Expand Down Expand Up @@ -251,15 +251,6 @@ def get_farm_client(db, farm, version=2):
# Use the saved scope.
scope = farm.scope

# Raise an error if the API endpoint doesn't match the server version.
# The length of the access tokens differs between 1.x and 2.x
if len(token.access_token) < 60 and version == 2:
error = "Server is running farmOS 1.x. Use the /api/v1/farms endpoint."
raise ClientError(error)
elif len(token.access_token) > 60 and version == 1:
error = "Server is running farmOS 2.x. Use the /api/v2/farms endpoint."
raise ClientError(error)

token_updater = partial(_save_token, db=db, farm=farm)

# Allow OAuth over http
Expand Down Expand Up @@ -294,7 +285,6 @@ def get_farm_client(db, farm, version=2):
scope=scope,
token=token.dict(),
token_updater=token_updater,
version=version,
)

# Make an authenticated request to trigger automatic refresh.
Expand Down
1 change: 0 additions & 1 deletion backend/app/app/tests/utils/farm.py
Expand Up @@ -23,7 +23,6 @@ def get_test_farm_instance(db: Session):
hostname=settings.TEST_FARM_URL,
client_id="farm",
scope="farm_manager",
version=2,
)
token = farm_client.authorize(
username=settings.TEST_FARM_USERNAME, password=settings.TEST_FARM_PASSWORD
Expand Down

0 comments on commit 8285090

Please sign in to comment.