Skip to content

Commit

Permalink
[#38] replacing pydantic data type HttpUrl with custom data type Http…
Browse files Browse the repository at this point in the history
…UrlStr
  • Loading branch information
pkdash committed Jan 31, 2024
1 parent f5cd531 commit a552324
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
27 changes: 14 additions & 13 deletions api/adapters/hydroshare.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
from datetime import datetime
from typing import List, Optional, Union
from pydantic import BaseModel, EmailStr, HttpUrl
from pydantic import BaseModel, EmailStr

from api.adapters.base import (
AbstractRepositoryMetadataAdapter,
Expand All @@ -12,13 +12,14 @@
from api.models import schema
from api.models.catalog import DatasetMetadataDOC
from api.models.user import Submission
from api.models.schema import HttpUrlStr


class Creator(BaseModel):
name: Optional[str] = None
email: Optional[EmailStr] = None
organization: Optional[str] = None
homepage: Optional[HttpUrl] = None
homepage: Optional[HttpUrlStr] = None
address: Optional[str] = None
identifiers: Optional[dict] = {}

Expand All @@ -39,7 +40,7 @@ def to_dataset_creator(self):
creator = schema.Organization.model_construct()
creator.name = self.organization
if self.homepage:
creator.url = str(self.homepage)
creator.url = self.homepage
if self.address:
creator.address = self.address

Expand All @@ -50,7 +51,7 @@ class Award(BaseModel):
funding_agency_name: str
title: Optional[str] = None
number: Optional[str] = None
funding_agency_url: Optional[HttpUrl] = None
funding_agency_url: Optional[HttpUrlStr] = None

def to_dataset_grant(self):
grant = schema.Grant.model_construct()
Expand All @@ -64,7 +65,7 @@ def to_dataset_grant(self):
funder = schema.Organization.model_construct()
funder.name = self.funding_agency_name
if self.funding_agency_url:
funder.url = str(self.funding_agency_url)
funder.url = self.funding_agency_url

grant.funder = funder
return grant
Expand Down Expand Up @@ -119,7 +120,7 @@ def to_dataset_spatial_coverage(self):

class ContentFile(BaseModel):
file_name: str
url: HttpUrl
url: HttpUrlStr
size: int
content_type: str
logical_file_type: str
Expand All @@ -128,7 +129,7 @@ class ContentFile(BaseModel):

def to_dataset_media_object(self):
media_object = schema.MediaObject.model_construct()
media_object.contentUrl = str(self.url)
media_object.contentUrl = self.url
media_object.encodingFormat = self.content_type
media_object.contentSize = f"{self.size/1000.00} KB"
media_object.name = self.file_name
Expand Down Expand Up @@ -157,12 +158,12 @@ def to_dataset_part_relation(self, relation_type: str):

class Rights(BaseModel):
statement: str
url: HttpUrl
url: HttpUrlStr

def to_dataset_license(self):
_license = schema.License.model_construct()
_license.name = self.statement
_license.url = str(self.url)
_license.url = self.url
return _license


Expand Down Expand Up @@ -227,8 +228,8 @@ def update_submission(submission: Submission, repo_record_id: str) -> Submission
class _HydroshareResourceMetadata(BaseModel):
title: str
abstract: str
url: HttpUrl
identifier: HttpUrl
url: HttpUrlStr
identifier: HttpUrlStr
creators: List[Creator]
created: datetime
modified: datetime
Expand Down Expand Up @@ -305,8 +306,8 @@ def to_catalog_dataset(self):
dataset.provider = self.to_dataset_provider()
dataset.name = self.title
dataset.description = self.abstract
dataset.url = str(self.url)
dataset.identifier = [str(self.identifier)]
dataset.url = self.url
dataset.identifier = [self.identifier]
dataset.creator = self.to_dataset_creators()
dataset.dateCreated = self.created
dataset.dateModified = self.modified
Expand Down
11 changes: 6 additions & 5 deletions api/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from functools import lru_cache
from typing import Any

from pydantic import HttpUrl
from pydantic_settings import BaseSettings, SettingsConfigDict
from dotenv import load_dotenv

from api.models.schema import HttpUrlStr

# had to use load_dotenv() to get the env variables to work during testing
load_dotenv()

Expand All @@ -18,15 +19,15 @@ class Settings(BaseSettings):
testing: bool = False

oidc_issuer: str
hydroshare_meta_read_url: HttpUrl
hydroshare_file_read_url: HttpUrl
hydroshare_meta_read_url: HttpUrlStr
hydroshare_file_read_url: HttpUrlStr

def __init__(self, **data: Any) -> None:
super().__init__(**data)
if self.testing:
self.database_name = f"{self.database_name}"
self.hydroshare_meta_read_url = str(self.hydroshare_meta_read_url)
self.hydroshare_file_read_url = str(self.hydroshare_file_read_url)
self.hydroshare_meta_read_url = self.hydroshare_meta_read_url
self.hydroshare_file_read_url = self.hydroshare_file_read_url

@property
def db_connection_string(self):
Expand Down

0 comments on commit a552324

Please sign in to comment.