Skip to content

Commit

Permalink
Added GetColorsAsync GetColorAsync GetPartsAsync GetElementAsync methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gebirgslok committed Feb 6, 2022
1 parent cbccabe commit 449ed38
Show file tree
Hide file tree
Showing 26 changed files with 1,214 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.cs]
csharp_style_namespace_declarations=file_scoped:suggestion
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# RebrickableSharp
Easy-to-use C# client for the Rebrickable (LEGO) API.

47 changes: 47 additions & 0 deletions RebrickableSharp.Client/Color.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Text.Json.Serialization;
using RebrickableSharp.Client.Json;

namespace RebrickableSharp.Client;

public class Color
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; } = null!;

[JsonPropertyName("rgb"), JsonConverter(typeof(HtmlCodeConverter))]
public string HtmlCode { get; set; } = null!;

[JsonPropertyName("is_trans")]
public bool IsTransparent { get; set; }

[JsonPropertyName("external_ids")]
public ExternalColorData ExternalData { get; set; } = new();
}
49 changes: 49 additions & 0 deletions RebrickableSharp.Client/Element.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Text.Json.Serialization;

namespace RebrickableSharp.Client;

public class Element
{
[JsonPropertyName("part")]
public Part Part { get; set; } = null!;

[JsonPropertyName("color")]
public Color Color { get; set; } = null!;

[JsonPropertyName("element_id")]
public string ElementId { get; set; } = null!;

[JsonPropertyName("design_id")]
public string DesignId { get; set; } = null!;

[JsonPropertyName("element_img_url")]
public string? ElementImageUrl { get; set; }

[JsonPropertyName("part_img_url")]
public string? partImageUrl { get; set; }
}
34 changes: 34 additions & 0 deletions RebrickableSharp.Client/Extensions/BooleanExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

namespace RebrickableSharp.Client.Extensions;

internal static class BooleanExtensions
{
public static string ToQueryParam(this bool b)
{
return b ? "1" : "0";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Collections.Specialized;

namespace RebrickableSharp.Client.Extensions;

internal static class NameValueCollectionExtensions
{
public static void AddIfNotNull<T>(this NameValueCollection collection, string key, T value, Func<T, string>? toString = null)
{
if (value != null)
{
collection[key] = toString == null ? value.ToString() : toString.Invoke(value);
}
}
}
46 changes: 46 additions & 0 deletions RebrickableSharp.Client/ExternalColorData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Text.Json.Serialization;

namespace RebrickableSharp.Client;

public class ExternalColorData
{
[JsonPropertyName("LDraw")]
public ExternalColorIds LDraw { get; set; } = new();

[JsonPropertyName("BrickLink")]
public ExternalColorIds BrickLink { get; set; } = new();

[JsonPropertyName("BrickOwl")]
public ExternalColorIds BrickOwl { get; set; } = new();

[JsonPropertyName("LEGO")]
public ExternalColorIds Lego { get; set; } = new();

[JsonPropertyName("Peeron")]
public ExternalColorIds Peeron { get; set; } = new();
}
37 changes: 37 additions & 0 deletions RebrickableSharp.Client/ExternalColorIds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Text.Json.Serialization;

namespace RebrickableSharp.Client;

public class ExternalColorIds
{
[JsonPropertyName("ext_ids")]
public int?[] Ids { get; set; } = Array.Empty<int?>();

[JsonPropertyName("ext_descrs")]
public string[][] Descriptions { get; set; } = Array.Empty<string[]>();
}
84 changes: 84 additions & 0 deletions RebrickableSharp.Client/ExternalPartIds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#region License
// Copyright (c) 2022 Jens Eisenbach
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Text.Json.Serialization;

namespace RebrickableSharp.Client;

public class ExternalPartIds
{
private class ExternalPartIdStringBuilder
{
private readonly IDictionary<string, int> _externalIdCount = new Dictionary<string, int>();

public void Append(string[] ids, string externalSource)
{
if (ids.Any())
{
_externalIdCount.Add(externalSource, ids.Length);
}
}

public override string ToString()
{
if (!_externalIdCount.Any())
{
return "No external IDs available";
}

return string.Join(",", _externalIdCount.Select(kvp => $"{kvp.Key}:{kvp.Value}"));
}
}

[JsonPropertyName("BrickLink")]
public string[] BricklinkIds { get; set; } = Array.Empty<string>();

[JsonPropertyName("BrickOwl")]
public string[] BrickOwlIds { get; set; } = Array.Empty<string>();

[JsonPropertyName("Brickset")]
public string[] BricksetIds { get; set; } = Array.Empty<string>();

[JsonPropertyName("LEGO")]
public string[] LegoIds { get; set; } = Array.Empty<string>();

[JsonPropertyName("LDraw")]
public string[] LDrawIds { get; set; } = Array.Empty<string>();

[JsonPropertyName("Peeron")]
public string[] PeeronIds { get; set; } = Array.Empty<string>();

public override string ToString()
{
var builder = new ExternalPartIdStringBuilder();
builder.Append(BricklinkIds, "BrickLink");
builder.Append(BrickOwlIds, "BrickOwl");
builder.Append(BricksetIds, "Brickset");
builder.Append(LegoIds, "LEGO");
builder.Append(LDrawIds, "LDraw");
builder.Append(PeeronIds, "Peeron");
return builder.ToString();
}
}
4 changes: 4 additions & 0 deletions RebrickableSharp.Client/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<NullGuard IncludeDebugAssert="false"
Mode="NullableReferenceTypes" />
</Weavers>
Loading

0 comments on commit 449ed38

Please sign in to comment.