Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends CrudApiPlugin with GetMany #494

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-proxy-plugins/MockResponses/CrudApiDefinitionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public void LoadApiDefinition()
{
if (!File.Exists(_configuration.ApiFile))
{
_logger.LogWarn($"File {_configuration.ApiFile} not found. CRUD API will be disabled");
return;
}

Expand Down Expand Up @@ -51,6 +50,7 @@ public void LoadApiDefinition()
CrudApiActionType.Create => "POST",
CrudApiActionType.GetAll => "GET",
CrudApiActionType.GetOne => "GET",
CrudApiActionType.GetMany => "GET",
CrudApiActionType.Merge => "PATCH",
CrudApiActionType.Update => "PUT",
CrudApiActionType.Delete => "DELETE",
Expand Down
137 changes: 99 additions & 38 deletions dev-proxy-plugins/MockResponses/CrudApiPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum CrudApiActionType
Create,
GetAll,
GetOne,
GetMany,
Merge,
Update,
Delete
Expand Down Expand Up @@ -85,7 +86,6 @@ private void LoadData()
var dataFilePath = Path.GetFullPath(ProxyUtils.ReplacePathTokens(_configuration.DataFile), Path.GetDirectoryName(_proxyConfiguration?.ConfigFile ?? string.Empty) ?? string.Empty);
if (!File.Exists(dataFilePath))
{
_logger?.LogWarn($"File {dataFilePath} not found. CRUD API will be disabled");
_configuration.Actions = Array.Empty<CrudApiAction>();
return;
}
Expand Down Expand Up @@ -166,68 +166,128 @@ private void GetAll(SessionEventArgs e, CrudApiAction action, IDictionary<string

private void GetOne(SessionEventArgs e, CrudApiAction action, IDictionary<string, string> parameters)
{
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
try
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
}

SendJsonResponse(JsonConvert.SerializeObject(item, Formatting.Indented), HttpStatusCode.OK, e);
_logger?.LogRequest([$"200 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}
catch (Exception ex)
{
SendJsonResponse(JsonConvert.SerializeObject(ex, Formatting.Indented), HttpStatusCode.InternalServerError, e);
_logger?.LogRequest([$"500 {action.Url}"], MessageType.Failed, new LoggingContext(e));
}
}

SendJsonResponse(JsonConvert.SerializeObject(item, Formatting.Indented), HttpStatusCode.OK, e);
_logger?.LogRequest([$"200 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
private void GetMany(SessionEventArgs e, CrudApiAction action, IDictionary<string, string> parameters)
{
try
{
var items = _data?.SelectTokens(ReplaceParams(action.Query, parameters));
if (items is null)
{
items = Array.Empty<JToken>();
}

SendJsonResponse(JsonConvert.SerializeObject(items, Formatting.Indented), HttpStatusCode.OK, e);
_logger?.LogRequest([$"200 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}
catch (Exception ex)
{
SendJsonResponse(JsonConvert.SerializeObject(ex, Formatting.Indented), HttpStatusCode.InternalServerError, e);
_logger?.LogRequest([$"500 {action.Url}"], MessageType.Failed, new LoggingContext(e));
}
}

private void Create(SessionEventArgs e, CrudApiAction action, IDictionary<string, string> parameters)
{
_data?.Add(JObject.Parse(e.HttpClient.Request.BodyString));
SendJsonResponse(JsonConvert.SerializeObject(e.HttpClient.Request.BodyString, Formatting.Indented), HttpStatusCode.Created, e);
_logger?.LogRequest([$"201 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
try
{
_data?.Add(JObject.Parse(e.HttpClient.Request.BodyString));
SendJsonResponse(JsonConvert.SerializeObject(e.HttpClient.Request.BodyString, Formatting.Indented), HttpStatusCode.Created, e);
_logger?.LogRequest([$"201 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}
catch (Exception ex)
{
SendJsonResponse(JsonConvert.SerializeObject(ex, Formatting.Indented), HttpStatusCode.InternalServerError, e);
_logger?.LogRequest([$"500 {action.Url}"], MessageType.Failed, new LoggingContext(e));
}
}

private void Merge(SessionEventArgs e, CrudApiAction action, IDictionary<string, string> parameters)
{
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
try
{
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
}
var update = JObject.Parse(e.HttpClient.Request.BodyString);
((JContainer)item)?.Merge(update);
SendEmptyResponse(HttpStatusCode.NoContent, e);
_logger?.LogRequest([$"204 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}
catch (Exception ex)
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
SendJsonResponse(JsonConvert.SerializeObject(ex, Formatting.Indented), HttpStatusCode.InternalServerError, e);
_logger?.LogRequest([$"500 {action.Url}"], MessageType.Failed, new LoggingContext(e));
}
var update = JObject.Parse(e.HttpClient.Request.BodyString);
((JContainer)item)?.Merge(update);
SendEmptyResponse(HttpStatusCode.NoContent, e);
_logger?.LogRequest([$"204 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}

private void Update(SessionEventArgs e, CrudApiAction action, IDictionary<string, string> parameters)
{
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
try
{
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
}
var update = JObject.Parse(e.HttpClient.Request.BodyString);
((JContainer)item)?.Replace(update);
SendEmptyResponse(HttpStatusCode.NoContent, e);
_logger?.LogRequest([$"204 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}
catch (Exception ex)
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
SendJsonResponse(JsonConvert.SerializeObject(ex, Formatting.Indented), HttpStatusCode.InternalServerError, e);
_logger?.LogRequest([$"500 {action.Url}"], MessageType.Failed, new LoggingContext(e));
}
var update = JObject.Parse(e.HttpClient.Request.BodyString);
((JContainer)item)?.Replace(update);
SendEmptyResponse(HttpStatusCode.NoContent, e);
_logger?.LogRequest([$"204 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}

private void Delete(SessionEventArgs e, CrudApiAction action, IDictionary<string, string> parameters)
{
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
try
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
}
var item = _data?.SelectToken(ReplaceParams(action.Query, parameters));
if (item is null)
{
SendNotFoundResponse(e);
_logger?.LogRequest([$"404 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
return;
}

item?.Remove();
SendEmptyResponse(HttpStatusCode.NoContent, e);
_logger?.LogRequest([$"204 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
item?.Remove();
SendEmptyResponse(HttpStatusCode.NoContent, e);
_logger?.LogRequest([$"204 {action.Url}"], MessageType.Mocked, new LoggingContext(e));
}
catch (Exception ex)
{
SendJsonResponse(JsonConvert.SerializeObject(ex, Formatting.Indented), HttpStatusCode.InternalServerError, e);
_logger?.LogRequest([$"500 {action.Url}"], MessageType.Failed, new LoggingContext(e));
}
}

private Tuple<Action<SessionEventArgs, CrudApiAction, IDictionary<string, string>>, CrudApiAction, IDictionary<string, string>>? GetMatchingActionHandler(Request request)
Expand Down Expand Up @@ -291,6 +351,7 @@ private void Delete(SessionEventArgs e, CrudApiAction action, IDictionary<string
CrudApiActionType.Create => Create,
CrudApiActionType.GetAll => GetAll,
CrudApiActionType.GetOne => GetOne,
CrudApiActionType.GetMany => GetMany,
CrudApiActionType.Merge => Merge,
CrudApiActionType.Update => Update,
CrudApiActionType.Delete => Delete,
Expand Down
11 changes: 10 additions & 1 deletion schemas/v1.0/crudapiplugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
"type": "object",
"properties": {
"action": {
"type": "string"
"type": "string",
"enum": [
"create",
"getAll",
"getOne",
"getMany",
"merge",
"update",
"delete"
]
},
"url": {
"type": "string"
Expand Down
Loading