Skip to content

Commit 0820ee8

Browse files
committed
Finish GetCompanyProfileAsync
1 parent 9c0d8c6 commit 0820ee8

File tree

12 files changed

+131
-14
lines changed

12 files changed

+131
-14
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using FinancialModelingPrepApi.Model;
22
using FinancialModelingPrepApi.Model.CompanyValuation;
3+
using System.Collections.Generic;
34
using System.Threading.Tasks;
45

56
namespace FinancialModelingPrepApi.Abstractions.CompanyValuation
67
{
78
public interface ICompanyValuation
89
{
9-
public Task<ApiResponse<CompanyProfileResponse>> GetCompanyProfileAsync(string symbol);
10+
public Task<ApiResponse<List<CompanyProfileResponse>>> GetCompanyProfileAsync(string symbol);
1011
}
1112
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
namespace FinancialModelingPrepApi.Abstractions
1+
using FinancialModelingPrepApi.Abstractions.CompanyValuation;
2+
3+
namespace FinancialModelingPrepApi
24
{
35
public interface IFinancialModelingPrepApiClient
46
{
7+
ICompanyValuation CompanyValuation { get; }
58
}
69
}

FinancialModelingPrepApi/Core/CompanyValuation/CompanyValuation.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using FinancialModelingPrepApi.Core.Http;
44
using FinancialModelingPrepApi.Model;
55
using FinancialModelingPrepApi.Model.CompanyValuation;
6+
using System.Collections.Generic;
67
using System.Collections.Specialized;
78
using System.Threading.Tasks;
89

@@ -17,7 +18,7 @@ public CompanyValuation(FinancialModelingPrepHttpClient client)
1718
this.client = client ?? throw new System.ArgumentNullException(nameof(client));
1819
}
1920

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

@@ -27,7 +28,7 @@ public Task<ApiResponse<CompanyProfileResponse>> GetCompanyProfileAsync(string s
2728
{ "symbol", symbol }
2829
};
2930

30-
return client.GetAsync<CompanyProfileResponse>(url, pathParams, null);
31+
return client.GetAsync<List<CompanyProfileResponse>>(url, pathParams, null);
3132
}
3233
}
3334
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
using FinancialModelingPrepApi.Abstractions;
1+
using FinancialModelingPrepApi.Abstractions.CompanyValuation;
22

33
namespace FinancialModelingPrepApi.Core
44
{
55
public class FinancialModelingPrepApiClient : IFinancialModelingPrepApiClient
66
{
7-
7+
public ICompanyValuation CompanyValuation { get; private set; }
8+
9+
public FinancialModelingPrepApiClient(ICompanyValuation companyValuation)
10+
{
11+
this.CompanyValuation = companyValuation;
12+
}
813
}
914
}

FinancialModelingPrepApi/Core/Http/FinancialModelingPrepHttpClient.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ public class FinancialModelingPrepHttpClient
1111
{
1212
private readonly HttpClient client;
1313
private readonly FinancialModelingPrepOptions options;
14+
private readonly JsonSerializerOptions jsonSerializerOptions;
1415

1516
public FinancialModelingPrepHttpClient(HttpClient client, FinancialModelingPrepOptions options)
1617
{
1718
this.client = client ?? throw new ArgumentNullException(nameof(client));
1819
this.options = options ?? throw new ArgumentNullException(nameof(options));
20+
this.jsonSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);
1921

2022
if (string.IsNullOrWhiteSpace(this.options.ApiKey))
2123
{
@@ -29,11 +31,11 @@ public async Task<ApiResponse<T>> GetAsync<T>(string urlPattern, NameValueCollec
2931
try
3032
{
3133
var response = await CallApiAsync(urlPattern, pathParams, queryString);
32-
var data = JsonSerializer.Deserialize<T>(response.Data);
34+
var data = JsonSerializer.Deserialize<T>(response.Data, jsonSerializerOptions);
3335

3436
return ApiResponse.FromSucces(data);
3537
}
36-
catch (Exception ex)
38+
catch (JsonException ex)
3739
{
3840
return ApiResponse.FromError<T>(ex.ToString());
3941
}

FinancialModelingPrepApi/Core/Http/QueryStringBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Specialized;
2+
using System.Web;
23

34
namespace FinancialModelingPrepApi.Core.Http
45
{
@@ -8,7 +9,7 @@ public class QueryStringBuilder
89

910
public QueryStringBuilder()
1011
{
11-
queryParams = new NameValueCollection();
12+
queryParams = HttpUtility.ParseQueryString(string.Empty);
1213
}
1314

1415
public void Add(string key, object value)
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
using FinancialModelingPrepApi.Abstractions;
2-
using FinancialModelingPrepApi.Core;
3-
using FinancialModelingPrepApi.Core.Http;
1+
using FinancialModelingPrepApi.Core.Http;
42
using Microsoft.Extensions.DependencyInjection;
53
using Microsoft.Extensions.DependencyInjection.Extensions;
64
using System;
5+
using FinancialModelingPrepApi.Core;
6+
using FinancialModelingPrepApi.Abstractions.CompanyValuation;
7+
using FinancialModelingPrepApi.Core.CompanyValuation;
78

89
namespace FinancialModelingPrepApi
910
{
1011
public static class DependencyInjectionExtensions
1112
{
12-
public static void AddFinancialModelingPrepApiClient(this IServiceCollection services)
13+
public static void AddFinancialModelingPrepApiClient(this IServiceCollection services, FinancialModelingPrepOptions options)
1314
{
15+
services.AddSingleton(options);
16+
1417
services.AddLogging();
18+
1519
services.AddHttpClient<FinancialModelingPrepHttpClient>(client
1620
=> client.BaseAddress = new Uri("https://financialmodelingprep.com/api/"));
21+
1722
services.TryAddSingleton<IFinancialModelingPrepApiClient, FinancialModelingPrepApiClient>();
23+
services.TryAddScoped<ICompanyValuation, CompanyValuation>();
1824
}
1925
}
2026
}

FinancialModelingPrepApi/FinancialModelingPrepApi.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31229.75
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialModelingPrepApi", "FinancialModelingPrepApi.csproj", "{B691C86E-CE23-40FA-811E-8D17BE9DD8BE}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "..\Tests\Tests.csproj", "{239A9A06-4A6A-47E8-9255-C9C5FA1BF13D}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{B691C86E-CE23-40FA-811E-8D17BE9DD8BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{B691C86E-CE23-40FA-811E-8D17BE9DD8BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{B691C86E-CE23-40FA-811E-8D17BE9DD8BE}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{239A9A06-4A6A-47E8-9255-C9C5FA1BF13D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{239A9A06-4A6A-47E8-9255-C9C5FA1BF13D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{239A9A06-4A6A-47E8-9255-C9C5FA1BF13D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{239A9A06-4A6A-47E8-9255-C9C5FA1BF13D}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

FinancialModelingPrepApi/Model/CompanyValuation/CompanyProfileResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace FinancialModelingPrepApi.Model.CompanyValuation
22
{
3-
public sealed class CompanyProfileResponse
3+
public class CompanyProfileResponse
44
{
55
public string symbol { get; set; }
66
public double price { get; set; }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using FinancialModelingPrepApi;
2+
using FinancialModelingPrepApi.Abstractions.CompanyValuation;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace Tests.CompanyValuation
9+
{
10+
public class CompanyValuationTests : TestingBase
11+
{
12+
public CompanyValuationTests(ITestOutputHelper testOutput) : base(testOutput)
13+
{
14+
}
15+
16+
[Fact]
17+
public async Task GetCompanyProfileTests()
18+
{
19+
var api = ServiceProvider.GetRequiredService<IFinancialModelingPrepApiClient>();
20+
21+
var result = await api.CompanyValuation.GetCompanyProfileAsync("AAPL,MSFT");
22+
23+
Assert.NotNull(result);
24+
Assert.False(result.HasError);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)