Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

66 rader
1.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Mvc;
  7. using MQTTnet.Server.Scripting;
  8. using MQTTnet.Server.Web;
  9. namespace MQTTnet.Server.Controllers
  10. {
  11. [Authorize]
  12. [ApiController]
  13. public class ScriptsController : Controller
  14. {
  15. private readonly PythonScriptHostService _pythonScriptHostService;
  16. public ScriptsController(PythonScriptHostService pythonScriptHostService)
  17. {
  18. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  19. }
  20. [Route("api/v1/scripts")]
  21. [HttpGet]
  22. public ActionResult<List<string>> GetScriptUids()
  23. {
  24. return _pythonScriptHostService.GetScriptUids();
  25. }
  26. [Route("api/v1/scripts/uid")]
  27. [HttpGet]
  28. public async Task<ActionResult<string>> GetScript(string uid)
  29. {
  30. var script = await _pythonScriptHostService.ReadScriptAsync(uid, HttpContext.RequestAborted);
  31. if (script == null)
  32. {
  33. return NotFound();
  34. }
  35. return Content(script);
  36. }
  37. [Route("api/v1/scripts/uid")]
  38. [HttpPost]
  39. public Task PostScript(string uid)
  40. {
  41. var code = HttpContext.Request.ReadBodyAsString();
  42. return _pythonScriptHostService.WriteScriptAsync(uid, code, CancellationToken.None);
  43. }
  44. [Route("api/v1/scripts/uid")]
  45. [HttpDelete]
  46. public Task DeleteScript(string uid)
  47. {
  48. return _pythonScriptHostService.DeleteScriptAsync(uid);
  49. }
  50. [Route("api/v1/scripts/initialize")]
  51. [HttpPost]
  52. public Task PostInitializeScripts()
  53. {
  54. return _pythonScriptHostService.TryInitializeScriptsAsync();
  55. }
  56. }
  57. }