Introduction

The Web API Client is a simple object that allows to send HTTP GET requests with an integrated caching system. The client can be used in all projects, the Unity one and standard .NET ones (for example the Server of the Website). Two implementations exist, one for the Unity project and the other one for the rest of the .NET projects. Although there’s a set of shared interfaces the actual client objects do not have a unified interface. This is because Unity has its own way of providing “async programming”, which is the co-routines.

 

Unity Client

The Unity specific interface is called IUnityWebApiClient which can send an HTTP GET request using the GetAsync() method. This method is intended to be used inside a co-routine as it returns an IEnumerator object. Once the co-routine is completed the Response property of the client can be used to retrieve the response. The caching is implemented internally and the end user doesn’t need to worry about it. Instances of the client should be obtained using the UnityWebApiClientFactory. This factory also exposes a method to get a default instance of the client: GetDefaultClient(). The caching system of the Unity implementation is persisted through game executions. The current system uses the PlayerPrefs feature of Unity (see PlayerPrefsWebApiCacheStorage).

 

.NET Client

All non Unity projects are instead supposed to use the IAsyncWebApiClient interface. Its main method is once again called GetAsync() but it returns a Task object, thus the method can be used with the await operator. The response is returned directly by the method if used with the await operator or it can be retrieved by using the Task.Result property. This client also uses a caching system but the cache is not persisted. To get instances of the client the AsyncWebApiClientFactory has to be used.

 

Caching in the Web API

Caching can be added to any method of the Web API. To do so a method has to be decorated with the [Cache] attribute. This attribute has one constructor parameter, the lifespan of the cached response. This indicates for how much time a client should keep the received response in its internal cache. The parameter can be either an integer, representing milliseconds, or a string that can be parsed into a TimeSpan value. For example the string “00:15:30” indicates a lifespan of 15 minutes and 30 seconds.

Comments

comments

Author danielem.dev
Published
Views 105
War to the Core

Recent Comments

Latest Updates

  • Solo missions

    Solo missions

    Hey guys, here are the specifics of the first two solo missions, those are sketches of the first two missions. First mission: A harvester is placed near the ship, generating supply caches. The player collects energy caches and escapes a ring of mines. No supply links allowed. The sensor component is introduced by one of […]Read More »
  • We made it to the Top 100!

    We made it to the Top 100!

    We are stoked that we managed to land on IndieDB’s Indie of the Year’s Top 100 list! We are also the only MOBA that made it to the list! The support we got from the community was overwhelming, and we would like to thank each and every person that helped us get this far. Whether […]Read More »
  • Full Campaign Series, Version 3

    Full Campaign Series, Version 3

    Characters: Daniel Blaze. Protagonist, player-character. Headstrong and rebellious. Ambiguous loyalty to the Shepherd Coalition. Joseph Blaze. Father of Daniel Blaze. Commander of the Leviathan mothership until executed for treason by the Shepherd Coalition. Sylvia. AI assistant. Provides intel, advice, mission objectives. Captain Zhukov. Cyborg commander assigned to hunt down player during the first several missions. […]Read More »
  • Story: Episode 1, Version 2

    Story: Episode 1, Version 2

    The Blaze family had long been controversial. They supported the Shepherds, but continually frustrated the Orthodox elements of their faction. Joseph Blaze was especially known for his tolerance of the weakling families that used genetic engineering to alleviate the sicknesses that exposure to the Earth’s surface had inflicted on them. Such tensions between orthodoxy and […]Read More »
  • Story: Episode 1

    Story: Episode 1

    Location: The Pole Base. When Daniel Blaze passed his final test and became the primary candidate to pilot the Leviathan, the Shepherds’ newest ship, he caused a quite a stir. Not because he’s the youngest member of the Pole fleet to be nominated for admiralship, the faction had already made peace with this as a […]Read More »
Skip to toolbar