Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 29, 2024
1 parent cb50238 commit 521482a
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions osm_fieldwork/OdkCentralAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import logging
import os
from asyncio import gather
from typing import Optional, List, Dict
from typing import Dict, List, Optional
from uuid import uuid4

import aiohttp
Expand Down Expand Up @@ -343,14 +343,13 @@ async def getEntity(
except aiohttp.ClientError as e:
log.error(f"Error fetching entity: {e}")
return {}

async def createDataset(
self,
projectId: int,
datasetName: str,
self,
projectId: int,
datasetName: str,
):
"""
Creates a dataset for a given project.
"""Creates a dataset for a given project.
Args:
projectId (int): The ID of the project to create the dataset for.
Expand All @@ -362,11 +361,8 @@ async def createDataset(
Raises:
aiohttp.ClientError: If an error occurs during the dataset creation process.
"""

url = f"{self.base}projects/{projectId}/datasets"
payload = {
"name": "features"
}
payload = {"name": "features"}
try:
log.info(f"creating datasets {datasetName} for project {projectId}")
async with self.session.post(
Expand All @@ -382,10 +378,9 @@ async def createDataset(
except aiohttp.ClientError as e:
log.error(f"Failed to create Entity: {e}")
return {}

async def createProperty(self, projectId: int, datasetName: str, field: dict):
"""
Create a property for a dataset.
"""Create a property for a dataset.
Args:
projectId (int): The ID of the project.
Expand Down Expand Up @@ -416,8 +411,7 @@ async def createProperty(self, projectId: int, datasetName: str, field: dict):
return {}

async def createProperties(self, projectId: int, datasetName: str, properties: List[dict]):
"""
Create a property for a dataset.
"""Create a property for a dataset.
Args:
projectId (int): The ID of the project.
Expand All @@ -430,20 +424,19 @@ async def createProperties(self, projectId: int, datasetName: str, properties: L
Raises:
aiohttp.ClientError: If an error occurs during the API request.
"""

try:
log.info(f"bulk uploading properties of dataset {datasetName}")
properties_tasks = [self.createProperty(projectId, datasetName, field) for field in properties]
properties = await gather(*properties_tasks, return_exceptions=True) # type: ignore
properties = await gather(*properties_tasks, return_exceptions=True) # type: ignore
if not properties:
log.warning(f"No properties were uploaded for ODK project ({projectId}) dataset name ({datasetName})")
log.info(f"Successfully created properties for dataset {datasetName}")
return {"success"}
except aiohttp.ClientError as e:
log.error(f"Failed to create properties: {e}")
return {}
#TODO: not required anymore, only if required to upload single entity

# TODO: not required anymore, only if required to upload single entity
async def createEntity(
self,
projectId: int,
Expand Down Expand Up @@ -548,20 +541,13 @@ async def createEntities(
"""
log.info(f"Bulk uploading Entities for project ({projectId}) dataset ({datasetName})")
url = f"{self.base}projects/{projectId}/datasets/{datasetName}/entities"
payload = {
"entities": entities,
"source": {"name": "features.csv", "size": len(entities)}
}
payload = {"entities": entities, "source": {"name": "features.csv", "size": len(entities)}}
try:
log.info(f"Creating ({len(entities)}) entities for project " f"({projectId}) dataset ({datasetName})")
async with self.session.post(
url,
ssl=self.verify,
json=payload
) as response:
response.raise_for_status()
log.info(f"Successfully created entities for project {projectId} in dataset {datasetName}")
return await response.json()
async with self.session.post(url, ssl=self.verify, json=payload) as response:
response.raise_for_status()
log.info(f"Successfully created entities for project {projectId} in dataset {datasetName}")
return await response.json()
except aiohttp.ClientError as e:
log.error(f"Failed to create Entity: {e}")
return {}
Expand Down

0 comments on commit 521482a

Please sign in to comment.