コード例 #1
0
        //----------------------------------------------------------------------------
        //
        // AirtableBase.ListRecords
        //
        // Called to get a list of records in the table specified by 'tableName'
        //
        //----------------------------------------------------------------------------

        public async Task <AirtableListRecordsResponse> ListRecords(
            string tableName,
            string offset = null,
            IEnumerable <string> fields = null,
            string filterByFormula      = null,
            int?maxRecords          = null,
            int?pageSize            = null,
            IEnumerable <Sort> sort = null,
            string view             = null)
        {
            HttpResponseMessage response = await ListRecordsInternal(tableName, offset, fields, filterByFormula,
                                                                     maxRecords, pageSize, sort, view);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(new AirtableListRecordsResponse(error));
            }

            var responseBody = await response.Content.ReadAsStringAsync();

            AirtableRecordList recordList = JsonSerializer.Deserialize <AirtableRecordList>(responseBody, JsonOptionIgnoreNullValues);

            return(new AirtableListRecordsResponse(recordList));
        }
コード例 #2
0
        //----------------------------------------------------------------------------
        //
        // AirtableBase.DeleteRecord
        //
        // Called to delete a record with the specified ID in the specified table
        //
        //----------------------------------------------------------------------------

        public async Task <AirtableDeleteRecordResponse> DeleteRecord(
            string tableName,
            string id)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("Table name cannot be null", "tableName");
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("Record ID cannot be null", "id");
            }

            string uriStr   = AIRTABLE_API_URL + BaseId + "/" + Uri.EscapeDataString(tableName) + "/" + id;
            var    request  = new HttpRequestMessage(HttpMethod.Delete, uriStr);
            var    response = await httpClientWithRetries.SendAsync(request);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(new AirtableDeleteRecordResponse(error));
            }
            var responseBody = await response.Content.ReadAsStringAsync();

            var deletedRecord = JsonSerializer.Deserialize <AirtableDeletedRecord>(responseBody);

            return(new AirtableDeleteRecordResponse(deletedRecord.Deleted, deletedRecord.Id));
        }
コード例 #3
0
        //----------------------------------------------------------------------------
        //
        // AirtableBase.RetrieveRecord
        //
        // Called to retrieve a record with the specified id from the specified table.
        //
        //----------------------------------------------------------------------------

        public async Task <AirtableRetrieveRecordResponse> RetrieveRecord(
            string tableName,
            string id)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("Table Name cannot be null", "tableName");
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("Record ID cannot be null", "id");
            }

            string uriStr   = AIRTABLE_API_URL + BaseId + "/" + tableName + "/" + id;
            var    request  = new HttpRequestMessage(HttpMethod.Get, uriStr);
            var    response = await httpClientWithRetries.SendAsync(request);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(new AirtableRetrieveRecordResponse(error));
            }
            var responseBody = await response.Content.ReadAsStringAsync();

            var airtableRecord = JsonConvert.DeserializeObject <AirtableRecord>(responseBody);

            return(new AirtableRetrieveRecordResponse(airtableRecord));
        }
コード例 #4
0
        private async Task <(string ResponseBody, AirtableApiException Error)> ListRecordsInternal(
            string tableName,
            string offset,
            IEnumerable <string> fields,
            string filterByFormula,
            int?maxRecords,
            int?pageSize,
            IEnumerable <Sort> sort,
            string view)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("Table Name cannot be null", "tableName");
            }
            var uri      = BuildUriForListRecords(tableName, offset, fields, filterByFormula, maxRecords, pageSize, sort, view);
            var request  = new HttpRequestMessage(HttpMethod.Get, uri);
            var response = await httpClientWithRetries.SendAsync(request);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(null, error);
            }

            var responseBody = await response.Content.ReadAsStringAsync();

            return(responseBody, null);
        }
コード例 #5
0
        //----------------------------------------------------------------------------
        //
        // AirtableBase.ListRecords
        //
        // Called to get a list of records in the table specified by 'tableName'
        //
        //----------------------------------------------------------------------------

        public async Task <AirtableListRecordsResponse> ListRecords(
            string tableName,
            string offset = null,
            IEnumerable <string> fields = null,
            string filterByFormula      = null,
            int?maxRecords          = null,
            int?pageSize            = null,
            IEnumerable <Sort> sort = null,
            string view             = null)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("Table Name cannot be null", "tableName");
            }

            AirtableRecordList recordList = null;
            var uri      = BuildUriForListRecords(tableName, offset, fields, filterByFormula, maxRecords, pageSize, sort, view);
            var request  = new HttpRequestMessage(HttpMethod.Get, uri);
            var response = await Client.SendAsync(request);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(new AirtableListRecordsResponse(error));
            }

            var responseBody = await response.Content.ReadAsStringAsync();

            recordList = JsonConvert.DeserializeObject <AirtableRecordList>(responseBody);

            return(new AirtableListRecordsResponse(recordList));
        }
コード例 #6
0
        //----------------------------------------------------------------------------
        //
        // AirtableBase.CreateUpdateReplaceRecord
        //
        // worker function which does the real work for creating, updating, or replacing a record
        //
        //----------------------------------------------------------------------------

        private async Task <AirtableCreateUpdateReplaceRecordResponse> CreateUpdateReplaceRecord(
            string tableName,
            Fields fields,
            OperationType operationType,
            string recordId = null,
            bool typecast   = false)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("Table Name cannot be null", "tableName");
            }

            string     uriStr = AIRTABLE_API_URL + BaseId + "/" + tableName + "/";
            HttpMethod httpMethod;

            switch (operationType)
            {
            case OperationType.UPDATE:
                uriStr    += recordId + "/";
                httpMethod = new HttpMethod("PATCH");
                break;

            case OperationType.REPLACE:
                uriStr    += recordId + "/";
                httpMethod = HttpMethod.Put;
                break;

            case OperationType.CREATE:
                httpMethod = HttpMethod.Post;
                break;

            default:
                throw new ArgumentException("Operation Type must be one of { OperationType.UPDATE, OperationType.REPLACE, OperationType.CREATE }", "operationType");
            }

            var fieldsAndTypecast = new { fields = fields.FieldsCollection, typecast = typecast };
            var json = JsonConvert.SerializeObject(fieldsAndTypecast, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            var request = new HttpRequestMessage(httpMethod, uriStr);

            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await httpClientWithRetries.SendAsync(request);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(new AirtableCreateUpdateReplaceRecordResponse(error));
            }
            var responseBody = await response.Content.ReadAsStringAsync();

            var airtableRecord = JsonConvert.DeserializeObject <AirtableRecord>(responseBody);

            return(new AirtableCreateUpdateReplaceRecordResponse(airtableRecord));
        }
コード例 #7
0
        //----------------------------------------------------------------------------
        //
        // AirtableBase.CreateUpdateReplaceMultipleRecords
        //
        // worker function which does the real work for creating, updating or replacing multiple records
        // in one operation
        //
        //----------------------------------------------------------------------------

        private async Task <AirtableCreateUpdateReplaceMultipleRecordsResponse> CreateUpdateReplaceMultipleRecords(string tableName, HttpMethod method, string json)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentException("Table Name cannot be null", "tableName");
            }
            string uriStr  = AIRTABLE_API_URL + BaseId + "/" + Uri.EscapeDataString(tableName) + "/";
            var    request = new HttpRequestMessage(method, uriStr);

            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await httpClientWithRetries.SendAsync(request);

            AirtableApiException error = await CheckForAirtableException(response);

            if (error != null)
            {
                return(new AirtableCreateUpdateReplaceMultipleRecordsResponse(error));
            }
            var responseBody = await response.Content.ReadAsStringAsync();

            var recordList = JsonSerializer.Deserialize <AirtableRecordList>(responseBody);

            return(new AirtableCreateUpdateReplaceMultipleRecordsResponse(recordList.Records));
        }
コード例 #8
0
 public AirtableCreateUpdateReplaceRecordResponse(AirtableApiException error) : base(error)
 {
     Record = null;
 }
コード例 #9
0
 public AirtableRetrieveRecordResponse(AirtableApiException error) : base(error)
 {
     Record = null;
 }
コード例 #10
0
 public AirtableListRecordsResponse(AirtableApiException error) : base(error)
 {
     Offset  = null;
     Records = null;
 }
コード例 #11
0
 public AirtableDeleteRecordResponse(AirtableApiException error) : base(error)
 {
     Deleted = false;
     Id      = null;
 }
コード例 #12
0
 protected AirtableApiResponse(AirtableApiException error)
 {
     Success          = false;
     AirtableApiError = error;
 }