Skip to content

Commit

Permalink
feat: fetch bodytype specific templates and refactor code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
rk132 committed Jun 17, 2024
1 parent dcf3ed6 commit f817e08
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
20 changes: 2 additions & 18 deletions Runtime/AvatarCreator/Scripts/WebRequests/AvatarAPIRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public class AvatarAPIRequests
private const string RPM_AVATAR_V2_BASE_URL = Env.RPM_API_V2_BASE_URL + "avatars";

private const string COLOR_PARAMETERS = "colors?type=skin,beard,hair,eyebrow";
private const string FULL_BODY = "fullbody";
private const string FULL_BODY_XR = "fullbody-xr";
private const string HALF_BODY = "halfbody";
private const string PARTNER = "partner";
private const string DATA = "data";
private const string BODY_TYPE = "bodyType";
Expand Down Expand Up @@ -66,7 +63,7 @@ public async Task<List<AvatarTemplateData>> GetAvatarTemplates()
var response = await authorizedRequest.SendRequest<Response>(
new RequestData
{
Url = $"{RPM_AVATAR_V2_BASE_URL}/templates",
Url = $"{RPM_AVATAR_V2_BASE_URL}/templates?{BODY_TYPE}={CoreSettingsHandler.CoreSettings.BodyType.GetDescription()}",
Method = HttpMethod.GET
},
ctx
Expand All @@ -83,7 +80,7 @@ public async Task<AvatarProperties> CreateFromTemplateAvatar(string templateId,
var payloadData = new Dictionary<string, string>
{
{ nameof(partner), partner },
{ nameof(bodyType), GetBodyTypeValue(bodyType) }
{ nameof(bodyType), bodyType.GetDescription() }
};

var payload = AuthDataConverter.CreatePayload(payloadData);
Expand All @@ -105,19 +102,6 @@ public async Task<AvatarProperties> CreateFromTemplateAvatar(string templateId,
return JsonConvert.DeserializeObject<AvatarProperties>(data);
}

private static string GetBodyTypeValue(BodyType bodyType)
{

var body = bodyType switch
{
BodyType.FullBody => FULL_BODY,
BodyType.FullBodyXR => FULL_BODY_XR,
BodyType.HalfBody => HALF_BODY,
_ => throw new ArgumentOutOfRangeException(nameof(bodyType), bodyType, null)
};
return body;
}

public async Task<AssetColor[]> GetAvatarColors(string avatarId, AssetType assetType = AssetType.None)
{
ValidateAvatarId(avatarId);
Expand Down
11 changes: 11 additions & 0 deletions Runtime/Core/Scripts/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Reflection;

namespace ReadyPlayerMe.Core
{
Expand All @@ -24,5 +25,15 @@ public static T GetValueFromDescription<T>(string description) where T : Enum

return default;
}

public static string GetDescription(this Enum value)
{
var field = value.GetType().GetField(value.ToString());

var attribute = field.GetCustomAttribute<DescriptionAttribute>();

return attribute == null ? value.ToString() : attribute.Description;
}

}
}
13 changes: 1 addition & 12 deletions Runtime/Core/Scripts/JsonConverters/BodyTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ namespace ReadyPlayerMe.Core
[Preserve]
public class BodyTypeConverter : JsonConverter<BodyType>
{
private const string FULL_BODY = "fullbody";
private const string FULL_BODY_XR = "fullbody-xr";
private const string HALF_BODY = "halfbody";

public override void WriteJson(JsonWriter writer, BodyType value, JsonSerializer serializer)
{
serializer.Serialize(writer, value.ToString().ToLower());
Expand All @@ -23,14 +19,7 @@ public override BodyType ReadJson(JsonReader reader, Type objectType, BodyType e
var token = JToken.Load(reader);
if (token.Type == JTokenType.String)
{

return token.ToString() switch
{
FULL_BODY => BodyType.FullBody,
HALF_BODY => BodyType.HalfBody,
FULL_BODY_XR => BodyType.FullBodyXR,
_ => BodyType.None
};
return EnumExtensions.GetValueFromDescription<BodyType>(token.ToString());
}

throw new JsonSerializationException("Expected string value, instead found: " + token.Type);
Expand Down

0 comments on commit f817e08

Please sign in to comment.