ArchiSteamFarm/ArchiSteamFarm.CustomPlugins.ExamplePlugin/CatController.cs

58 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-03-16 23:06:13 +00:00
// ----------------------------------------------------------------------------------------------
2019-04-11 12:04:58 +00:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
2024-03-16 23:06:13 +00:00
// ----------------------------------------------------------------------------------------------
2024-03-17 01:35:40 +00:00
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
2019-04-11 12:04:58 +00:00
// Contact: JustArchi@JustArchi.net
2024-03-17 01:35:40 +00:00
// |
2019-04-11 12:04:58 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2024-03-17 01:35:40 +00:00
// |
2019-04-11 12:04:58 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-03-17 01:35:40 +00:00
// |
2019-04-11 12:04:58 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
2019-04-11 12:04:58 +00:00
using System.Net;
2023-11-14 20:20:51 +00:00
using System.Threading;
2019-04-11 12:04:58 +00:00
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
2019-04-11 12:04:58 +00:00
using ArchiSteamFarm.IPC.Controllers.Api;
using ArchiSteamFarm.IPC.Responses;
using Microsoft.AspNetCore.Mvc;
2021-11-10 20:23:24 +00:00
namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin;
2021-11-10 20:23:24 +00:00
// This is an example class which shows you how you can extend ASF's API with your own custom API routes and controllers
// You're free to decide whether you want to integrate with existing ASF concepts (such as ArchiController/GenericResponse), or roll out your own
// All API controllers will be discovered during our Kestrel initialization using attributes mapping, you're also getting usual ASF goodies such as swagger documentation out of the box
[Route("/Api/Cat")]
public sealed class CatController : ArchiController {
/// <summary>
/// Fetches URL of a random cat picture.
/// </summary>
[HttpGet]
[ProducesResponseType<GenericResponse<Uri>>((int) HttpStatusCode.OK)]
[ProducesResponseType<GenericResponse>((int) HttpStatusCode.ServiceUnavailable)]
2021-11-10 20:23:24 +00:00
public async Task<ActionResult<GenericResponse>> CatGet() {
if (ASF.WebBrowser == null) {
throw new InvalidOperationException(nameof(ASF.WebBrowser));
2019-04-11 12:04:58 +00:00
}
2021-11-10 20:23:24 +00:00
2023-11-14 20:20:51 +00:00
CancellationToken cancellationToken = HttpContext.RequestAborted;
Uri? url = await CatAPI.GetRandomCatURL(ASF.WebBrowser, cancellationToken).ConfigureAwait(false);
2021-11-10 20:23:24 +00:00
2022-06-24 21:19:31 +00:00
return url != null ? Ok(new GenericResponse<Uri>(url)) : StatusCode((int) HttpStatusCode.ServiceUnavailable, new GenericResponse(false));
2019-04-11 12:04:58 +00:00
}
}