92 lines
4.6 KiB
VB.net
92 lines
4.6 KiB
VB.net
Imports System.Configuration
|
|
Imports System.IO
|
|
Imports System.Net
|
|
Imports System.Net.Http
|
|
Imports System.Net.Http.Headers
|
|
Imports System.Net.Http.Json
|
|
Imports Microsoft.Extensions.Configuration
|
|
|
|
Public Class TsNotificacionesClient
|
|
|
|
Private ReadOnly _http As HttpClient
|
|
Private ReadOnly _idAplicacion As Integer
|
|
|
|
Public Sub New(baseUrl As String, idAplicacion As Integer, apiKey As String)
|
|
_idAplicacion = idAplicacion
|
|
ServicePointManager.ServerCertificateValidationCallback = Function(s, c, ch, e) True
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11
|
|
_http = New HttpClient() With {.BaseAddress = New Uri(baseUrl)}
|
|
_http.DefaultRequestHeaders.Add("X-Api-Key", apiKey)
|
|
End Sub
|
|
|
|
Public Shared Async Function RegistrarAsync(titulo As String, descripcion As String, TipoNotificacion As TipoNotificacionEnum, Optional Fichero As Byte() = Nothing) As Task
|
|
Try
|
|
ServicePointManager.ServerCertificateValidationCallback = Function(s, c, ch, e) True
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Tls11
|
|
|
|
'Dim baseUrl = ConfigurationManager.AppSettings("TsNotificaciones:ApiUrl")
|
|
'Dim idAplicacion = Integer.Parse(ConfigurationManager.AppSettings("TsNotificaciones:IdAplicacion"))
|
|
'Dim apiKey = ConfigurationManager.AppSettings("TsNotificaciones:ApiKey")
|
|
'Dim http = New HttpClient() With {.BaseAddress = New Uri(baseUrl)}
|
|
'Dim NombreServidor = ConfigurationManager.AppSettings("TsNotificaciones:ApiKey")
|
|
'If NombreServidor = "" Then NombreServidor = System.Environment.MachineName
|
|
|
|
Dim config = New ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", [optional]:=False).Build()
|
|
Dim apiUrl = If(config("TsNotificaciones:ApiUrl"), "http://localhost:7159")
|
|
Dim idAplicacion = Integer.Parse(If(config("TsNotificaciones:IdAplicacion"), "1"))
|
|
Dim apiKey = If(config("TsNotificaciones:ApiKey"), String.Empty)
|
|
Dim nombreServidor = If(config("TsNotificaciones:NombreServidor"), Environment.MachineName)
|
|
Dim http = New HttpClient() With {.BaseAddress = New Uri(apiUrl)}
|
|
|
|
Dim Tipo = CInt(TipoNotificacion)
|
|
Dim request = New HttpRequestMessage(HttpMethod.Post, "/api/alertas/registrar")
|
|
request.Headers.Add("X-Api-Key", apiKey)
|
|
Dim ipServidor As String = ObtenerIp()
|
|
request.Content = JsonContent.Create(New With {idAplicacion, nombreServidor, ipServidor, titulo, descripcion, Tipo})
|
|
Dim response = Await http.SendAsync(request)
|
|
Dim body As System.Text.Json.JsonElement = Await response.Content.ReadFromJsonAsync(Of System.Text.Json.JsonElement)()
|
|
Dim id = body.GetProperty("id").GetInt32()
|
|
If Fichero IsNot Nothing Then
|
|
Await SubirFichero(http, apiKey, id, Fichero)
|
|
End If
|
|
Catch ex As Exception
|
|
Throw New Exception(ex.Message, ex)
|
|
End Try
|
|
End Function
|
|
|
|
Private Shared Async Function SubirFichero(ByVal http As HttpClient, ByVal apiKey As String, ByVal alertaId As Integer, ByVal datos As Byte()) As Task
|
|
Try
|
|
Dim fileContent = New ByteArrayContent(datos)
|
|
fileContent.Headers.ContentType = New MediaTypeHeaderValue("image/png")
|
|
Dim multipart = New MultipartFormDataContent()
|
|
multipart.Add(fileContent, "archivo", "Imagen.png")
|
|
Dim request = New HttpRequestMessage(HttpMethod.Post, $"/api/alertas/{alertaId}/archivo")
|
|
request.Headers.Add("X-Api-Key", apiKey)
|
|
request.Content = multipart
|
|
Dim response = Await http.SendAsync(request)
|
|
Dim ok As Boolean = response.IsSuccessStatusCode
|
|
If Not ok Then Throw New Exception($"Subida fallida ({response.StatusCode}): {Await response.Content.ReadAsStringAsync()}")
|
|
Catch httpEx As Exception
|
|
Throw New Exception
|
|
End Try
|
|
End Function
|
|
|
|
|
|
|
|
Private Shared Function ObtenerIp() As String
|
|
For Each addr In Dns.GetHostAddresses(Dns.GetHostName())
|
|
If addr.AddressFamily = Sockets.AddressFamily.InterNetwork Then Return addr.ToString()
|
|
Next
|
|
Return "127.0.0.1"
|
|
End Function
|
|
|
|
'Private Shared Function EscaparJson(s As String) As String
|
|
' Return s.Replace("\", "\\").Replace("""", "\""").Replace(vbCr, "\r").Replace(vbLf, "\n")
|
|
'End Function
|
|
Public Enum TipoNotificacionEnum
|
|
INFO = 0
|
|
ADVERTENCIA = 1
|
|
[ERROR] = 2
|
|
CRÍTICO = 3
|
|
End Enum
|
|
End Class |