Skip to content

Commit 385f651

Browse files
committed
Improve company valuation
1 parent 0820ee8 commit 385f651

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using FinancialModelingPrepApi.Model;
22
using FinancialModelingPrepApi.Model.CompanyValuation;
3-
using System.Collections.Generic;
43
using System.Threading.Tasks;
54

65
namespace FinancialModelingPrepApi.Abstractions.CompanyValuation
76
{
87
public interface ICompanyValuation
98
{
10-
public Task<ApiResponse<List<CompanyProfileResponse>>> GetCompanyProfileAsync(string symbol);
9+
public Task<ApiResponse<CompanyProfileResponse>> GetCompanyProfileAsync(string symbol);
1110
}
1211
}

FinancialModelingPrepApi/Core/CompanyValuation/CompanyValuation.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using FinancialModelingPrepApi.Model.CompanyValuation;
66
using System.Collections.Generic;
77
using System.Collections.Specialized;
8+
using System.Linq;
89
using System.Threading.Tasks;
910

1011
namespace FinancialModelingPrepApi.Core.CompanyValuation
@@ -18,7 +19,7 @@ public CompanyValuation(FinancialModelingPrepHttpClient client)
1819
this.client = client ?? throw new System.ArgumentNullException(nameof(client));
1920
}
2021

21-
public Task<ApiResponse<List<CompanyProfileResponse>>> GetCompanyProfileAsync(string symbol)
22+
public async Task<ApiResponse<CompanyProfileResponse>> GetCompanyProfileAsync(string symbol)
2223
{
2324
const string url = "[version]/profile/[symbol]";
2425

@@ -28,7 +29,14 @@ public Task<ApiResponse<List<CompanyProfileResponse>>> GetCompanyProfileAsync(st
2829
{ "symbol", symbol }
2930
};
3031

31-
return client.GetAsync<List<CompanyProfileResponse>>(url, pathParams, null);
32+
var result = await client.GetAsync<List<CompanyProfileResponse>>(url, pathParams, null);
33+
34+
if (result.HasError)
35+
{
36+
return ApiResponse.FromError<CompanyProfileResponse>(result.Error);
37+
}
38+
39+
return ApiResponse.FromSucces(result.Data.First());
3240
}
3341
}
3442
}

FinancialModelingPrepApi/Core/Http/FinancialModelingPrepHttpClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FinancialModelingPrepApi.Model;
2+
using FinancialModelingPrepApi.Model.Error;
23
using System;
34
using System.Collections.Specialized;
45
using System.Net.Http;
@@ -31,6 +32,19 @@ public async Task<ApiResponse<T>> GetAsync<T>(string urlPattern, NameValueCollec
3132
try
3233
{
3334
var response = await CallApiAsync(urlPattern, pathParams, queryString);
35+
36+
if (response.HasError)
37+
{
38+
ApiResponse.FromError<T>(response.Data);
39+
}
40+
41+
if (response.Data.Contains("Error Message"))
42+
{
43+
var errorData = JsonSerializer.Deserialize<ErrorResponse>(response.Data);
44+
45+
return ApiResponse.FromError<T>(errorData.ErrorMessage);
46+
}
47+
3448
var data = JsonSerializer.Deserialize<T>(response.Data, jsonSerializerOptions);
3549

3650
return ApiResponse.FromSucces(data);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace FinancialModelingPrepApi.Model.Error
4+
{
5+
public class ErrorResponse
6+
{
7+
[JsonPropertyName("Error Message")]
8+
public string ErrorMessage { get; set; }
9+
}
10+
}

Tests/CompanyValuation/CompanyValuationTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,22 @@ public async Task GetCompanyProfileTests()
1818
{
1919
var api = ServiceProvider.GetRequiredService<IFinancialModelingPrepApiClient>();
2020

21-
var result = await api.CompanyValuation.GetCompanyProfileAsync("AAPL,MSFT");
21+
var result = await api.CompanyValuation.GetCompanyProfileAsync("AAPL");
2222

2323
Assert.NotNull(result);
2424
Assert.False(result.HasError);
25+
Assert.Equal("AAPL", result.Data.symbol);
26+
}
27+
28+
[Fact]
29+
public async Task GetCompanyProfile_Unknown_Symbol_Returns_HasError_True()
30+
{
31+
var api = ServiceProvider.GetRequiredService<IFinancialModelingPrepApiClient>();
32+
33+
var result = await api.CompanyValuation.GetCompanyProfileAsync("doesnotexist");
34+
35+
Assert.NotNull(result);
36+
Assert.True(result.HasError);
2537
}
2638
}
2739
}

0 commit comments

Comments
 (0)