11"""Data Loader CLI STAC_API Ingestion Tool."""
2+
23import json
34import os
5+ from typing import Any
46
57import click
6- import requests
8+ from httpx import Client
79
810
9- def load_data (data_dir , filename ) :
11+ def load_data (data_dir : str , filename : str ) -> dict [ str , Any ] :
1012 """Load json data from a file within the specified data directory."""
1113 filepath = os .path .join (data_dir , filename )
1214 if not os .path .exists (filepath ):
@@ -16,28 +18,25 @@ def load_data(data_dir, filename):
1618 return json .load (file )
1719
1820
19- def load_collection (base_url , collection_id , data_dir ) :
21+ def load_collection (client : Client , collection_id : str , data_dir : str ) -> None :
2022 """Load a STAC collection into the database."""
2123 collection = load_data (data_dir , "collection.json" )
2224 collection ["id" ] = collection_id
23- try :
24- resp = requests .post (f"{ base_url } /collections" , json = collection )
25- if resp .status_code == 200 or resp .status_code == 201 :
26- click .echo (f"Status code: { resp .status_code } " )
27- click .echo (f"Added collection: { collection ['id' ]} " )
28- elif resp .status_code == 409 :
29- click .echo (f"Status code: { resp .status_code } " )
30- click .echo (f"Collection: { collection ['id' ]} already exists" )
31- else :
32- click .echo (f"Status code: { resp .status_code } " )
33- click .echo (
34- f"Error writing { collection ['id' ]} collection. Message: { resp .text } "
35- )
36- except requests .ConnectionError :
37- click .secho ("Failed to connect" , fg = "red" , err = True )
25+ resp = client .post ("/collections" , json = collection )
26+ if resp .status_code == 200 or resp .status_code == 201 :
27+ click .echo (f"Status code: { resp .status_code } " )
28+ click .echo (f"Added collection: { collection ['id' ]} " )
29+ elif resp .status_code == 409 :
30+ click .echo (f"Status code: { resp .status_code } " )
31+ click .echo (f"Collection: { collection ['id' ]} already exists" )
32+ else :
33+ click .echo (f"Status code: { resp .status_code } " )
34+ click .echo (f"Error writing { collection ['id' ]} collection. Message: { resp .text } " )
3835
3936
40- def load_items (base_url , collection_id , use_bulk , data_dir ):
37+ def load_items (
38+ client : Client , collection_id : str , use_bulk : bool , data_dir : str
39+ ) -> None :
4140 """Load STAC items into the database based on the method selected."""
4241 # Attempt to dynamically find a suitable feature collection file
4342 feature_files = [
@@ -52,55 +51,48 @@ def load_items(base_url, collection_id, use_bulk, data_dir):
5251 err = True ,
5352 )
5453 raise click .Abort ()
55- feature_collection_file = feature_files [
56- 0
57- ] # Use the first found feature collection file
58- feature_collection = load_data (data_dir , feature_collection_file )
5954
60- load_collection (base_url , collection_id , data_dir )
55+ # Use the first found feature collection file
56+ feature_collection = load_data (data_dir , feature_files [0 ])
57+
58+ load_collection (client , collection_id , data_dir )
6159 if use_bulk :
62- load_items_bulk_insert (base_url , collection_id , feature_collection , data_dir )
60+ load_items_bulk_insert (client , collection_id , feature_collection )
6361 else :
64- load_items_one_by_one (base_url , collection_id , feature_collection , data_dir )
62+ load_items_one_by_one (client , collection_id , feature_collection )
6563
6664
67- def load_items_one_by_one (base_url , collection_id , feature_collection , data_dir ):
65+ def load_items_one_by_one (
66+ client : Client , collection_id : str , feature_collection : dict [str , Any ]
67+ ) -> None :
6868 """Load STAC items into the database one by one."""
6969 for feature in feature_collection ["features" ]:
70- try :
71- feature ["collection" ] = collection_id
72- resp = requests .post (
73- f"{ base_url } /collections/{ collection_id } /items" , json = feature
74- )
75- if resp .status_code == 200 :
76- click .echo (f"Status code: { resp .status_code } " )
77- click .echo (f"Added item: { feature ['id' ]} " )
78- elif resp .status_code == 409 :
79- click .echo (f"Status code: { resp .status_code } " )
80- click .echo (f"Item: { feature ['id' ]} already exists" )
81- except requests .ConnectionError :
82- click .secho ("Failed to connect" , fg = "red" , err = True )
83-
84-
85- def load_items_bulk_insert (base_url , collection_id , feature_collection , data_dir ):
86- """Load STAC items into the database via bulk insert."""
87- try :
88- for i , _ in enumerate (feature_collection ["features" ]):
89- feature_collection ["features" ][i ]["collection" ] = collection_id
90- resp = requests .post (
91- f"{ base_url } /collections/{ collection_id } /items" , json = feature_collection
92- )
70+ feature ["collection" ] = collection_id
71+ resp = client .post (f"/collections/{ collection_id } /items" , json = feature )
9372 if resp .status_code == 200 :
9473 click .echo (f"Status code: { resp .status_code } " )
95- click .echo ("Bulk inserted items successfully." )
96- elif resp .status_code == 204 :
97- click .echo (f"Status code: { resp .status_code } " )
98- click .echo ("Bulk update successful, no content returned." )
74+ click .echo (f"Added item: { feature ['id' ]} " )
9975 elif resp .status_code == 409 :
10076 click .echo (f"Status code: { resp .status_code } " )
101- click .echo ("Conflict detected, some items might already exist." )
102- except requests .ConnectionError :
103- click .secho ("Failed to connect" , fg = "red" , err = True )
77+ click .echo (f"Item: { feature ['id' ]} already exists" )
78+
79+
80+ def load_items_bulk_insert (
81+ client : Client , collection_id : str , feature_collection : dict [str , Any ]
82+ ) -> None :
83+ """Load STAC items into the database via bulk insert."""
84+ for feature in feature_collection ["features" ]:
85+ feature ["collection" ] = collection_id
86+ resp = client .post (f"/collections/{ collection_id } /items" , json = feature_collection )
87+ if resp .status_code == 200 :
88+ click .echo (f"Status code: { resp .status_code } " )
89+ click .echo ("Bulk inserted items successfully." )
90+ elif resp .status_code == 204 :
91+ click .echo (f"Status code: { resp .status_code } " )
92+ click .echo ("Bulk update successful, no content returned." )
93+ elif resp .status_code == 409 :
94+ click .echo (f"Status code: { resp .status_code } " )
95+ click .echo ("Conflict detected, some items might already exist." )
10496
10597
10698@click .command ()
@@ -117,9 +109,10 @@ def load_items_bulk_insert(base_url, collection_id, feature_collection, data_dir
117109 default = "sample_data/" ,
118110 help = "Directory containing collection.json and feature collection file" ,
119111)
120- def main (base_url , collection_id , use_bulk , data_dir ) :
112+ def main (base_url : str , collection_id : str , use_bulk : bool , data_dir : str ) -> None :
121113 """Load STAC items into the database."""
122- load_items (base_url , collection_id , use_bulk , data_dir )
114+ with Client (base_url = base_url ) as client :
115+ load_items (client , collection_id , use_bulk , data_dir )
123116
124117
125118if __name__ == "__main__" :
0 commit comments