Inside operations

Last updated 6 months ago

WAK operations define everything about an HTTP call. The information on how to structure the class comes from the API’s documentation.

Let’s take a look at one of Instagram’s endpoints.

Instagram Endpoint

Let’s take a look at the URI: https://api.instagram.com/v1/tags/{tag-name}/media/recent?access_token=ACCESS-TOKEN&count=100

Base URI

Resource Path

Query String

https://api.instagram.com/v1/

tags/{tag-name}/media/recent

?access_token=ACCESS-TOKEN&count=10

This is our entry point into Intagram’s version 1 API.

Notice the {tag-name}. This is a URI Template that must be replaced and resolved before making the request.

Parameters as part of the URI.

From this information we can begin constructing our operation class. Any operation class must inherit from hg.ApiWebKit.core.http.HttpOperation.

[HttpPath(null,"https://api.instagram.com/v1/tags/{$tag}/media/recent")]
[HttpGET]
public class InstagramRecentMediaForTag : hg.ApiWebKit.core.http.HttpOperation
{
[HttpUriSegment("tag")]
public string Tag = "gaming";
[HttpQueryString("count")]
public int Count = 10;
[HttpQueryString]
public string access_token = "abc123";
}

Notice that we are using C# attributes everywhere. At runtime the URI in the [HttpPath] attribute will be expanded by using the values from the class fields and will look like this: https://api.instagram.com/v1/tags/gaming/media/recent?access_token=abc123&count=10

Most attribute constructors in this example accept optional parameters and are really just key-value pairs. By using the parameterless constructor you are defining the ‘Name’ to be the same as the field name, as seen in the ‘access_token’ field.

  • Name: the name of the parameter. For example, the ‘Count’ field as is will evaluate to query string ‘count=10’.

  • Value: static value of the parameter. Instead of using the value of the ‘Count’ field during evaluation, you can force it to use a static value. This would look like [HttpQueryString("count", Value= "10")].

  • VariableName: name of the parameter resolved from settings in the Configuration class.

  • VariableValue: value of the parameter resolved from settings in the Configuration class.

Now we are ready to test our operation. By adding Configuration.SetSetting ("log-VERBOSE", true); to the beginning of the Start method, you will see copious amounts of information in the Unity Console.

Calling Instagram Endpoint

If you have entered a valid access token you should see these entries in Unity’s Console breaking down the request and responses.

Request
Response

The returned text is raw JSON and is pretty useless to us at this point.

Response Data

In the next post we will take a look on how to make this JSON data accessible to us in Unity.