Last active
May 1, 2018 17:04
-
-
Save jweisman/c31048c7a88bd5e7580763b465a0de7a to your computer and use it in GitHub Desktop.
Refit for Alma APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
You may need to add
<LangVersion>7.1</LangVersion>
to the<PropertyGroup>
in the.csproj
.See this blog for more details.