-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rest client which will be needed for Attachments
- Loading branch information
Antonio Zhu
committed
Dec 7, 2022
1 parent
1ca3335
commit bfee073
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Rocket.Chat.Net.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using RestSharp; | ||
using NLog; | ||
using WebSocketSharp; | ||
using RestSharp.Authenticators; | ||
using NLog.LayoutRenderers.Wrappers; | ||
|
||
namespace Rocket.Chat.Net.Driver | ||
{ | ||
public class RestClient : IRestClient | ||
{ | ||
public RestSharp.RestClient _client; | ||
private IAuthenticator _authenticator; | ||
private bool _isLoggedIn; | ||
ILogger _logger; | ||
|
||
public RestClient(string instanceUrl, ILogger logger) | ||
{ | ||
_logger = logger; | ||
_client = new RestSharp.RestClient(instanceUrl + "/api/v1/"); | ||
} | ||
|
||
private bool disposedValue; | ||
|
||
public string Url => throw new NotImplementedException(); | ||
|
||
public bool IsDisposed => throw new NotImplementedException(); | ||
|
||
public bool IsLoggedIn { get => _isLoggedIn; } | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="method">HTTP method, such as GET, POST, PUT etc.</param> | ||
/// <param name="path"></param> | ||
/// <param name="token"></param> | ||
/// <param name="args"></param> | ||
/// <returns></returns> | ||
public async Task<JObject> CallAsync(string method, string path, CancellationToken token, params object[] args) | ||
{ | ||
var request = new RestRequest(path, (Method) Enum.Parse(typeof(Method), method)); | ||
JObject data = JObject.FromObject(args); | ||
if (data != null) | ||
{ | ||
request.AddBody(data); | ||
} | ||
var response = await _client.ExecuteAsync(request).ConfigureAwait(false); | ||
return JObject.FromObject(response.Content); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!disposedValue) | ||
{ | ||
if (disposing) | ||
{ | ||
// TODO: Verwalteten Zustand (verwaltete Objekte) bereinigen | ||
_client.Dispose(); | ||
} | ||
|
||
// TODO: Nicht verwaltete Ressourcen (nicht verwaltete Objekte) freigeben und Finalizer überschreiben | ||
// TODO: Große Felder auf NULL setzen | ||
disposedValue = true; | ||
} | ||
} | ||
|
||
// // TODO: Finalizer nur überschreiben, wenn "Dispose(bool disposing)" Code für die Freigabe nicht verwalteter Ressourcen enthält | ||
// ~RestClient() | ||
// { | ||
// // Ändern Sie diesen Code nicht. Fügen Sie Bereinigungscode in der Methode "Dispose(bool disposing)" ein. | ||
// Dispose(disposing: false); | ||
// } | ||
|
||
public void Dispose() | ||
{ | ||
// Ändern Sie diesen Code nicht. Fügen Sie Bereinigungscode in der Methode "Dispose(bool disposing)" ein. | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public async Task LoginAsync(object args) | ||
{ | ||
JObject response = await CallAsync("POST", "login", CancellationToken.None, args).ConfigureAwait(false); | ||
if (response["status"].ToObject<String>() == "success") | ||
{ | ||
string authToken = response["status"]["authToken"].ToObject<String>(); | ||
string userId = response["status"]["userId"].ToObject<String>(); | ||
_client.Authenticator = new RocketAuthenticator(userId, authToken); | ||
_isLoggedIn = true; | ||
} | ||
} | ||
} | ||
|
||
public class RocketAuthenticator : IAuthenticator | ||
{ | ||
private string authToken; | ||
private string userId; | ||
|
||
public RocketAuthenticator(string userId, string authToken) | ||
{ | ||
this.authToken = authToken; | ||
this.userId = userId; | ||
} | ||
|
||
public ValueTask Authenticate(RestSharp.RestClient client, RestRequest request) | ||
{ | ||
request.AddHeader("X-Auth-Token", authToken); | ||
request.AddHeader("X-User-Id", userId); | ||
return new ValueTask(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Rocket.Chat.Net/Interfaces/Driver/IRocketRestClientManagement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Rocket.Chat.Net.Interfaces.Driver | ||
{ | ||
public interface IRocketRestClientManagement | ||
{ | ||
Task LoginRestApi(object loginArgs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
|
||
namespace Rocket.Chat.Net.Interfaces | ||
{ | ||
public interface IRestClient : IDisposable | ||
{ | ||
string Url { get; } | ||
bool IsDisposed { get; } | ||
Task<JObject> CallAsync(string method, string path, CancellationToken token, params object[] args); | ||
Task LoginAsync(object args); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters