Skip to content

Instantly share code, notes, and snippets.

@jweisman
Last active May 1, 2018 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jweisman/c31048c7a88bd5e7580763b465a0de7a to your computer and use it in GitHub Desktop.
Save jweisman/c31048c7a88bd5e7580763b465a0de7a to your computer and use it in GitHub Desktop.
Refit for Alma APIs
using Newtonsoft.Json;
using Refit;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace refitalma
{
[Headers("Accept: application/json")]
public interface IAlmaApi
{
[Get("/almaws/v1/users/{userName}")]
Task<AlmaUser> GetUser(string userName, [Header("Authorization")] string authorization);
}
public class AlmaUser
{
[JsonProperty(PropertyName = "first_name")]
public string firstName {get; set;}
[JsonProperty(PropertyName = "last_name")]
public string lastName {get; set;}
}
class Program
{
public static string authHeader =
"apikey " + Environment.GetEnvironmentVariable("ALMA_APIKEY");
public static async Task Main(string[] args)
{
var almaApi = RestService.For<IAlmaApi>("https://api-na.hosted.exlibrisgroup.com");
var user = await almaApi.GetUser(args[0], authHeader);
Console.WriteLine(
String.Format("Successfully retrieved user: {0} ({1} {2})\n",
args[0], user.firstName, user.lastName)
);
}
}
}
@jweisman
Copy link
Author

jweisman commented May 1, 2018

To use:

dotnet new console
dotnet add package refit
wget https://gist.github.com/jweisman/c31048c7a88bd5e7580763b465a0de7a/raw/26cb60b4df1235d1c5bddeecc24d94f1afae2407/Program.cs -O Program.cs
export ALMA_APIKEY={API KEY}
dotnet run {user}

You may need to add <LangVersion>7.1</LangVersion> to the <PropertyGroup> in the .csproj.

See this blog for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment