GithubHelp home page GithubHelp logo

unity3dazure / restclient Goto Github PK

View Code? Open in Web Editor NEW
50.0 6.0 14.0 23 KB

REST Client for Unity with JSON and XML parsing. (Features JSON helper to handle nested arrays and deserializing abstract types)

Home Page: https://deadlyfingers.net

License: MIT License

C# 100.00%
restclient unity jsonutility xml-parsing json-parsing unitywebrequest

restclient's Introduction

REST Client for Unity

For Unity developers looking to use REST Services in their Unity game / app.

RESTClient for Unity is built on top of UnityWebRequest and Unity's JsonUtility to make it easier to compose REST requests and return the results serialized as native C# data model objects.

Features

  • Methods to add request body, headers and query strings to REST requests.
  • Ability to return result as native object or as plain text.
  • Work around for nested arrays.
  • Work around for parsing abstract types on UWP.

How do I use this with cloud services?

Checkout the following projects for Unity which were built using this REST Client library as examples.

Example Usage

This snippet shows how to POST a REST request to a new Azure Function HTTP Trigger "hello" sample function:

using RESTClient;
using System;
public class RESTClientExample : MonoBehaviour {

  private string url = "https://***.azurewebsites.net/api/hello"; // Azure Function API endpoint
  private string code = "***"; // Azure Function code

	void Start () {
		StartCoroutine( SayHello(SayHelloCompleted) );
	}

	private IEnumerator SayHello(Action<IRestResponse<string>> callback = null) {
		RestRequest request = new RestRequest(url, Method.POST);
		request.AddHeader("Content-Type", "application/json");
		request.AddQueryParam("code", code);
		request.AddBody("{\"name\": \"unity\"}");
		yield return request.Request.Send();
		request.GetText(callback);
	}

	private void SayHelloCompleted(IRestResponse<string> response) {
		if (response.IsError) {
			Debug.LogError("Request error: " + response.StatusCode);
			return;
		}
		Debug.Log("Completed: " + response.Content);
	}

}

Requirements

Requires Unity v5.3 or greater as UnityWebRequest and JsonUtility features are used. Unity will be extending platform support for UnityWebRequest so keep Unity up to date if you need to support these additional platforms.

Supported platforms

Intended to work on all the platforms UnityWebRequest supports including:

  • Unity Editor (Mac/PC) and Standalone players
  • iOS
  • Android
  • Windows 10 (UWP)

Troubleshooting

  • Remember to wrap async calls with StartCoroutine()
  • Before building for a target platform remember to check the Internet Client capability is enabled in the Unity player settings, otherwise all REST calls will fail with a '-1' status error.

Questions or tweet @deadlyfingers

restclient's People

Contributors

deadlyfingers avatar rafek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

restclient's Issues

Does this serialize Nullables?

Is this project able to handle Nullables too? If so can you please confirm?

For example : If a rest response includes the following information
decimal ? decimalValue;
double? doubleValue;
float? floatValue;

Can this handle? As I see its using Unity's default deserialisation so wondering if you found a solution for it.

The priority of overloaded method (AddBody) was changed

When I tried StorageServicesDemo with latest RESTClient(4f985e0), an authentication error happend.
https://github.com/Unity3dAzure/StorageServicesDemo

The reason is that this commit(d563bb0) adds an default argument to AddBody.

BlobService.cs calls AddBody method.
https://github.com/Unity3dAzure/StorageServices/blob/f1321b3665839b8906516ca4327e9648c6743851/BlobService.cs#L130

Before this commit(d563bb0), BlobService.cs refers to this method.

public void AddBody(byte[] bytes, string contentType) {

After the commit, however, BlobService.cs refers to this method.
public virtual void AddBody<T>(T data, string contentType = "application/json; charset=utf-8") {

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.