UWRHandler is a Unity package that handles HTTP requests following SOLID principles, providing a clean, maintainable, and extensible way to manage GET and POST requests with support for custom headers and certificates.
- Single Responsibility Principle (SRP): Each class handles a specific responsibility.
- Open/Closed Principle (OCP): Easily extend functionality without modifying existing code.
- Liskov Substitution Principle (LSP): Derived classes are substitutable for their base types.
- Interface Segregation Principle (ISP): Clients depend only on the interfaces they use.
- Dependency Inversion Principle (DIP): Depend on abstractions rather than concrete implementations.
- Download the package from the Releases page.
- In Unity, go to
Assets > Import Package > Custom Package
. - Select the downloaded package and import all files.
- (Optional) For more complex JSON handling, install Newtonsoft Json.NET:
- Open the Unity Package Manager (
Window > Package Manager
). - Click the
+
button and selectAdd package from git URL...
. - Enter
https://github.com/jilleJr/Newtonsoft.Json-for-Unity.git
and clickAdd
.
- Open the Unity Package Manager (
Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.
- Initialize the Request Manager:
using System;
using System.Collections.Generic;
using UnityEngine;
namespace SleeplessDev
{
public class RequestManagerInitializer : MonoBehaviour
{
public KnownSources knownSources;
private IRequestHandler _requestHandler;
public event Action<string> OnResponseReceived;
async void Start()
{
ICertificateValidator certificateValidator;
if (knownSources.sourceMode == SourceMode.Remote)
{
var tempValidator = new CertificateValidator(new List<string>()); // Initialize with an empty list
certificateValidator = await tempValidator.InitializeFromRemote(knownSources.targetUrl);
}
else
{
certificateValidator = new CertificateValidator(knownSources.sources);
}
ICertificateHandler certificateHandler = new CertificateHandler(certificateValidator);
IResponseHandler responseHandler = new ResponseHandler();
ISerializer serializer = new JsonSerializer();
_requestHandler = new RequestManager(certificateHandler, responseHandler, serializer);
}
public async void FetchData()
{
//TODO: add your own url that works and also fill out known sources to accept all certificates https://api.example.com/data
string url = "https://trustedsource.com/api/data";
var response = await _requestHandler.SendGetRequestAsync(url, Header.None);
if (string.IsNullOrEmpty(response.Error))
{
Debug.Log(response.Text);
OnResponseReceived?.Invoke(response.Text);
}
else
{
Debug.LogError(response.Error);
OnResponseReceived?.Invoke($"Error: {response.Error}");
}
}
}
}
- Example Unity Web Request Handler:
using TMPro;
using UnityEngine;
namespace SleeplessDev.Examples
{
public class ExampleUWRHandler : MonoBehaviour
{
public RequestManagerInitializer localRequestManager;
public RequestManagerInitializer remoteRequestManager;
public TextMeshProUGUI responseText;
public void OnLocalButtonClicked()
{
localRequestManager.FetchData();
}
public void OnRemoteButtonClicked()
{
remoteRequestManager.FetchData();
}
void DisplayResponse(string response)
{
responseText.text = response;
}
private void Start()
{
// Attach event listeners
localRequestManager.OnResponseReceived += DisplayResponse;
remoteRequestManager.OnResponseReceived += DisplayResponse;
}
private void OnDestroy()
{
// Detach event listeners
localRequestManager.OnResponseReceived -= DisplayResponse;
remoteRequestManager.OnResponseReceived -= DisplayResponse;
}
}
}