Skip to content

Commit 9c0d8c6

Browse files
committed
Implement GetCompanyProfile API Call
1 parent 39a47c4 commit 9c0d8c6

File tree

6 files changed

+133
-4
lines changed

6 files changed

+133
-4
lines changed

FinancialModelingPrepApi/Abstractions/Models/ApiVersion.cs renamed to FinancialModelingPrepApi/Abstractions/ApiVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace FinancialModelingPrepApi.Abstractions.Models
1+
namespace FinancialModelingPrepApi.Abstractions
22
{
33
public enum ApiVersion
44
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using FinancialModelingPrepApi.Model;
2+
using FinancialModelingPrepApi.Model.CompanyValuation;
3+
using System.Threading.Tasks;
4+
5+
namespace FinancialModelingPrepApi.Abstractions.CompanyValuation
6+
{
7+
public interface ICompanyValuation
8+
{
9+
public Task<ApiResponse<CompanyProfileResponse>> GetCompanyProfileAsync(string symbol);
10+
}
11+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using FinancialModelingPrepApi.Abstractions;
2+
using FinancialModelingPrepApi.Abstractions.CompanyValuation;
3+
using FinancialModelingPrepApi.Core.Http;
4+
using FinancialModelingPrepApi.Model;
5+
using FinancialModelingPrepApi.Model.CompanyValuation;
6+
using System.Collections.Specialized;
7+
using System.Threading.Tasks;
8+
9+
namespace FinancialModelingPrepApi.Core.CompanyValuation
10+
{
11+
public class CompanyValuation : ICompanyValuation
12+
{
13+
private readonly FinancialModelingPrepHttpClient client;
14+
15+
public CompanyValuation(FinancialModelingPrepHttpClient client)
16+
{
17+
this.client = client ?? throw new System.ArgumentNullException(nameof(client));
18+
}
19+
20+
public Task<ApiResponse<CompanyProfileResponse>> GetCompanyProfileAsync(string symbol)
21+
{
22+
const string url = "[version]/profile/[symbol]";
23+
24+
var pathParams = new NameValueCollection()
25+
{
26+
{ "version", ApiVersion.v3.ToString() },
27+
{ "symbol", symbol }
28+
};
29+
30+
return client.GetAsync<CompanyProfileResponse>(url, pathParams, null);
31+
}
32+
}
33+
}

FinancialModelingPrepApi/Core/Http/FinancialModelingPrepHttpClient.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System;
1+
using FinancialModelingPrepApi.Model;
2+
using System;
23
using System.Collections.Specialized;
34
using System.Net.Http;
5+
using System.Text.Json;
46
using System.Threading.Tasks;
57

68
namespace FinancialModelingPrepApi.Core.Http
@@ -21,7 +23,23 @@ public FinancialModelingPrepHttpClient(HttpClient client, FinancialModelingPrepO
2123
}
2224
}
2325

24-
public async Task<string> CallApiAsync(string urlPattern, NameValueCollection pathParams, QueryStringBuilder queryString)
26+
public async Task<ApiResponse<T>> GetAsync<T>(string urlPattern, NameValueCollection pathParams, QueryStringBuilder queryString)
27+
where T : class
28+
{
29+
try
30+
{
31+
var response = await CallApiAsync(urlPattern, pathParams, queryString);
32+
var data = JsonSerializer.Deserialize<T>(response.Data);
33+
34+
return ApiResponse.FromSucces(data);
35+
}
36+
catch (Exception ex)
37+
{
38+
return ApiResponse.FromError<T>(ex.ToString());
39+
}
40+
}
41+
42+
private async Task<ApiResponse<string>> CallApiAsync(string urlPattern, NameValueCollection pathParams, QueryStringBuilder queryString)
2543
{
2644
PreProcessUrl(ref urlPattern, ref pathParams, ref queryString);
2745

@@ -30,7 +48,14 @@ public async Task<string> CallApiAsync(string urlPattern, NameValueCollection pa
3048
var requestUrl = $"{urlPattern}{queryString}";
3149

3250
using var response = await client.GetAsync(requestUrl);
33-
return await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
51+
var content = await response.Content.ReadAsStringAsync();
52+
53+
if (!response.IsSuccessStatusCode)
54+
{
55+
return ApiResponse.FromError<string>($"{response.StatusCode} - {content}");
56+
}
57+
58+
return ApiResponse.FromSucces(content);
3459
}
3560

3661
private static void PreProcessUrl(ref string url, ref NameValueCollection pathParams, ref QueryStringBuilder qsb)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace FinancialModelingPrepApi.Model
2+
{
3+
public static class ApiResponse
4+
{
5+
public static ApiResponse<TReturn> FromError<TReturn>(string error) where TReturn : class
6+
=> new()
7+
{
8+
Error = error
9+
};
10+
11+
public static ApiResponse<TReturn> FromSucces<TReturn>(TReturn data) where TReturn : class
12+
=> new()
13+
{
14+
Data = data
15+
};
16+
}
17+
18+
public class ApiResponse<T> where T : class
19+
{
20+
public string Error { get; set; }
21+
public bool HasError => !string.IsNullOrEmpty(Error);
22+
public T Data { get; set; }
23+
}
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace FinancialModelingPrepApi.Model.CompanyValuation
2+
{
3+
public sealed class CompanyProfileResponse
4+
{
5+
public string symbol { get; set; }
6+
public double price { get; set; }
7+
public double beta { get; set; }
8+
public int volAvg { get; set; }
9+
public long mktCap { get; set; }
10+
public double lastDiv { get; set; }
11+
public string range { get; set; }
12+
public double changes { get; set; }
13+
public string companyName { get; set; }
14+
public string currency { get; set; }
15+
public string isin { get; set; }
16+
public string cusip { get; set; }
17+
public string exchange { get; set; }
18+
public string exchangeShortName { get; set; }
19+
public string industry { get; set; }
20+
public string website { get; set; }
21+
public string description { get; set; }
22+
public string ceo { get; set; }
23+
public string sector { get; set; }
24+
public string country { get; set; }
25+
public string fullTimeEmployees { get; set; }
26+
public string phone { get; set; }
27+
public string address { get; set; }
28+
public string city { get; set; }
29+
public string state { get; set; }
30+
public string zip { get; set; }
31+
public double dcfDiff { get; set; }
32+
public double dcf { get; set; }
33+
public string image { get; set; }
34+
public string ipoDate { get; set; }
35+
}
36+
}

0 commit comments

Comments
 (0)