ArchiSteamFarm/IPC/Controllers/Api/TypeController.cs
/// <summary>
///     Fetches type info of given type.
/// </summary>
/// <remarks>
///     Type info is defined as a representation of given object with its fields and properties being assigned to a string value that defines their type.
/// </remarks>
Fetches type info of given type.
Fetches type info of given type .
[HttpGet("{type:required}")]
	[ProducesResponseType(typeof(GenericResponse<TypeResponse>), (int) HttpStatusCode.OK)]
	[ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)]
	public ActionResult<GenericResponse> TypeGet(string type) {
		if (string.IsNullOrEmpty(type)) {
			throw new ArgumentNullException(nameof(type));
		}
		Type? targetType = WebUtilities.ParseType(type);
		if (targetType == null) {
			return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, type)));
		}
		string? baseType = targetType.BaseType?.GetUnifiedName();
		HashSet<string> customAttributes = targetType.CustomAttributes.Select(static attribute => attribute.AttributeType.GetUnifiedName()).Where(static customAttribute => !string.IsNullOrEmpty(customAttribute)).ToHashSet(StringComparer.Ordinal)!;
		string? underlyingType = null;
		Dictionary<string, string> body = new(StringComparer.Ordinal);
		if (targetType.IsClass) {
			foreach (FieldInfo field in targetType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(static field => !field.IsPrivate)) {
				JsonPropertyAttribute? jsonProperty = field.GetCustomAttribute<JsonPropertyAttribute>();
				if (jsonProperty != null) {
					string? unifiedName = field.FieldType.GetUnifiedName();
					if (!string.IsNullOrEmpty(unifiedName)) {
						body[jsonProperty.PropertyName ?? field.Name] = unifiedName!;
					}
				}
			}
			foreach (PropertyInfo property in targetType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(static property => property.CanRead && (property.GetMethod?.IsPrivate == false))) {
				JsonPropertyAttribute? jsonProperty = property.GetCustomAttribute<JsonPropertyAttribute>();
				if (jsonProperty != null) {
					string? unifiedName = property.PropertyType.GetUnifiedName();
					if (!string.IsNullOrEmpty(unifiedName)) {
						body[jsonProperty.PropertyName ?? property.Name] = unifiedName!;
					}
				}
			}
		} else if (targetType.IsEnum) {
			Type enumType = Enum.GetUnderlyingType(targetType);
			underlyingType = enumType.GetUnifiedName();
			foreach (object? value in Enum.GetValues(targetType)) {
				string? valueText = value?.ToString();
				if (string.IsNullOrEmpty(valueText)) {
					ASF.ArchiLogger.LogNullError(nameof(valueText));
					return BadRequest(new GenericResponse(false, string.Format(CultureInfo.CurrentCulture, Strings.ErrorObjectIsNull, nameof(valueText))));
				}
				string? valueObjText = Convert.ChangeType(value, enumType, CultureInfo.InvariantCulture)?.ToString();
				if (string.IsNullOrEmpty(valueObjText)) {
					continue;
				}
				body[valueText!] = valueObjText!;
			}
		}
		TypeProperties properties = new(baseType, customAttributes.Count > 0 ? customAttributes : null, underlyingType);
		TypeResponse response = new(body, properties);
		return Ok(new GenericResponse<TypeResponse>(response));
	}
[ HttpGet ( " " ) ] [ ProducesResponseType ( typeof ( GenericResponse < TypeResponse > ) , ( int ) HttpStatusCode . OK ) ] [ ProducesResponseType ( typeof ( GenericResponse ) , ( int ) HttpStatusCode . BadRequest ) ] public ActionResult < GenericResponse > TypeGet ( string type ) { if ( string . IsNullOrEmpty ( type ) ) { throw new ArgumentNullException ( nameof ( type ) ) ; } Type ? targetType = WebUtilities . ParseType ( type ) ; if ( targetType == null ) { return BadRequest ( new GenericResponse ( false , string . Format ( CultureInfo . CurrentCulture , Strings . ErrorIsInvalid , type ) ) ) ; } string ? baseType = targetType . BaseType ? . GetUnifiedName ( ) ; HashSet < string > customAttributes = targetType . CustomAttributes . Select ( static attribute => attribute . AttributeType . GetUnifiedName ( ) ) . Where ( static customAttribute => ! string . IsNullOrEmpty ( customAttribute ) ) . ToHashSet ( StringComparer . Ordinal ) ! ; string ? underlyingType = null ; Dictionary < string , string > body = new ( StringComparer . Ordinal ) ; if ( targetType . IsClass ) { foreach ( FieldInfo field in targetType . GetFields ( BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ) . Where ( static field => ! field . IsPrivate ) ) { JsonPropertyAttribute ? jsonProperty = field . GetCustomAttribute < JsonPropertyAttribute > ( ) ; if ( jsonProperty != null ) { string ? unifiedName = field . FieldType . GetUnifiedName ( ) ; if ( ! string . IsNullOrEmpty ( unifiedName ) ) { body [ jsonProperty . PropertyName ?? field . Name ] = unifiedName ! ; } } } foreach ( PropertyInfo property in targetType . GetProperties ( BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ) . Where ( static property => property . CanRead && ( property . GetMethod ? . IsPrivate == false ) ) ) { JsonPropertyAttribute ? jsonProperty = property . GetCustomAttribute < JsonPropertyAttribute > ( ) ; if ( jsonProperty != null ) { string ? unifiedName = property . PropertyType . GetUnifiedName ( ) ; if ( ! string . IsNullOrEmpty ( unifiedName ) ) { body [ jsonProperty . PropertyName ?? property . Name ] = unifiedName ! ; } } } } else if ( targetType . IsEnum ) { Type enumType = Enum . GetUnderlyingType ( targetType ) ; underlyingType = enumType . GetUnifiedName ( ) ; foreach ( object ? value in Enum . GetValues ( targetType ) ) { string ? valueText = value ? . ToString ( ) ; if ( string . IsNullOrEmpty ( valueText ) ) { ASF . ArchiLogger . LogNullError ( nameof ( valueText ) ) ; return BadRequest ( new GenericResponse ( false , string . Format ( CultureInfo . CurrentCulture , Strings . ErrorObjectIsNull , nameof ( valueText ) ) ) ) ; } string ? valueObjText = Convert . ChangeType ( value , enumType , CultureInfo . InvariantCulture ) ? . ToString ( ) ; if ( string . IsNullOrEmpty ( valueObjText ) ) { continue ; } body [ valueText ! ] = valueObjText ! ; } } TypeProperties properties = new ( baseType , customAttributes . Count > 0 ? customAttributes : null , underlyingType ) ; TypeResponse response = new ( body , properties ) ; return Ok ( new GenericResponse < TypeResponse > ( response ) ) ; }
Fetches type info of given type.
Fetches type info of given type .
// ReSharper disable once RedundantSuppressNullableWarningExpression - required for .NET Framework // ReSharper disable once RedundantSuppressNullableWarningExpression - required for .NET Framework // ReSharper disable RedundantSuppressNullableWarningExpression - required for .NET Framework // ReSharper restore RedundantSuppressNullableWarningExpression - required for .NET Framework
type
string
remarks
Type info is defined as a representation of given object with its fields and properties being assigned to a string value that defines their type.
Type info is defined as a representation of given object with its fields and properties being assigned to a string value that defines their type .