public async Task <string> GetRecord(string table, string recordId) { string errorMessage = null; var records = new List <AirtableRecord>(); AirtableRecord myRecord = new AirtableRecord(); using (AirtableBase airtableBase = new AirtableBase(ApiKey, BaseId)) { var response = await airtableBase.RetrieveRecord(table, recordId); if (response.Success) { myRecord = response.Record; } else if (response.AirtableApiError is AirtableApiException) { errorMessage = response.AirtableApiError.GetBaseException().Message; } else { errorMessage = "Error!"; } } var responseJson = JsonConvert.SerializeObject(myRecord); return(responseJson); }
public async Task <AirtableRetrieveRecordResponse> GetRecordMethodAsync(AirtableBase airtableBase, String stringIDparam) { Task <AirtableRetrieveRecordResponse> task = airtableBase.RetrieveRecord(tablename, stringIDparam); response = await task; return(response); }
// STATUS [ July 13, 2019 ] : this works /// <summary> /// Get one record from a given table /// </summary> /// <remarks> /// Configuration for each table is setup in Startup.cs and airtableConfiguration.json /// See: https://github.com/ngocnicholas/airtable.net /// </remarks> /// <param name="tableName"> /// Equivalent to the TableName in airtableConfiguration.json /// Equivalent to the tab name in actual airtable /// </param> /// <param name="tableAuthenticationString"> /// Equivalent to the AuthenticationString in airtableConfiguration.json /// </param> /// <param name="recordId"> /// The airtable generated record Id /// This is visible thorugh the API but NOT in the actual table /// It is different than things like "Author_Id", "Record_Id", "Website_Id" /// </param> /// <example> /// var oneRecord = await _atM.GetOneRecordFromAirtableAsync("SpRankings", authenticationString, "rec7yJqKs5Ht3I7j3"); /// </example> public async Task <AirtableRecord> GetOneRecordFromAirtableAsync(string tableName, string tableAuthenticationString, string recordId) { using (AirtableBase airtableBase = new AirtableBase(_airtableConfig.ApiKey, tableAuthenticationString)) { Task <AirtableRetrieveRecordResponse> recordTask = airtableBase.RetrieveRecord(tableName, recordId); AirtableRetrieveRecordResponse recordTaskResponse = await recordTask.ConfigureAwait(false); AirtableRecord oneRecord = recordTaskResponse.Record; return(oneRecord); } }
public async Task <Thing> GetThingAsync(string id) { var response = await Airtable.RetrieveRecord(TABLE_NAME, id); if (response.Success) { return(new Thing(response.Record)); } return(null); }
// public async Task <bool> LoopTasks() { AirtableBase airtableBase = new AirtableBase(appKey, baseID); var tasks = new List <Task <AirtableRetrieveRecordResponse> >(); foreach (string stringID in stringIDs) { if (stringID != null) { Task <AirtableRetrieveRecordResponse> task = airtableBase.RetrieveRecord(tablename, stringID); response = await task; outRecords.Add(response.Record); errorMessage = "Success!"; } else { outRecords.Add(null); errorMessage = response.AirtableApiError.DetailedErrorMessage2; } } return(true); }
//==================== GET BY ID ============================ public async Task <AirtableRecord> GetById(string id) { var tableRecord = new AirtableRecord(); using (AirtableBase airtableBase = new AirtableBase(appKey, baseId)) { AirtableRecord record = new AirtableRecord(); var retrieveResponse = await airtableBase.RetrieveRecord(tableName, id.ToString()); if (!retrieveResponse.Success) { if (retrieveResponse.AirtableApiError is AirtableApiException) { errorMessage = retrieveResponse.AirtableApiError.ErrorMessage; } else { errorMessage = "Unknown error"; } } else { tableRecord = retrieveResponse.Record; } if (!string.IsNullOrEmpty(errorMessage)) { Console.WriteLine("Error message"); // Error reporting } else { Console.WriteLine("Else message"); // Do something with the retrieved 'records' and the 'offset' // for the next page of the record list. } } return(tableRecord); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string rotation = await new StreamReader(req.Body).ReadToEndAsync(); using (AirtableBase airtableBase = new AirtableBase( Environment.GetEnvironmentVariable("airtableApiKey"), Environment.GetEnvironmentVariable("airtableBaseId"))) { log.LogInformation("Airtable connection initialized."); var rotationNumber = int.Parse(new Regex(@"(^\d+)", RegexOptions.Multiline).Match(rotation).Groups[1].Value); var rotationStartDateString = new Regex(@" (\d{2}[A-Z]{3})").Match(rotation).Groups[1].Value; var rotationStartDate = monthConverter[rotationStartDateString.Substring(2, 3)] + "/" + int.Parse(rotationStartDateString.Substring(0, 2)) + "/" + DateTime.Now.Year; var rotationNumberOfAttendants = new Regex(@"([0-9]+) F\/A").Match(rotation).Groups[1].Value; log.LogInformation($"Parsed rotation #{rotationNumber}-{rotationStartDate}."); // Update rotation log.LogInformation("Updating rotation."); var rotationRecordId = req.Headers["Airtable-Record-Id"][0]; var rotationLookup = airtableBase.RetrieveRecord("Rotations", rotationRecordId).GetAwaiter().GetResult(); if (!rotationLookup.Success) { log.LogError(rotationLookup.AirtableApiError.ErrorMessage); return(new StatusCodeResult(500)); } log.LogInformation("Looked up rotation successfully."); var rotationFields = new Fields(); rotationFields.AddField("Rotation #", rotationNumber); rotationFields.AddField("Date", rotationStartDate); var rotationUpdate = airtableBase.UpdateRecord("People", rotationFields, rotationRecordId).GetAwaiter().GetResult(); if (!rotationUpdate.Success) { log.LogError(rotationUpdate.AirtableApiError.ErrorMessage); return(new StatusCodeResult(500)); } log.LogInformation("Updated rotation successfully."); // Add flight attendants for (int i = 0; i < int.Parse(rotationNumberOfAttendants); i++) { log.LogInformation($"Starting to process flight attendant {i}."); var flightAttendantRecord = new Regex($@"^{Convert.ToChar(65 + i)} 0([0-9]*) ([A-Za-z]*)([A-Za-z ]*)", RegexOptions.Multiline).Match(rotation); var faEmployeeId = int.Parse(flightAttendantRecord.Groups[1].Value); var faLookup = airtableBase.ListRecords("People", null, null, $"{{Employee ID}} = {faEmployeeId}").GetAwaiter().GetResult(); if (!faLookup.Success) { log.LogError(faLookup.AirtableApiError.ErrorMessage); return(new StatusCodeResult(500)); } log.LogInformation($"Looked up flight attendant {faLookup} successfully."); if (!faLookup.Records.Any()) { log.LogInformation("Adding flight attendant."); var fields = new Fields(); fields.AddField("Employee ID", faEmployeeId); fields.AddField("First name", flightAttendantRecord.Groups[2].Value); fields.AddField("Last name", flightAttendantRecord.Groups[3].Value); var result = airtableBase.CreateRecord("People", fields).GetAwaiter().GetResult(); if (!result.Success) { log.LogError(result.AirtableApiError.ErrorMessage); return(new StatusCodeResult(500)); } log.LogInformation("Added flight attendant successfully."); } else { log.LogInformation("Flight attendant already registered."); } } } return((ActionResult) new OkResult()); }