1515 */
1616package org .springframework .batch .extensions .notion ;
1717
18- import notion .api .v1 .NotionClient ;
19- import notion .api .v1 .http .JavaNetHttpClient ;
20- import notion .api .v1 .logging .Slf4jLogger ;
21- import notion .api .v1 .model .databases .QueryResults ;
22- import notion .api .v1 .model .databases .query .filter .QueryTopLevelFilter ;
23- import notion .api .v1 .model .databases .query .sort .QuerySort ;
24- import notion .api .v1 .model .pages .Page ;
25- import notion .api .v1 .model .pages .PageProperty ;
26- import notion .api .v1 .model .pages .PageProperty .RichText ;
27- import notion .api .v1 .request .databases .QueryDatabaseRequest ;
2818import org .jspecify .annotations .Nullable ;
19+ import org .springframework .batch .extensions .notion .PageProperty .RichTextProperty ;
20+ import org .springframework .batch .extensions .notion .PageProperty .TitleProperty ;
2921import org .springframework .batch .extensions .notion .mapping .PropertyMapper ;
3022import org .springframework .batch .infrastructure .item .ExecutionContext ;
3123import org .springframework .batch .infrastructure .item .ItemReader ;
3224import org .springframework .batch .infrastructure .item .data .AbstractPaginatedDataItemReader ;
25+ import org .springframework .http .HttpHeaders ;
3326import org .springframework .util .Assert ;
27+ import org .springframework .web .client .ApiVersionInserter ;
28+ import org .springframework .web .client .RestClient ;
29+ import org .springframework .web .client .support .RestClientAdapter ;
30+ import org .springframework .web .service .invoker .HttpServiceProxyFactory ;
3431
3532import java .util .Collections ;
3633import java .util .Iterator ;
3936import java .util .Map .Entry ;
4037import java .util .Objects ;
4138import java .util .stream .Collectors ;
42- import java .util .stream .Stream ;
4339
4440/**
4541 * Restartable {@link ItemReader} that reads entries from a Notion database via a paging
@@ -71,11 +67,11 @@ public class NotionDatabaseItemReader<T> extends AbstractPaginatedDataItemReader
7167
7268 private String baseUrl = DEFAULT_BASE_URL ;
7369
74- private @ Nullable QueryTopLevelFilter filter ;
70+ private @ Nullable Filter filter ;
7571
76- private @ Nullable List < QuerySort > sorts ;
72+ private Sort [] sorts = new Sort [ 0 ] ;
7773
78- private @ Nullable NotionClient client ;
74+ private @ Nullable NotionDatabaseService service ;
7975
8076 private boolean hasMore ;
8177
@@ -117,7 +113,7 @@ public void setBaseUrl(String baseUrl) {
117113 * @see Filter#where(Filter)
118114 */
119115 public void setFilter (Filter filter ) {
120- this .filter = filter . toQueryTopLevelFilter () ;
116+ this .filter = filter ;
121117 }
122118
123119 /**
@@ -130,7 +126,7 @@ public void setFilter(Filter filter) {
130126 * @see Sort#by(Sort.Timestamp)
131127 */
132128 public void setSorts (Sort ... sorts ) {
133- this .sorts = Stream . of ( sorts ). map ( Sort :: toQuerySort ). toList () ;
129+ this .sorts = sorts ;
134130 }
135131
136132 /**
@@ -151,10 +147,15 @@ public void setPageSize(int pageSize) {
151147 */
152148 @ Override
153149 protected void doOpen () {
154- client = new NotionClient (token );
155- client .setHttpClient (new JavaNetHttpClient ());
156- client .setLogger (new Slf4jLogger ());
157- client .setBaseUrl (baseUrl );
150+ RestClient restClient = RestClient .builder ()
151+ .baseUrl (baseUrl )
152+ .apiVersionInserter (ApiVersionInserter .useHeader ("Notion-Version" ))
153+ .defaultHeader (HttpHeaders .AUTHORIZATION , "Bearer " + token )
154+ .build ();
155+
156+ RestClientAdapter adapter = RestClientAdapter .create (restClient );
157+ HttpServiceProxyFactory factory = HttpServiceProxyFactory .builderFor (adapter ).build ();
158+ service = factory .createClient (NotionDatabaseService .class );
158159
159160 hasMore = true ;
160161 }
@@ -168,53 +169,47 @@ protected Iterator<T> doPageRead() {
168169 return Collections .emptyIterator ();
169170 }
170171
171- QueryDatabaseRequest request = new QueryDatabaseRequest (databaseId );
172- request .setFilter (filter );
173- request .setSorts (sorts );
174- request .setStartCursor (nextCursor );
175- request .setPageSize (pageSize );
172+ QueryRequest request = new QueryRequest (pageSize , nextCursor , filter , sorts );
176173
177174 @ SuppressWarnings ("DataFlowIssue" )
178- QueryResults queryResults = client . queryDatabase ( request );
175+ QueryResult result = service . query ( databaseId , request );
179176
180- hasMore = queryResults . getHasMore ();
181- nextCursor = queryResults . getNextCursor ();
177+ hasMore = result . hasMore ();
178+ nextCursor = result . nextCursor ();
182179
183- return queryResults . getResults ()
180+ return result . results ()
184181 .stream ()
185182 .map (NotionDatabaseItemReader ::getProperties )
186183 .map (propertyMapper ::map )
187184 .iterator ();
188185 }
189186
190- private static Map <String , String > getProperties (Page element ) {
191- return element . getProperties ()
187+ private static Map <String , String > getProperties (Page page ) {
188+ return page . properties ()
192189 .entrySet ()
193190 .stream ()
194191 .collect (Collectors .toUnmodifiableMap (Entry ::getKey , entry -> getPropertyValue (entry .getValue ())));
195192 }
196193
197194 private static String getPropertyValue (PageProperty property ) {
198- return switch (property .getType ()) {
199- case RichText -> getPlainText (property .getRichText ());
200- case Title -> getPlainText (property .getTitle ());
201- default -> throw new IllegalArgumentException ("Unsupported type: " + property .getType ());
202- };
195+ if (property instanceof RichTextProperty p ) {
196+ return getPlainText (p .richText ());
197+ }
198+ if (property instanceof TitleProperty p ) {
199+ return getPlainText (p .title ());
200+ }
201+ throw new IllegalArgumentException ("Unsupported type: " + property .getClass ());
203202 }
204203
205204 private static String getPlainText (List <RichText > texts ) {
206- return texts .isEmpty () ? "" : texts .get (0 ).getPlainText ();
205+ return texts .isEmpty () ? "" : texts .get (0 ).plainText ();
207206 }
208207
209208 /**
210209 * {@inheritDoc}
211210 */
212- @ SuppressWarnings ("DataFlowIssue" )
213211 @ Override
214212 protected void doClose () {
215- client .close ();
216- client = null ;
217-
218213 hasMore = false ;
219214 }
220215
0 commit comments