kevbite / CompaniesHouse.NET

A simple .NET client wrapper for CompaniesHouse API
MIT License
37 stars 44 forks source link

Is there a quick manner to get the source values as returned by the API - instead of the enum values? #156

Open dm007 opened 3 years ago

dm007 commented 3 years ago

Is there a way to retrieve the source value as returned by the API instead of the enum values or look to include the following availalbe from Companies House documentation for proper mapping?

https://raw.githubusercontent.com/companieshouse/api-enumerations/master/constants.yml

For example: Company Profile > Company Type: Source from API: "ltd" enum from the library: "Ltd" The mapping from the constants.yml and as it appears on the Companies House site: "Private limited company"

Company Profile > Jurisdiction: source from API: "england-wales" enum from the library: "EnglandAndWales" The mapping from the constants.yml and as it appears on the Companies House site: "England/Wales"

kevbite commented 3 years ago

Hey, @dm007 sorry about the late reply, originally there was no constants.yml files when we started this project, also it seems that they are also slower at updating the yaml files than their API as we've implemented new enumerations before they've been listed too (See https://github.com/kevbite/CompaniesHouse.NET/issues/149). This could cause some issues down the line.

I'd say the library doesn't really support the display name of the enum values, as the actual Enum values are just representations for the options (also we've not been able to have an enum of "England/Wales" as that's not supported by the language.

Honestly, it'd be a nice feature to add in and I think it would be possible by adding some [DisplayName] attributes to all the enums.

I'd imagine the code to look something like the following:

var result = await client.GetCompanyProfileAsync("10440441");

// EnglandAndWales
var jurisdiction = result.Data.Jurisdiction;
Console.WriteLine(jurisdiction);

// England/Wales
var displayName = result.Data.GetDisplayName(x =>x.Jurisdiction);
Console.WriteLine(displayName);

Alternately you could get the raw value returned back from the API and map it yourself and map it directly in your own code. This can be done with a bit of reflection magic (https://dotnetfiddle.net/TjAyaH):

// Actually get value from API
var companyProfile = new
{
    Jurisdiction = Jurisdiction.EnglandAndWales
};

var enumType = typeof(Jurisdiction);
var memberInfos = enumType.GetMember(companyProfile.Jurisdiction.ToString());
var enumValueMemberInfo = memberInfos[0];
var valueAttributes = enumValueMemberInfo.GetCustomAttributes(typeof(EnumMemberAttribute), false);
var rawValue = ((EnumMemberAttribute)valueAttributes[0]).Value;

// england-wales
Console.WriteLine(rawValue);
dm007 commented 3 years ago

Thanks @kevbite. I'm using reflection right now in my poc to lookup the values as returned by the API. I will keep an eye here to see if/when DisplayName gets added, and can update as necessary.

This is a great library though, as it makes it quick and easy to fetch data from Companies House.

note: By the way, I'm trying to figure out a way to fetch document metadata and document pdf, as the document id seems to be issue and nothing seems to be working so far. Going to keep looking when I get chance, and hopefully, get figure out the gaps/issues. Thanks again.

kevbite commented 3 years ago

@dm007 glad the library is useful, if you fancy forking the code and pushing the changes in for the DisplayName stuff I'll happily accept it into the codebase. (I'm just fairly busy at the moment to implement it).

I've raised an issue here for you to track - https://github.com/kevbite/CompaniesHouse.NET/issues/157

Not sure what's going off with the document metadata, we do have some tests in the codebase that show them working which might be useful? https://github.com/kevbite/CompaniesHouse.NET/blob/master/src/CompaniesHouse.IntegrationTests/Tests/DocumentTests/DocumentMetadataTestsValid.cs

dm007 commented 3 years ago

@kevbite - I will try to have a look at it over the next few days. Thanks for the pointer to the documents, will need to check it further, as nothing worked based on IDs and URL we receive as part of the filing history results.

Thanks!