/// <summary> /// Deletes a record from the RJG Website /// </summary> /// <param name="schema"></param> /// <param name="record"></param> /// <param name="endpointHelper"></param> /// <param name="client"></param> /// <returns></returns> public static async Task <string> DeleteRecord(Schema schema, Record record, EndpointHelper endpointHelper, RequestHelper client) { Dictionary <string, object> recObj; var endpoint = endpointHelper.GetEndpointForName(schema.Id); if (String.IsNullOrEmpty(endpoint.MetaDataPath)) { try { recObj = JsonConvert.DeserializeObject <Dictionary <string, object> >(record.DataJson); if (recObj.ContainsKey("id")) { if (recObj["id"] != null) { // delete record // try each endpoint foreach (var path in endpoint.ReadPaths) { try { var uri = String.Format("{0}/{1}", path, recObj["id"]); var response = await client.DeleteAsync(uri); response.EnsureSuccessStatusCode(); Logger.Info("Deleted 1 record."); return(""); } catch (Exception e) { Logger.Error(e, e.Message); } } } return("Could not delete record with no id."); } return("Key 'id' not found on requested record to delete."); } catch (Exception e) { Logger.Error(e, e.Message); return(e.Message); } } // code for modifying forms would go here if needed but currently is not needed return("Write backs are only supported for Classes."); }
/// <summary> /// Writes a record out to RJG Website /// </summary> /// <param name="schema"></param> /// <param name="record"></param> /// <param name="endpointHelper"></param> /// <param name="client"></param> /// <returns></returns> public static async Task <string> PutRecord(Schema schema, Record record, EndpointHelper endpointHelper, RequestHelper client) { // Logger.SetLogLevel(Logger.LogLevel.Debug); Dictionary <string, object> recObj; var endpoint = endpointHelper.GetEndpointForName(schema.Id); if (String.IsNullOrEmpty(endpoint.MetaDataPath)) { try { recObj = JsonConvert.DeserializeObject <Dictionary <string, object> >(record.DataJson); Logger.Debug($"Raw: {JsonConvert.SerializeObject(recObj, Formatting.Indented)}"); var postObj = GetPostObject(endpoint, recObj); Logger.Debug($"Post Obj: {JsonConvert.SerializeObject(postObj, Formatting.Indented)}"); var content = new StringContent(JsonConvert.SerializeObject(postObj), Encoding.UTF8, "application/json"); var response = await client.PostAsync(endpoint.ReadPaths.First(), content); // add checking for if patch needs to happen if (!response.IsSuccessStatusCode) { Logger.Debug(await response.Content.ReadAsStringAsync()); var errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(await response.Content.ReadAsStringAsync()); Logger.Debug($"Post response: {await response.Content.ReadAsStringAsync()}"); if (errorResponse.Code == "product_invalid_sku" && errorResponse.Message.Contains("duplicated")) { // record already exists, check date then patch it var id = errorResponse.Data["resource_id"]; // build and send request var path = String.Format("{0}/{1}", endpoint.ReadPaths.First(), id); var patchObj = GetPatchObject(endpoint, recObj); Logger.Debug($"Patch Obj: {JsonConvert.SerializeObject(patchObj, Formatting.Indented)}"); content = new StringContent(JsonConvert.SerializeObject(patchObj), Encoding.UTF8, "application/json"); response = await client.PatchAsync(path, content); Logger.Debug($"Patch response: {await response.Content.ReadAsStringAsync()}"); Logger.Debug(await response.Content.ReadAsStringAsync()); if (!response.IsSuccessStatusCode) { Logger.Error(null, "Failed to update record."); return(await response.Content.ReadAsStringAsync()); } Logger.Info("Modified 1 record."); return(""); } Logger.Error(null, "Failed to create record."); return(await response.Content.ReadAsStringAsync()); } Logger.Info("Created 1 record."); return(""); } catch (AggregateException e) { Logger.Error(e, e.Flatten().ToString()); return(e.Message); } } // code for modifying forms would go here if needed but currently is not needed return("Write backs are only supported for Classes."); }
/// <summary> /// Discovers schemas located in the RJG Website /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns>Discovered schemas</returns> public override async Task <DiscoverSchemasResponse> DiscoverSchemas(DiscoverSchemasRequest request, ServerCallContext context) { // Logger.SetLogPrefix("discover"); Logger.Info("Discovering Schemas..."); var discoverSchemasResponse = new DiscoverSchemasResponse(); // only return requested schemas if refresh mode selected if (request.Mode == DiscoverSchemasRequest.Types.Mode.Refresh) { try { var refreshSchemas = request.ToRefresh; Logger.Info($"Refresh schemas attempted: {refreshSchemas.Count}"); var tasks = refreshSchemas.Select((s) => { var endpoint = _endpointHelper.GetEndpointForName(s.Id); return(DiscoverSchema.GetSchemaForEndpoint(endpoint, _client)); }) .ToArray(); await Task.WhenAll(tasks); discoverSchemasResponse.Schemas.AddRange(tasks.Where(x => x.Result != null).Select(x => x.Result)); // return refresh schemas Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}"); return(discoverSchemasResponse); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new DiscoverSchemasResponse()); } } // get all schemas try { Logger.Info($"Schemas attempted: {_endpointHelper.Endpoints.Count}"); var tasks = _endpointHelper.Endpoints.Select(e => DiscoverSchema.GetSchemaForEndpoint(e, _client)) .ToArray(); await Task.WhenAll(tasks); discoverSchemasResponse.Schemas.AddRange(tasks.Where(x => x.Result != null).Select(x => x.Result)); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new DiscoverSchemasResponse()); } // return all schemas otherwise Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}"); return(discoverSchemasResponse); }