Agrovision Sows Public API - Technical overview

Created by Lodewijk Lucas, Modified on Wed, 4 Sep at 2:28 PM by Lodewijk Lucas

TABLE OF CONTENTS

Authentication

A JWT access token is required to retrieve data from the Agrovision Sows Public API. The token must be created through the Agrovision Identity Access Management API by calling it with the correct parameters.

The authentication token must be passed in the Authorization header of each call to the API.

At the bottom of this document you will find a code snippet that demonstrates how to get and use the authentication token.


API endpoints

The endpoints which are available through the API can be found in the Swagger documentation of the API:

https://av-pi-sows-public-agw-app.azurewebsites.net/swagger/index.html


API structure

Here you find a picture of how the endpoints in the API are related to each other.


Example code

Here you find a code snippet for a small console application in C# that demonstrates the following functions:

  1. Get an access token from the Identity Access Management API by using the Client ID and Client Secret that Agrovision provided to you
  2. Use the access token to get the list of farms available to you


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace TestPublicAPI
{
    internal class Program
    {
        static async Task GetToken()
        {
            var url = "https://login.agrovision.com/connect/token";
            Console.Write("Client ID: ");
            var client_id = Console.ReadLine();
            Console.Write("Client Secret: ");
            var client_secret = Console.ReadLine();

            var httpclient = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post, url);
            var credential = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes($"{client_id}:{client_secret}"));
            
            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("Authorization", $"Basic {credential}");
            var body = new Dictionary
            {
                {"grant_type", "service_account" },
                {"scope", "PigSows.API" },
            };
            request.Content = new FormUrlEncodedContent(body);
            var response = await httpclient.SendAsync(request);
            var result = "";
            if (response.IsSuccessStatusCode)
            {
                result = await response.Content.ReadAsStringAsync();
            }
            return JsonConvert.DeserializeObject(result);            
        }

        static async Task GetFarms(AccessToken Token)
        {
            if (Token != null)
            {
                var url = "https://av-pi-sows-public-agw-app.azurewebsites.net/farm/search";
                var httpclient = new HttpClient();
                var request = new HttpRequestMessage(HttpMethod.Post, url);
                
                request.Headers.Add("Authorization", $"{Token.Token_type} {Token.Access_token}");
                request.Headers.Add("Accept", "text/plain");
                var body = new JObject
                    (
                        new JProperty("top", 100)
                    );
                request.Content = new StringContent(body.ToString(), Encoding.UTF8, "application/json");
                var response = await httpclient.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    return JArray.Parse(await response.Content.ReadAsStringAsync());
                }
            }
            return null;
        }

        static void Main(string[] args)
        {
            var TokenResult = Task.Run(async () => await GetToken().ConfigureAwait(true)).Result;
            if (TokenResult != null)
            {
                Console.WriteLine($"Token: {TokenResult.Access_token}\r\n\r\nExpires: {DateTime.Now.AddSeconds(TokenResult.Expires_in)}");
                Console.Write("Press Enter"); Console.ReadLine();
                var farms = Task.Run(async () => await GetFarms(TokenResult).ConfigureAwait(true)).Result;
                if (farms != null)
                {
                    Console.WriteLine(farms);
                }
            }
            else
            {
                Console.WriteLine("Cannot obtain Token");
            }
            Console.Write("Press Enter"); Console.ReadLine();
        }

        class AccessToken
        {
            public string Access_token {  get; set; }
            public int Expires_in { get; set; }
            public string Scope { get; set; }
            public string Token_type { get; set; }
        }
    }
}



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article