// public async Task CreateRecordMethodAsync(AirtableBase airtableBase) { Task <AirtableCreateUpdateReplaceRecordResponse> task = airtableBase.CreateRecord(tablename, fields, conversion); var response = await task; OutRecord = response.Record; if (response.AirtableApiError.ErrorMessage != null) { // Error reporting errorMessageString = response.AirtableApiError.ErrorMessage; } else { // Do something with the retrieved 'record' errorMessageString = "Success!"; OutRecord = response.Record; } if (response.Success == true) { errorMessageString = "Success!"; } }
private bool GetPotentialPrintersListCore(AirtableRecord record, out List <string> printerNames, out string preferredPrinter) { printerNames = new List <string>(); preferredPrinter = ""; if (record == null) { return(false); } var productRecord = record; if (productRecord != null) { if (productRecord.Fields.ContainsKey(potentialPrintersFieldName)) { foreach (var component in (JArray)(productRecord.Fields[potentialPrintersFieldName])) { var cTask = _invAirtableBase.RetrieveRecord(defaultOwnersTableName, component.ToString()); printerNames.Add(cTask.Result.Record.Fields["Name"].ToString()); } } if (productRecord.Fields.ContainsKey(preferredPrinterFieldName)) { foreach (var component in (JArray)(productRecord.Fields[preferredPrinterFieldName])) { var cTask = _invAirtableBase.RetrieveRecord(defaultOwnersTableName, component.ToString()); preferredPrinter = cTask.Result.Record.Fields["Name"].ToString(); break; } return(true); } } return(false); }
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); }
/// <summary> /// AirtableRecord to LogMessage Object /// </summary> /// <param name="record"></param> /// <returns></returns> public LogMessage ExtractLogMessageFromAirTabelRecord(AirtableRecord record) { LogMessage logMessage = new LogMessage(); if (record == null) { return(null); } if (record.Fields.ContainsKey(Constants.ID)) { logMessage.Id = record.Fields[Constants.ID] as string; } if (record.Fields.ContainsKey(Constants.SUMMARY)) { logMessage.Title = record.Fields[Constants.SUMMARY] as string; } if (record.Fields.ContainsKey(Constants.MESSAGE)) { logMessage.Text = record.Fields[Constants.MESSAGE] as string; } if (record.Fields.ContainsKey(Constants.RECEIVEDAT)) { logMessage.ReceivedAt = record.Fields[Constants.RECEIVEDAT] as DateTime?; } return(logMessage); }
public Thing(AirtableRecord record) { ThingId = record.Id; Name = record.Fields.GetValueOrDefault("Name") as string; Description = record.Fields.GetValueOrDefault("Description") as string; IsActive = (record.Fields.GetValueOrDefault("Is Active") as bool?) ?? false; CreatedDate = DateTime.ParseExact(record.Fields["Created Date"] as string, "yyyy-MM-dd", CultureInfo.InvariantCulture); EmailAddress = record.Fields.GetValueOrDefault("Email Address") as string; }
public static JObject RecordJObject(this AirtableRecord record) { var j = new JObject(new JProperty("id", record.Id), new JProperty("createdTime", record.CreatedTime)); foreach (var field in record.Fields) { j.Add(field.Key, JToken.FromObject(field.Value)); } return(j); }
// STATUS [ July 13, 2019 ] : this works /// <summary> /// Retrieves the Airtable generated Id for an author based on their name /// Note: the Airtable generated Id is different than "Author_Id" /// </summary> /// <param name="authorName">todo: describe authorName parameter on GetAuthorIdFromAuthorName</param> /// <remarks> /// This can be used when the Author table is used as a field in another table /// When author is referenced on another table, the Api provides the generated id instead of the author name /// If you look at the table, it'll show the author's name but that is not what the api actually provides /// </remarks> /// <example> /// var authorNameFromId = await _atM.GetAuthorIdFromAuthorName("Eno Sarris"); /// </example> public async Task <string> GetAuthorIdFromAuthorNameAsync(string authorName) { List <AirtableRecord> authorTableList = await GetAllRecordsFromAirtableAsync(_authorsConfiguration.TableName, _authorsConfiguration.AuthenticationString).ConfigureAwait(false); IEnumerable <AirtableRecord> airtableRecordEnumerable = GetOneRecordFromAirtable(authorTableList, "Name", authorName); AirtableRecord authorRecord = airtableRecordEnumerable.First(); string authorAirtableId = authorRecord.Id; return(authorAirtableId); }
// 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); } }
// STATUS [ July 13, 2019 ] : this works /// <summary> /// Retrieves the Airtable generated Id for website based on its name /// Note: the Airtable generated Id is different than "Website_Id" /// </summary> /// <param name="websiteName">todo: describe websiteName parameter on GetWebsiteIdFromWebsiteName</param> /// <remarks> /// This can be used when the website table is used as a field in another table /// When the website is referenced on another table, the Api provides the generated id instead of the website name /// If you look at the table, it'll show the websites's name but that is not what the api actually provides /// </remarks> /// <example> /// var websiteId = await _atM.GetWebsiteIdFromWebsiteName("FanGraphs"); /// </example> public async Task <string> GetWebsiteIdFromWebsiteNameAsync(string websiteName) { PrintTableConfigurationInfo(_websitesConfiguration.TableName, _websitesConfiguration.AuthenticationString); List <AirtableRecord> websiteTableList = await GetAllRecordsFromAirtableAsync(_websitesConfiguration.TableName, _websitesConfiguration.AuthenticationString).ConfigureAwait(false); IEnumerable <AirtableRecord> airtableRecordEnumerable = GetOneRecordFromAirtable(websiteTableList, "Name", websiteName); AirtableRecord websiteRecord = airtableRecordEnumerable.First(); string websiteAirtableId = websiteRecord.Id; return(websiteAirtableId); }
private bool GetPreferredShipperCore(AirtableRecord record, out string preferredShipper) { preferredShipper = ""; if (record.Fields.ContainsKey(preferredShipperFieldName)) { foreach (var component in (JArray)(record.Fields[preferredShipperFieldName])) { var cTask = _invAirtableBase.RetrieveRecord(defaultOwnersTableName, component.ToString()); preferredShipper = cTask.Result.Record.Fields["Name"].ToString(); break; } return(true); } return(false); }
// public async Task UpdateRecordMethodAsync(AirtableBase airtableBase) { Task <AirtableCreateUpdateReplaceRecordResponse> task = airtableBase.UpdateRecord(tablename, fields, stringID, conversion); var response = await task; OutRecord = response.Record; if (response.AirtableApiError.ErrorMessage != null) { // Error reporting errorMessageString = response.AirtableApiError.DetailedErrorMessage2; } else { // Do something with the retrieved 'record' OutRecord = response.Record; } }
public void ExtractFromAirTabelRecord_ShouldReturnLogMessageObject_FromAirTabelRecord() { //Arrange AirtableRecord airtableRecord = new AirtableRecord(); airtableRecord.Fields.Add(Constants.ID, "Test ID"); airtableRecord.Fields.Add(Constants.SUMMARY, "Test Title"); airtableRecord.Fields.Add(Constants.MESSAGE, "This is test Message"); airtableRecord.Fields.Add(Constants.RECEIVEDAT, DateTime.Now); //Act LogMessage result = _sut.ExtractLogMessageFromAirTabelRecord(airtableRecord); //Assert Assert.Equal(result.Id, airtableRecord.Fields[Constants.ID]); Assert.Equal(result.Title, airtableRecord.Fields[Constants.SUMMARY]); Assert.Equal(result.Text, airtableRecord.Fields[Constants.MESSAGE]); Assert.Equal(result.ReceivedAt, airtableRecord.Fields[Constants.RECEIVEDAT]); }
public async Task WriteLog_ShouldReturn_ServiceResponseWithBoolTrueAndLogMessageAsData_WhenSucessful() { //Arrange LogMessage logMessage = new LogMessage() { Title = "Test Title insert", Text = "Test Text insert Mockup" }; //_configuration.Setup(x => x.GetSection("appKey").Value).Returns("DummyAppKey"); AirtableRecord airtableRecord = new AirtableRecord(); airtableRecord.Fields.Add("Summary", logMessage.Title); airtableRecord.Fields.Add("Message", logMessage.Text); airtableRepo.Setup(x => x.CreateRecordAsAsync(logMessage)).ReturnsAsync(new AirtableCreateUpdateReplaceRecordResponse(airtableRecord)); //Act ServiceResponse <LogMessage> result = await _sut.WriteLog(logMessage); //Assert Assert.True(result.Sucess); Assert.Equal(result.Data, logMessage, new LogMessageCompare()); }
//==================== 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 async Task <List <string> > GetRecordsFromFormula(string table, string formula) { string offset = null; string errorMessage = null; var records = new List <AirtableRecord>(); AirtableRecord myRecord = new AirtableRecord(); using (AirtableBase airtableBase = new AirtableBase(ApiKey, BaseId)) { Task <AirtableListRecordsResponse> task = airtableBase.ListRecords( table, offset, null, formula); AirtableListRecordsResponse response = await task; if (response.Success) { records.AddRange(response.Records.ToList()); offset = response.Offset; } else if (response.AirtableApiError is AirtableApiException) { errorMessage = response.AirtableApiError.GetBaseException().Message; } else { errorMessage = "Unknown error"; } } List <string> jsonRecords = new List <string>(); foreach (AirtableRecord record in records) { var jsonRec = JsonConvert.SerializeObject(record); jsonRecords.Add(jsonRec); } return(jsonRecords); }
/// <summary> /// get the preferred printer for a specified component (for inventory requests) or product record (for on-demand order printing) /// </summary> /// <param name="record"></param> /// <returns></returns> private string GetPreferredPrinterCore(AirtableRecord record) { string preferredPrinter = ""; if (record == null) { return(""); } var productRecord = record; if (productRecord != null) { if (productRecord.Fields.ContainsKey(preferredPrinterFieldName)) { foreach (var item in (JArray)(productRecord.Fields[preferredPrinterFieldName])) { var cTask = _invAirtableBase.RetrieveRecord(defaultOwnersTableName, item.ToString()); preferredPrinter = cTask.Result.Record.Fields["Name"].ToString(); break; } } } return(preferredPrinter); }
public InventoryLocationEntry(AirtableRecord record) { Record = record; }
public async Task <HttpResponseMessage> GetById(string id) { AirtableRecord record = await airtableService.GetById(id); return(Request.CreateResponse(HttpStatusCode.OK, record)); }
protected override void SolveInstance(IGH_DataAccess DA) { bool data = false; fieldNameList.Clear(); fieldList.Clear(); stringID = null; inputRecord = null; // Use the DA object to retrieve the data inside the first input parameter. // If the retieval fails (for example if there is no data) we need to abort. if (!DA.GetData(0, ref data)) { return; } if (!DA.GetData(1, ref baseID)) { return; } if (!DA.GetData(2, ref appKey)) { return; } if (!DA.GetData(3, ref tablename)) { return; } if (!DA.GetDataList(4, fieldNameList)) { return; } if (!DA.GetDataList(5, fieldList)) { return; } if (!DA.GetData(6, ref inputRecord)) { return; } // If the retrieved data is Nothing, we need to abort. // We're also going to abort on a zero-length String. if (data == false) { return; } // Create airtable base and create a record AirtableBase airtableBase = new AirtableBase(appKey, baseID); stringID = inputRecord.Id; if (!fields.FieldsCollection.Any()) { int i = 0; foreach (var fieldval in fieldList) { if (fieldval is Grasshopper.Kernel.Types.GH_String) { fields.AddField(fieldNameList[i], fieldval.ToString()); } else if (fieldval is GH_ObjectWrapper) { GH_ObjectWrapper wrapper = (GH_ObjectWrapper)fieldval; if (wrapper.Value is Newtonsoft.Json.Linq.JArray) { var attList = JsonConvert.DeserializeObject <List <AirtableAttachment> >(wrapper.Value.ToString()); fields.AddField(fieldNameList[i], attList); } else { AirtableRecord record = (AirtableRecord)wrapper.Value; string recID = record.Id; string[] recIDs = new string[1]; recIDs[0] = recID; fields.AddField(fieldNameList[i], recIDs); } } i++; } } Task OutResponse = UpdateRecordMethodAsync(airtableBase); var responseString = OutResponse.ToString(); // if (OutRecord != null) { errorMessageString = "Success!"; } // Use the DA object to assign a new String to the first output parameter. DA.SetData(0, errorMessageString); DA.SetData(1, OutRecord); fieldList.Clear(); fields.FieldsCollection.Clear(); stringID = null; inputRecord = null; }
public static async Task <SdlPlayer> RetrieveSdlPlayer(ulong discordId) { try { AirtableRecord playerRecord = await GetPlayerRecord(discordId); SdlPlayer sdlPlayer = new SdlPlayer(discordId) { AirtableName = playerRecord.Fields.ContainsKey("Name") ? playerRecord.Fields["Name"].ToString() : string.Empty, PowerLevel = Convert.ToDouble(playerRecord.Fields["Power"].ToString()), SwitchFriendCode = playerRecord.Fields.ContainsKey("Friend Code") ? playerRecord.Fields["Friend Code"].ToString() : string.Empty, AirtableId = playerRecord.Id, Role = playerRecord.Fields.ContainsKey("Role") ? playerRecord.Fields["Role"].ToString() : string.Empty }; try { if (playerRecord.Fields.ContainsKey("W%")) { sdlPlayer.OverallWinRate = Convert.ToDouble(playerRecord.Fields["W%"]); } else { sdlPlayer.OverallWinRate = -1; } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("SZ W%")) { sdlPlayer.WinRates[GameMode.SplatZones] = Convert.ToDouble(playerRecord.Fields["SZ W%"]); } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("TC W%")) { sdlPlayer.WinRates[GameMode.TowerControl] = Convert.ToDouble(playerRecord.Fields["TC W%"]); } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("RM W%")) { sdlPlayer.WinRates[GameMode.Rainmaker] = Convert.ToDouble(playerRecord.Fields["RM W%"]); } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("CB W%")) { sdlPlayer.WinRates[GameMode.ClamBlitz] = Convert.ToDouble(playerRecord.Fields["CB W%"]); } } catch (Exception e) { Logger.Warn(e); } return(sdlPlayer); } catch (Exception e) { SdlAirTableException caughtAirTableException = new SdlAirTableException( e.Message, SdlAirTableException.AirtableErrorType.Generic); Logger.Error(caughtAirTableException); throw caughtAirTableException; } }
public InventoryComponent(AirtableRecord record) { Record = record; }
private bool RecordHasFields(AirtableRecord record) { string[] fields = { "id", "Summary", "Message", "receivedAt" }; return(fields.All(f => record.Fields.ContainsKey(f))); }
public InventoryProduct(AirtableRecord record) { Record = record; }
public static async Task PenalizePlayer(ulong discordId, int points, string notes) { AirtableRecord playerRecord = await GetPlayerRecord(discordId); using (AirtableBase airtableBase = new AirtableBase(Globals.BotSettings.AppKey, Globals.BotSettings.BaseId)) { Fields adjustmentsFields = new Fields(); adjustmentsFields.AddField("Player", playerRecord.Id); adjustmentsFields.AddField("Points", -points); adjustmentsFields.AddField("Notes", notes); Task <AirtableCreateUpdateReplaceRecordResponse> createRecordTask = airtableBase.CreateRecord("Adjustments", adjustmentsFields, true); AirtableCreateUpdateReplaceRecordResponse createRecordResponse = await createRecordTask; if (!createRecordResponse.Success) { string errorMessage = createRecordResponse.AirtableApiError != null ? createRecordResponse.AirtableApiError.ErrorMessage : "Unknown error"; SdlAirTableException exception = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(exception); throw exception; } AirtableRecord record = createRecordResponse.Record; if (!playerRecord.Fields.ContainsKey("Adjustments")) { playerRecord.Fields["Adjustments"] = new JArray(); } IEnumerable <JToken> updatedAdjustmentIds = ((JArray)playerRecord.Fields["Adjustments"]).Append(record.Id); Fields updatePlayerFields = new Fields(); updatePlayerFields.AddField("Adjustments", updatedAdjustmentIds.ToArray()); Task <AirtableCreateUpdateReplaceRecordResponse> updateRecordTask = airtableBase.UpdateRecord("Draft Standings", updatePlayerFields, playerRecord.Id, true); AirtableCreateUpdateReplaceRecordResponse updateRecordResponse = await updateRecordTask; if (!updateRecordResponse.Success) { string errorMessage = updateRecordResponse.AirtableApiError != null ? updateRecordResponse.AirtableApiError.ErrorMessage : "Unknown error"; SdlAirTableException exception = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(exception); throw exception; } } }