mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 02:26:55 -07:00
Added gravitar image
This commit is contained in:
parent
f6bf6493b2
commit
7d8848a69c
17 changed files with 137 additions and 25 deletions
46
Ombi.UI/Modules/Api/ApiDocsModule.cs
Normal file
46
Ombi.UI/Modules/Api/ApiDocsModule.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiDocsModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Nancy.Responses.Negotiation;
|
||||
using Ombi.Core;
|
||||
using Ombi.Core.SettingModels;
|
||||
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiDocsModule : BaseModule
|
||||
{
|
||||
public ApiDocsModule(ISettingsService<PlexRequestSettings> pr, ISecurityExtensions security) : base("apidocs", pr, security)
|
||||
{
|
||||
Get["/"] = x => Documentation();
|
||||
}
|
||||
public Negotiator Documentation()
|
||||
{
|
||||
return View["Index"];
|
||||
}
|
||||
}
|
||||
}
|
115
Ombi.UI/Modules/Api/ApiRequestMetadataModule.cs
Normal file
115
Ombi.UI/Modules/Api/ApiRequestMetadataModule.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiRequestMetadataModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Nancy.Metadata.Modules;
|
||||
using Nancy.Swagger;
|
||||
using Ombi.Store;
|
||||
using Ombi.UI.Models;
|
||||
using Ombi.UI.Models.UserManagement;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiRequestMetadataModule: MetadataModule<SwaggerRouteData>
|
||||
{
|
||||
public ApiRequestMetadataModule()
|
||||
{
|
||||
Describe["GetRequests"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/requests");
|
||||
with.Summary("The list of requests");
|
||||
|
||||
with.Notes("This returns a list of requests");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.Model<ApiModel<List<RequestedModel>>>();
|
||||
});
|
||||
|
||||
Describe["GetRequest"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/requests/{id}");
|
||||
with.Summary("Get's a single request");
|
||||
|
||||
with.Notes("This returns a single request");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.PathParam<int>("id");
|
||||
with.Model<ApiModel<List<RequestedModel>>>();
|
||||
});
|
||||
|
||||
Describe["PostRequests"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/requests");
|
||||
with.Summary("Create a new request");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.BodyParam<RequestedModel>("The request", true);
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.Notes("Creates a new request");
|
||||
});
|
||||
|
||||
Describe["PutRequests"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/requests");
|
||||
with.Summary("Updates an existing request");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.BodyParam<RequestedModel>("The request", true);
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.Notes("Updates an existing request e.g. Add a issue to the request");
|
||||
});
|
||||
|
||||
Describe["DeleteRequests"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/requests/{id}");
|
||||
with.Summary("Deletes an existing request");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.PathParam<int>("id", required:true);
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.Notes("Deletes an existing request. If the request doesn't exist we will return an error.");
|
||||
});
|
||||
|
||||
Describe["GetApiKey"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/apikey");
|
||||
with.Summary("Gets the Api Key for Ombi");
|
||||
with.Model<ApiModel<string>>();
|
||||
with.QueryParam<string>("username", required:true );
|
||||
with.QueryParam<string>("password", required: true );
|
||||
with.Notes("Get's the current api key for the application");
|
||||
});
|
||||
|
||||
Describe["PutCredentials"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/credentials/{username}");
|
||||
with.Summary("Sets a new password for the user");
|
||||
with.Model<ApiModel<string>>();
|
||||
with.PathParam<int>("username", required:true);
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<UserUpdateViewModel>("User update view model", true);
|
||||
with.Notes("Sets a new password for the user");
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
165
Ombi.UI/Modules/Api/ApiRequestModule.cs
Normal file
165
Ombi.UI/Modules/Api/ApiRequestModule.cs
Normal file
|
@ -0,0 +1,165 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiRequestModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Nancy;
|
||||
using Nancy.Extensions;
|
||||
using Nancy.Validation;
|
||||
using Newtonsoft.Json;
|
||||
using Ombi.Core;
|
||||
using Ombi.Core.SettingModels;
|
||||
using Ombi.Store;
|
||||
using Ombi.UI.Models;
|
||||
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiRequestModule : BaseApiModule
|
||||
{
|
||||
public ApiRequestModule(IRequestService service, ISettingsService<PlexRequestSettings> pr, ISecurityExtensions security) : base("api", pr, security)
|
||||
{
|
||||
Get["GetRequests","/requests"] = x => GetRequests();
|
||||
Get["GetRequest","/requests/{id}"] = x => GetSingleRequests(x);
|
||||
Post["PostRequests", "/requests"] = x => CreateRequest();
|
||||
Put["PutRequests", "/requests"] = x => UpdateRequest();
|
||||
Delete["DeleteRequests", "/requests/{id}"] = x => DeleteRequest(x);
|
||||
|
||||
|
||||
RequestService = service;
|
||||
SettingsService = pr;
|
||||
}
|
||||
|
||||
private IRequestService RequestService { get; }
|
||||
private ISettingsService<PlexRequestSettings> SettingsService { get; }
|
||||
|
||||
public Response GetRequests()
|
||||
{
|
||||
var apiModel = new ApiModel<List<RequestedModel>> { Data = new List<RequestedModel>() };
|
||||
|
||||
var requests = RequestService.GetAll();
|
||||
apiModel.Data.AddRange(requests);
|
||||
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
public Response GetSingleRequests(dynamic x)
|
||||
{
|
||||
var id = (int)x.id;
|
||||
var apiModel = new ApiModel<List<RequestedModel>> { Data = new List<RequestedModel>() };
|
||||
|
||||
var requests = RequestService.Get(id);
|
||||
if (string.IsNullOrEmpty(requests.Title))
|
||||
{
|
||||
apiModel.Error = true;
|
||||
apiModel.ErrorMessage = "Request does not exist";
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
apiModel.Data.Add(requests);
|
||||
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
public Response CreateRequest()
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<RequestedModel>(Request.Body.AsString());
|
||||
var a = this.Validate(request);
|
||||
if (!a.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(a);
|
||||
}
|
||||
|
||||
var apiModel = new ApiModel<bool>();
|
||||
var result = RequestService.AddRequest(request);
|
||||
|
||||
if (result == -1)
|
||||
{
|
||||
apiModel.Error = true;
|
||||
apiModel.ErrorMessage = "Could not insert the new request into the database. Internal error.";
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
apiModel.Data = true;
|
||||
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
public Response UpdateRequest()
|
||||
{
|
||||
var request = JsonConvert.DeserializeObject<RequestedModel>(Request.Body.AsString());
|
||||
var a = this.Validate(request);
|
||||
if (!a.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(a);
|
||||
}
|
||||
|
||||
|
||||
var apiModel = new ApiModel<bool>();
|
||||
var result = RequestService.UpdateRequest(request);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
apiModel.Error = true;
|
||||
apiModel.ErrorMessage = "Could not update the request into the database. Internal error.";
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
apiModel.Data = true;
|
||||
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
public Response DeleteRequest(dynamic x)
|
||||
{
|
||||
var id = (int)x.id;
|
||||
var apiModel = new ApiModel<bool>();
|
||||
|
||||
try
|
||||
{
|
||||
var exisitingRequest = RequestService.Get(id);
|
||||
if (string.IsNullOrEmpty(exisitingRequest.Title))
|
||||
{
|
||||
apiModel.Error = true;
|
||||
apiModel.ErrorMessage = $"The request id {id} does not exist";
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
RequestService.DeleteRequest(exisitingRequest);
|
||||
apiModel.Data = true;
|
||||
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
apiModel.Error = true;
|
||||
apiModel.ErrorMessage = "Could not delete the request from the database. Internal error.";
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
175
Ombi.UI/Modules/Api/ApiSettingsMetadataModule.cs
Normal file
175
Ombi.UI/Modules/Api/ApiSettingsMetadataModule.cs
Normal file
|
@ -0,0 +1,175 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiSettingsMetadataModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Nancy.Metadata.Modules;
|
||||
using Nancy.Swagger;
|
||||
using Ombi.Core.SettingModels;
|
||||
using Ombi.UI.Models;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiSettingsMetadataModule: MetadataModule<SwaggerRouteData>
|
||||
{
|
||||
public ApiSettingsMetadataModule()
|
||||
{
|
||||
Describe["GetAuthSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/authentication");
|
||||
with.Summary("Gets the authentication settings saved in the application");
|
||||
with.Model<ApiModel<AuthenticationSettings>>();
|
||||
with.Notes("Gets the authentication settings saved in the application");
|
||||
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostAuthSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/authentication");
|
||||
with.Summary("Saves the authentication settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<AuthenticationSettings>("Authentication settings", true);
|
||||
with.Notes("Saves the authentication settings saved in the application");
|
||||
});
|
||||
|
||||
Describe["GetPlexSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/plex");
|
||||
with.Summary("Gets the Plex settings saved in the application");
|
||||
with.Model<ApiModel<PlexSettings>>();
|
||||
with.Notes("Gets the Plex settings saved in the application");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostPlexSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/plex");
|
||||
with.Summary("Saves the Plex settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<PlexSettings>("Plex settings", true);
|
||||
with.Notes("Saves the Plex settings saved in the application");
|
||||
});
|
||||
|
||||
|
||||
Describe["GetCouchPotatoSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/couchpotato");
|
||||
with.Summary("Gets the CouchPotato settings saved in the application");
|
||||
with.Model<ApiModel<CouchPotatoSettings>>();
|
||||
with.Notes("Gets the CouchPotato settings saved in the application");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostCouchPotatoSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/couchpotato");
|
||||
with.Summary("Saves the CouchPotato settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<CouchPotatoSettings>("CouchPotato settings", true);
|
||||
with.Notes("Saves the CouchPotato settings saved in the application");
|
||||
});
|
||||
|
||||
Describe["GetSonarrSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/sonarr");
|
||||
with.Summary("Gets the sonarr settings saved in the application");
|
||||
with.Model<ApiModel<SonarrSettings>>();
|
||||
with.Notes("Gets the sonarr settings saved in the application");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostSonarrSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/sonarr");
|
||||
with.Summary("Saves the sonarr settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<SonarrSettings>("sonarr settings", true);
|
||||
with.Notes("Saves the sonarr settings saved in the application");
|
||||
});
|
||||
|
||||
Describe["GetSickRageSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/sickrage");
|
||||
with.Summary("Gets the SickRage settings saved in the application");
|
||||
with.Model<ApiModel<SickRageSettings>>();
|
||||
with.Notes("Gets the SickRage settings saved in the application");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostSickRageSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/sickrage");
|
||||
with.Summary("Saves the SickRage settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<SickRageSettings>("SickRage settings", true);
|
||||
with.Notes("Saves the sickrage settings saved in the application");
|
||||
});
|
||||
|
||||
Describe["GetHeadphonesSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/headphones");
|
||||
with.Summary("Gets the headphones settings saved in the application");
|
||||
with.Model<ApiModel<HeadphonesSettings>>();
|
||||
with.Notes("Gets the headphones settings saved in the application");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostHeadphonesSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/sickrage");
|
||||
with.Summary("Saves the headphones settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<HeadphonesSettings>("headphones settings", true);
|
||||
with.Notes("Saves the headphones settings saved in the application");
|
||||
});
|
||||
|
||||
Describe["GetPlexRequestSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/plexrequest");
|
||||
with.Summary("Gets the plexrequest settings saved in the application");
|
||||
with.Model<ApiModel<PlexRequestSettings>>();
|
||||
with.Notes("Gets the plexrequest settings saved in the application");
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
});
|
||||
|
||||
Describe["PostPlexRequestSettings"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/settings/plexrequest");
|
||||
with.Summary("Saves the plexrequest settings saved in the application");
|
||||
with.Model<ApiModel<bool>>();
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<PlexRequestSettings>("plexrequest settings", true);
|
||||
with.Notes("Saves the plexrequest settings saved in the application");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
366
Ombi.UI/Modules/Api/ApiSettingsModule.cs
Normal file
366
Ombi.UI/Modules/Api/ApiSettingsModule.cs
Normal file
|
@ -0,0 +1,366 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using Nancy;
|
||||
using Nancy.Extensions;
|
||||
using Nancy.Validation;
|
||||
using Newtonsoft.Json;
|
||||
using Ombi.Core;
|
||||
using Ombi.Core.SettingModels;
|
||||
using Ombi.Helpers;
|
||||
using Ombi.UI.Models;
|
||||
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiSettingsModule : BaseApiModule
|
||||
{
|
||||
public ApiSettingsModule(ISettingsService<PlexRequestSettings> pr, ISettingsService<AuthenticationSettings> auth,
|
||||
ISettingsService<PlexSettings> plexSettings, ISettingsService<CouchPotatoSettings> cp,
|
||||
ISettingsService<SonarrSettings> sonarr, ISettingsService<SickRageSettings> sr, ISettingsService<HeadphonesSettings> hp, ISecurityExtensions security) : base("api", pr, security)
|
||||
{
|
||||
Get["GetVersion", "/version"] = x => GetVersion();
|
||||
|
||||
|
||||
Get["GetAuthSettings", "/settings/authentication"] = x => GetAuthSettings();
|
||||
Post["PostAuthSettings", "/settings/authentication"] = x => PostAuthSettings();
|
||||
|
||||
Get["GetPlexRequestSettings", "/settings/plexrequest"] = x => GetPrSettings();
|
||||
Post["PostPlexRequestSettings", "/settings/plexrequest"] = x => PostPrSettings();
|
||||
|
||||
Get["GetPlexSettings", "/settings/plex"] = x => GetPlexSettings();
|
||||
Post["PostPlexSettings", "/settings/plex"] = x => PostPlexSettings();
|
||||
|
||||
Get["GetCouchPotatoSettings", "/settings/couchpotato"] = x => GetCpSettings();
|
||||
Post["PostCouchPotatoSettings", "/settings/couchpotato"] = x => PostCpSettings();
|
||||
|
||||
Get["GetSonarrSettings", "/settings/sonarr"] = x => GetSonarrSettings();
|
||||
Post["PostSonarrSettings", "/settings/sonarr"] = x => PostSonarrSettings();
|
||||
|
||||
Get["GetSickRageSettings", "/settings/sickrage"] = x => GetSickRageSettings();
|
||||
Post["PostSickRageSettings", "/settings/sickrage"] = x => PostSickRageSettings();
|
||||
|
||||
Get["GetHeadphonesSettings", "/settings/headphones"] = x => GetHeadphonesSettings();
|
||||
Post["PostHeadphonesSettings", "/settings/headphones"] = x => PostHeadphonesSettings();
|
||||
|
||||
SettingsService = pr;
|
||||
AuthSettings = auth;
|
||||
PlexSettings = plexSettings;
|
||||
CpSettings = cp;
|
||||
SonarrSettings = sonarr;
|
||||
SickRageSettings = sr;
|
||||
HeadphonesSettings = hp;
|
||||
}
|
||||
|
||||
private ISettingsService<PlexRequestSettings> SettingsService { get; }
|
||||
private ISettingsService<AuthenticationSettings> AuthSettings { get; }
|
||||
private ISettingsService<PlexSettings> PlexSettings { get; }
|
||||
private ISettingsService<CouchPotatoSettings> CpSettings { get; }
|
||||
private ISettingsService<SonarrSettings> SonarrSettings { get; }
|
||||
private ISettingsService<SickRageSettings> SickRageSettings { get; }
|
||||
private ISettingsService<HeadphonesSettings> HeadphonesSettings { get; }
|
||||
|
||||
private Response GetVersion()
|
||||
{
|
||||
return ReturnReponse(AssemblyHelper.GetProductVersion());
|
||||
}
|
||||
|
||||
|
||||
private Response GetPrSettings()
|
||||
{
|
||||
var model = new ApiModel<PlexRequestSettings>();
|
||||
try
|
||||
{
|
||||
var settings = SettingsService.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostPrSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<PlexRequestSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = SettingsService.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
private Response GetAuthSettings()
|
||||
{
|
||||
var model = new ApiModel<AuthenticationSettings>();
|
||||
try
|
||||
{
|
||||
var settings = AuthSettings.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostAuthSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<AuthenticationSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = AuthSettings.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
private Response GetPlexSettings()
|
||||
{
|
||||
var model = new ApiModel<PlexSettings>();
|
||||
try
|
||||
{
|
||||
var settings = PlexSettings.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostPlexSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<PlexSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = PlexSettings.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
private Response GetCpSettings()
|
||||
{
|
||||
var model = new ApiModel<CouchPotatoSettings>();
|
||||
try
|
||||
{
|
||||
var settings = CpSettings.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostCpSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<CouchPotatoSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = CpSettings.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
private Response GetSonarrSettings()
|
||||
{
|
||||
var model = new ApiModel<SonarrSettings>();
|
||||
try
|
||||
{
|
||||
var settings = SonarrSettings.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostSonarrSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<SonarrSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = SonarrSettings.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
private Response GetSickRageSettings()
|
||||
{
|
||||
var model = new ApiModel<SickRageSettings>();
|
||||
try
|
||||
{
|
||||
var settings = SickRageSettings.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostSickRageSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<SickRageSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = SickRageSettings.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
private Response GetHeadphonesSettings()
|
||||
{
|
||||
var model = new ApiModel<HeadphonesSettings>();
|
||||
try
|
||||
{
|
||||
var settings = HeadphonesSettings.GetSettings();
|
||||
model.Data = settings;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
model.ErrorMessage = e.Message;
|
||||
model.Error = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
|
||||
private Response PostHeadphonesSettings()
|
||||
{
|
||||
var newSettings = JsonConvert.DeserializeObject<HeadphonesSettings>(Request.Body.AsString());
|
||||
var result = this.Validate(newSettings);
|
||||
if (!result.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(result);
|
||||
}
|
||||
|
||||
var model = new ApiModel<bool>();
|
||||
var settings = HeadphonesSettings.SaveSettings(newSettings);
|
||||
if (settings)
|
||||
{
|
||||
model.Data = true;
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Could not update the settings";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
}
|
||||
}
|
62
Ombi.UI/Modules/Api/ApiUserMetadataModule.cs
Normal file
62
Ombi.UI/Modules/Api/ApiUserMetadataModule.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiUserMetadataModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Nancy.Metadata.Modules;
|
||||
using Nancy.Swagger;
|
||||
using Ombi.UI.Models;
|
||||
using Ombi.UI.Models.UserManagement;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiUserMetadataModule: MetadataModule<SwaggerRouteData>
|
||||
{
|
||||
public ApiUserMetadataModule()
|
||||
{
|
||||
Describe["GetApiKey"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/apikey");
|
||||
with.Summary("Gets the Api Key for Ombi");
|
||||
with.Model<ApiModel<string>>();
|
||||
with.QueryParam<string>("username", required:true );
|
||||
with.QueryParam<string>("password", required: true );
|
||||
with.Notes("Get's the current api key for the application");
|
||||
});
|
||||
|
||||
Describe["PutCredentials"] = description => description.AsSwagger(with =>
|
||||
{
|
||||
with.ResourcePath("/credentials/{username}");
|
||||
with.Summary("Sets a new password for the user");
|
||||
with.Model<ApiModel<string>>();
|
||||
with.PathParam<int>("username", required:true);
|
||||
with.QueryParam<string>("apikey", "The Api Key found in the settings", true);
|
||||
with.BodyParam<UserUpdateViewModel>("User update view model", true);
|
||||
with.Notes("Sets a new password for the user");
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
102
Ombi.UI/Modules/Api/ApiUserModule.cs
Normal file
102
Ombi.UI/Modules/Api/ApiUserModule.cs
Normal file
|
@ -0,0 +1,102 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApiModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Nancy;
|
||||
using Nancy.ModelBinding;
|
||||
using Ombi.Core;
|
||||
using Ombi.Core.SettingModels;
|
||||
using Ombi.UI.Models;
|
||||
using Ombi.UI.Models.UserManagement;
|
||||
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public class ApiUserModule : BaseApiModule
|
||||
{
|
||||
public ApiUserModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, ISecurityExtensions security) : base("api", pr, security)
|
||||
{
|
||||
|
||||
Put["PutCredentials", "/credentials/{username}"] = x => ChangePassword(x);
|
||||
|
||||
Get["GetApiKey", "/apikey"] = x => GetApiKey();
|
||||
|
||||
SettingsService = pr;
|
||||
UserMapper = m;
|
||||
}
|
||||
|
||||
private ISettingsService<PlexRequestSettings> SettingsService { get; }
|
||||
private ICustomUserMapper UserMapper { get; }
|
||||
|
||||
public Response ChangePassword(dynamic x)
|
||||
{
|
||||
var username = (string)x.username;
|
||||
var userModel = this.BindAndValidate<UserUpdateViewModel>();
|
||||
|
||||
if (!ModelValidationResult.IsValid)
|
||||
{
|
||||
return ReturnValidationReponse(ModelValidationResult);
|
||||
}
|
||||
|
||||
var valid = UserMapper.ValidateUser(username, userModel.CurrentPassword);
|
||||
if (valid == null)
|
||||
{
|
||||
var errorModel = new ApiModel<string> { Error = true, ErrorMessage = "Incorrect username or password" };
|
||||
return ReturnReponse(errorModel);
|
||||
}
|
||||
var result = UserMapper.UpdatePassword(username, userModel.CurrentPassword, userModel.NewPassword);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
var errorModel = new ApiModel<string> { Error = true, ErrorMessage = "Could not update the password. " };
|
||||
return ReturnReponse(errorModel);
|
||||
}
|
||||
|
||||
|
||||
var model = new ApiModel<string> { Data = "Successfully updated the password"};
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
public Response GetApiKey()
|
||||
{
|
||||
var user = Request.Query["username"];
|
||||
var password = Request.Query["password"];
|
||||
var result = UserMapper.ValidateUser(user, password);
|
||||
var model = new ApiModel<string>();
|
||||
if (result == null)
|
||||
{
|
||||
model.Error = true;
|
||||
model.ErrorMessage = "Incorrect username or password";
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
var settings = SettingsService.GetSettings();
|
||||
model.Data = settings.ApiKey;
|
||||
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
122
Ombi.UI/Modules/Api/BaseApiModule.cs
Normal file
122
Ombi.UI/Modules/Api/BaseApiModule.cs
Normal file
|
@ -0,0 +1,122 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: BaseApiModule.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Nancy;
|
||||
using Nancy.Validation;
|
||||
using Ombi.Core;
|
||||
using Ombi.Core.SettingModels;
|
||||
using Ombi.Store;
|
||||
using Ombi.UI.Models;
|
||||
using ISecurityExtensions = Ombi.Core.ISecurityExtensions;
|
||||
|
||||
namespace Ombi.UI.Modules
|
||||
{
|
||||
public abstract class BaseApiModule : BaseModule
|
||||
{
|
||||
protected BaseApiModule(ISettingsService<PlexRequestSettings> s, ISecurityExtensions security) : base(s,security)
|
||||
{
|
||||
Settings = s;
|
||||
Before += (ctx) => CheckAuth();
|
||||
}
|
||||
|
||||
protected BaseApiModule(string modulePath, ISettingsService<PlexRequestSettings> s, ISecurityExtensions security) : base(modulePath, s, security)
|
||||
{
|
||||
Settings = s;
|
||||
Before += (ctx) => CheckAuth();
|
||||
}
|
||||
|
||||
private ISettingsService<PlexRequestSettings> Settings { get; }
|
||||
|
||||
protected Response ReturnReponse(object result)
|
||||
{
|
||||
var queryString = (DynamicDictionary)Context.Request.Query;
|
||||
dynamic value;
|
||||
if (queryString.TryGetValue("xml", out value))
|
||||
{
|
||||
if ((bool)value)
|
||||
{
|
||||
return Response.AsXml(result);
|
||||
}
|
||||
}
|
||||
return Response.AsJson(result);
|
||||
}
|
||||
|
||||
protected Response ReturnValidationReponse(ModelValidationResult result)
|
||||
{
|
||||
var errors = result.Errors;
|
||||
var model = new ApiModel<List<string>>
|
||||
{
|
||||
Error = true,
|
||||
ErrorMessage = "Please view the error messages inside the data node",
|
||||
Data = new List<string>()
|
||||
};
|
||||
|
||||
foreach (var error in errors)
|
||||
{
|
||||
model.Data.AddRange(error.Value.Select(x => x.ErrorMessage));
|
||||
}
|
||||
|
||||
return ReturnReponse(model);
|
||||
}
|
||||
|
||||
private Response CheckAuth()
|
||||
{
|
||||
if (Request.Path.Contains("api/apikey")) // We do not need the apikey for this call
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var settings = Settings.GetSettings();
|
||||
var apiModel = new ApiModel<List<RequestedModel>> { Data = new List<RequestedModel>() };
|
||||
if (!Authenticated(settings))
|
||||
{
|
||||
apiModel.Error = true;
|
||||
apiModel.ErrorMessage = "ApiKey is invalid or not present, Please use 'apikey' in the querystring.";
|
||||
return ReturnReponse(apiModel);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool Authenticated(PlexRequestSettings settings)
|
||||
{
|
||||
var query = (DynamicDictionary)Context.Request.Query;
|
||||
dynamic key;
|
||||
if (!query.TryGetValue("apikey", out key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if ((string)key == settings.ApiKey)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue