Skip to content

UWRH - Unity Web Request Handler 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.

License

Notifications You must be signed in to change notification settings

Sadeqsoli/UWRHandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UWRHandler

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.

openupm

Features

  • 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.

Installation

  1. Download the package from the Releases page.
  2. In Unity, go to Assets > Import Package > Custom Package.
  3. Select the downloaded package and import all files.
  4. (Optional) For more complex JSON handling, install Newtonsoft Json.NET:
    • Open the Unity Package Manager (Window > Package Manager).
    • Click the + button and select Add package from git URL....
    • Enter https://github.com/jilleJr/Newtonsoft.Json-for-Unity.git and click Add.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

Usage

  1. 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}");
			}
		}
	}
}
  1. 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;
		}
	}
}

About

UWRH - Unity Web Request Handler 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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published