/*
 * Copyright 2017-2020 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.grails.airtable;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpHeaderValues;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.annotation.QueryValue;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author Sergio del Amo
 * @since 1.0.0
 */
public class AirtableBaseApi {

    private final AirtableClient airtableClient;
    private final AirtableConfiguration airtableConfiguration;
    private final AirtableBaseConfiguration airtableBaseConfiguration;

    public AirtableBaseApi(AirtableClient airtableClient,
                           AirtableConfiguration airtableConfiguration,
                           AirtableBaseConfiguration airtableBaseConfiguration) {
        this.airtableClient = airtableClient;
        this.airtableConfiguration = airtableConfiguration;
        this.airtableBaseConfiguration = airtableBaseConfiguration;
    }

    public RecordList list(@NonNull @NotBlank @PathVariable String table,
                                   @Nullable @QueryValue("fields[]") List<String> fields,
                                   @Nullable @QueryValue String filterByFormula,
                                   @Nullable @QueryValue Integer maxRecords,
                                   @Nullable @QueryValue Integer pageSize,
                                   @Nullable @QueryValue List<Sort> sort,
                                   @Nullable @QueryValue String view,
                                   @Nullable @QueryValue CellFormat cellFormat,
                                   @Nullable @QueryValue TimeZone timeZone,
                                   @Nullable @QueryValue UserLocale userLocale) {
        return airtableClient.list(HttpHeaderValues.AUTHORIZATION_PREFIX_BEARER + StringUtils.SPACE + airtableConfiguration.getApiKey(),
                airtableConfiguration.getApiVersion(),
                airtableBaseConfiguration.getId(),
                table,
                fields,
                filterByFormula,
                maxRecords,
                pageSize,
                sort,
                view,
                cellFormat,
                timeZone,
                userLocale);
    }

    public RecordList list(@NonNull @NotBlank @PathVariable String table) {
        return list(table,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null);
    }
}

@@ -917,151 +917,488 @@
 ort 
-javax.validation.constraints.NotBlank;%0Aimport java.util.List;%0A%0A/**%0A * @author Sergio del Amo%0A * @since 1.0.0%0A */%0Apublic class AirtableBaseApi %7B
+io.micronaut.http.client.exceptions.HttpClientException;%0Aimport io.micronaut.http.client.exceptions.HttpClientResponseException;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport javax.validation.constraints.NotBlank;%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.Optional;%0A%0A/**%0A * @author Sergio del Amo%0A * @since 1.0.0%0A */%0Apublic class AirtableBaseApi %7B%0A    private static final Logger LOG = LoggerFactory.getLogger(AirtableBaseApi.class);
 %0A%0A  
@@ -2747,24 +2747,42 @@
 erLocale) %7B%0A
+        try %7B%0A    
         retu
@@ -2914,32 +2914,36 @@
                 
+    
 airtableConfigur
@@ -2961,24 +2961,28 @@
 iVersion(),%0A
+    
             
@@ -3036,16 +3036,20 @@
         
+    
 table,%0A 
@@ -3063,24 +3063,28 @@
         
+   
+ 
 fields,%0A
         
@@ -3067,32 +3067,36 @@
         fields,%0A
+    
                 
@@ -3104,32 +3104,36 @@
 ilterByFormula,%0A
+    
                 
@@ -3160,16 +3160,20 @@
         
+    
 pageSize
@@ -3166,32 +3166,36 @@
       pageSize,%0A
+    
                 
@@ -3216,22 +3216,30 @@
         
+    
 view,%0A
+    
         
@@ -3274,16 +3274,20 @@
         
+    
 timeZone
@@ -3280,32 +3280,36 @@
       timeZone,%0A
+    
                 
@@ -3321,16 +3321,992 @@
 ocale);%0A
+        %7D catch (HttpClientResponseException e) %7B%0A            if (LOG.isErrorEnabled()) %7B%0A                LOG.error(%22HTTPClientResponseException getting records - status %7B%7D%22, e.getStatus());%0A            %7D%0A            Optional%3CAirtableApiError%3E airtableApiErrorOptional = e.getResponse().getBody(AirtableApiError.class);%0A            if (airtableApiErrorOptional.isPresent()) %7B%0A                AirtableApiError airtableApiError = airtableApiErrorOptional.get();%0A                if (LOG.isErrorEnabled()) %7B%0A                    LOG.error(%22Airtable API Error - type %7B%7D message %7B%7D%22,%0A                            airtableApiError.getError() != null ? airtableApiError.getError().getType() : %22%22,%0A                            airtableApiError.getError() != null ? airtableApiError.getError().getMessage() : %22%22);%0A                %7D%0A            %7D%0A        %7D%0A        RecordList recordList = new RecordList();%0A        recordList.setRecords(Collections.emptyList());%0A        return recordList;%0A
     %7D%0A%0A 

return empty list if airtable api fails