In this guide, we'll walk you through creating an Assistant extension that fetches user information from the Autodesk API using the Pre-Authorized HTTP Client. The Autodesk API provides valuable user data, and we'll demonstrate how to access it seamlessly in your Assistant extension.
To follow along with this tutorial, you can refer to the Autodesk User Information API documentation, which explains the details of the API endpoint we'll be using.
Let's get started with building this extension step by step.
To begin, create an Assistant Extension Task and navigate to the development mode. Then, click the 'Create' button. Use the default template, and name your extension AutodeskUserInfo.
For more detailed instructions on creating an Assistant extension, you can refer to the Create Assistant Extension guide.
Modify the Args class to define the input for your extension. This class will specify the authorized HTTP client and its configuration. Here's an example:
public class AutodeskUserInfoArgs
{
[Authorization(Login.Autodesk), BaseUrl("https://api.userprofile.autodesk.com/")]
public IExtensionHttpClient? AutodeskClient { get; set; }
}
In this class, the [Authorization] attribute is used to specify that the AutodeskClient should use Autodesk login authorization. The [BaseUrl] attribute sets the base URL for the client.
Modify the Command class that implements IAssistantExtension and defines how your extension works. This class will make the HTTP request to the Autodesk API to fetch user information. Here's an example:
public class AutodeskUserInfoCommand : IAssistantExtension<AutodeskUserInfoArgs>
{
public async Task<IExtensionResult> RunAsync(IAssistantExtensionContext context, AutodeskUserInfoArgs args, CancellationToken cancellationToken)
{
if (args.AutodeskClient is not { } autodeskClient)
return Result.Text.Failed("Autodesk Client is required.");
var requestResult = await autodeskClient.GetAsJsonAsync<AutodeskUserInfoResult>("userinfo", cancellationToken);
if (requestResult.StatusCode == System.Net.HttpStatusCode.Unauthorized)
return Result.Text.Failed("You are not logged in! Please log in to Autodesk in Assistant Logins.");
if (requestResult.StatusCode != System.Net.HttpStatusCode.OK)
return Result.Text.Failed($"Request failed to get user info, status code: {requestResult.StatusCode}");
if (requestResult.Result is not {} userInfo)
return Result.Text.Failed("Request failed to get user info, result is null.");
return userInfo;
}
}
In this class:
AutodeskUserInfoCommand class implements the IAssistantExtension interface, indicating that it's an Assistant extension.RunAsync method, it first checks if the AutodeskClient has been configured.Define a result class that represents the response from the Autodesk user info endpoint. Here's an example:
namespace AutodeskUserInfo;
public record AutodeskUserInfoResult(
string sub,
string name,
string given_name,
string family_name,
string preferred_username,
string email,
bool email_verified,
string profile,
string picture,
string locale,
int updated_at
) : IExtensionResult
{
public ExecutionResult Result { get; set; } = ExecutionResult.Succeeded;
public string AsText()
{
return $"Hello, {name}! Your email is {email}.";
}
}
This class should extend IExtensionResult to represent the result. It includes properties that correspond to the JSON response from the Autodesk API.
Ensure you have the required dependencies and build the project. You can also run tests to verify that your extension works correctly.
By following these steps, you've created an Assistant extension that utilizes the Pre-Authorized HTTP Client to access the Autodesk user information endpoint. This is a practical example of how to leverage authentication and HTTP requests in your extensions.