RebrickableSharp 是 Rebrickable API 的強型別、易於使用的 C# 用戶端,只需幾行程式碼即可開始使用。它處理身份驗證、錯誤處理以及將 JSON 解析為類型化實例。它支援與.NET 標準 2.0相容的所有 .NET 平台。
查看示範項目以取得功能齊全的範例。
您需要在 Rebrickable 上有一個帳戶。然後,前往帳戶 > 設定 > API並建立一個新金鑰或使用現有金鑰。
請注意,CSV 幫助程式類別不需要使用 API 金鑰。
Install-Package RebrickableSharp
nuget install RebrickableSharp
RebrickableClientConfiguration . Instance . ApiKey = " <Your API Key> " ;
var client = RebrickableClientFactory . Build ( ) ;
// Do stuff
// Client must be disposed properly
client . Dispose ( ) ;
或者,可以使用外部管理的HttpClient :
var httpClient = new HttpClient ( ) ;
var client = RebrickableClientFactory . Build ( httpClient ) ;
// Do stuff
// Client *and* HttpClient must be disposed properly
client . Dispose ( ) ;
httpClient . Dispose ( ) ;
建議在應用程式的整個生命週期中建立並使用一個IRebrickableClient客戶端。
在使用 IoC 容器的應用程式中,您可以將IRebrickableClient註冊為服務並將其註入到消費實例(例如控制器)中。請參閱以下範例將IRebrickableClient註冊為單一實例 (Singleton)。
containerBuilder . Register ( c => RebrickableClientFactory . Build ( ) )
. As < IRebrickableClient > ( )
. SingleInstance ( ) ;
services . AddSingleton ( typeof ( IRebrickableClient ) , provider =>
{
return RebrickableClientFactory . Build ( ) ;
} ) ;
// API
Task < PagedResponse < Part > > GetPartsAsync ( int page = 1 , int pageSize = 100 ,
bool includeDetails = false , string ? bricklinkId = null ,
string ? partNumber = null , IEnumerable < string > ? partNumbers = null ,
int ? categoryId = null , string ? brickOwlId = null ,
string ? legoId = null , string ? lDrawId = null ,
string ? searchTerm = null ,
CancellationToken cancellationToken = default ) ;
// Example
var response = await client . GetPartsAsync ( page : 1 , pageSize : 50 , includeDetails : true , searchTerm : " M-Tron " ) ;
var parts = response . Results ;
// API
Task < Part ? > FindPartByBricklinkIdAsync ( string bricklinkId ,
bool includeDetails = false ,
CancellationToken cancellationToken = default ) ;
// Example
var part = await client . FindPartByBricklinkIdAsync ( " 3005 " , true ) ;
// API
Task < PagedResponse < PartColor > > GetPartColorsAsync ( string partNumber ,
RebrickableCredentials ? credentials = null ,
CancellationToken cancellationToken = default ) ;
// Example
var pagedResult = await client . GetPartColorDetailsAsync ( " 3003 " ) ;
var partColors = pagedResult . Results ;
// API
Task < PartColorDetails > GetPartColorDetailsAsync ( string partNumber , int colorId ,
CancellationToken cancellationToken = default ) ;
// Example
var colorId = 1 ; //Blue
var partColorDetails = await client . GetPartColorDetailsAsync ( " 3005 " , colorId )
// API
Task < PagedResponse < Color > > GetColorsAsync ( int page = 1 , int pageSize = 100 ,
bool includeDetails = false ,
CancellationToken cancellationToken = default ) ;
// Example
var response = await client . GetColorsAsync ( includeDetails : true ,
page : 1 , pageSize : 50 ) ;
var colors = response . Results ;
// API
Task < Color > GetColorAsync ( int colorId , bool includeDetails = false ,
CancellationToken cancellationToken = default ) ;
// Example
var colorId = 0 ; //black
var black = await client . GetColorAsync ( colorId , includeDetails : true ) ;
// API
Task < Element > GetElementAsync ( string elementId ,
CancellationToken cancellationToken = default ) ;
// Example
var elementId = " 300521 " ; //1x1 Brick in Red
var element = await client . GetElementAsync ( elementId ) ;
// API
Task < Minifig > GetMinifigByIdAsync (
string minifigId ,
RebrickableCredentials ? credentials = null ,
CancellationToken cancellationToken = default ) ;
// Example
var minifigId = " fig-000001 " ; //Toy Store Employee, note that minifigId is named 'set_num' in the API docs.
var minifig = await client . GetMinifigByIdAsync ( minifigId ) ;