Skip to content

Commit c7f867f

Browse files
authored
Merge pull request #867 from abuzuhri/codex/remove-restsharp
Remove RestSharp dependency
2 parents 45cef31 + b2fbc1c commit c7f867f

File tree

12 files changed

+311
-26
lines changed

12 files changed

+311
-26
lines changed

Source/FikaAmazonAPI.SampleCode/FikaAmazonAPI.SampleCode.csproj

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.300.106" />
23-
<PackageReference Include="AWSSDK.SQS" Version="3.7.301.19" />
22+
<PackageReference Include="AWSSDK.SecurityToken" Version="4.0.1.3" />
23+
<PackageReference Include="AWSSDK.SQS" Version="4.0.0.13" />
2424
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
25-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
26-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
27-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
28-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
29-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
30-
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
31-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
25+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.6" />
26+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.6" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.6" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.6" />
29+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.6" />
30+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6" />
31+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.6" />
3232
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
33-
<PackageReference Include="RestSharp" Version="112.1.0" />
34-
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="111.2.0" />
35-
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
33+
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.2" />
3634
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
3735
<PackageReference Include="System.Collections" Version="4.3.0" />
3836
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ static async Task Main(string[] args)
4242
});
4343

4444

45-
var itemsList=amazonConnection.FulFillmentInbound.GetShipmentItemsByShipmentId("FBA15KBCBMXC");
45+
// var itemsList=amazonConnection.FulFillmentInbound.GetShipmentItemsByShipmentId("FBA15KBCBMXC");
4646

4747

4848

49-
FeedsSample feedsSample = new FeedsSample(amazonConnection);
50-
feedsSample.SubmitFeedPRICING_JSONAsync("B09H73T814.259", 112.0M, 53.51M, 112.20M).GetAwaiter().GetResult();
49+
//FeedsSample feedsSample = new FeedsSample(amazonConnection);
50+
//feedsSample.SubmitFeedPRICING_JSONAsync("B09H73T814.259", 112.0M, 53.51M, 112.20M).GetAwaiter().GetResult();
5151

5252

5353
Console.ReadLine();

Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials, string
2626
// RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
2727
if (string.IsNullOrWhiteSpace(proxyAddress))
2828
{
29-
RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
30-
}else
29+
var options = new RestClientOptions(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
30+
RestClient = new RestClient(options);
31+
}
32+
else
3133
{
3234
var options = new RestClientOptions(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority))
3335
{
@@ -63,7 +65,10 @@ public virtual async Task<TokenResponse> GetAccessTokenAsync(CancellationToken c
6365

6466
if (!IsSuccessful(response))
6567
{
66-
throw new IOException("Unsuccessful LWA token exchange", response.ErrorException);
68+
var message = string.IsNullOrEmpty(response.ErrorMessage)
69+
? "Unsuccessful LWA token exchange"
70+
: $"Unsuccessful LWA token exchange: {response.ErrorMessage}";
71+
throw new IOException(message);
6772
}
6873

6974
var tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(response.Content);

Source/FikaAmazonAPI/FikaAmazonAPI.csproj

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<Product>CSharp Amazon Sp API</Product>
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<LangVersion>8.0</LangVersion>
10-
<Version>1.9.2</Version>
11-
<AssemblyVersion>1.9.2</AssemblyVersion>
12-
<FileVersion>1.9.2</FileVersion>
10+
<Version>1.9.3</Version>
11+
<AssemblyVersion>1.9.3</AssemblyVersion>
12+
<FileVersion>1.9.3</FileVersion>
1313
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1414
<PackageProjectUrl>https://github.com/abuzuhri/Amazon-SP-API-CSharp</PackageProjectUrl>
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -32,18 +32,20 @@
3232
</ItemGroup>
3333

3434
<ItemGroup>
35-
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.300.106" />
36-
<PackageReference Include="AWSSDK.SQS" Version="3.7.301.19" />
35+
<PackageReference Include="AWSSDK.SecurityToken" Version="4.0.1.3" />
36+
<PackageReference Include="AWSSDK.SQS" Version="4.0.0.13" />
3737
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
38-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
38+
<PackageReference Include="System.Text.Json" Version="9.0.6" />
39+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.6" />
3940
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
40-
<PackageReference Include="RestSharp" Version="112.1.0" />
41-
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="111.2.0" />
4241
<PackageReference Include="StandardSocketsHttpHandler" Version="2.2.0.8" />
4342
<PackageReference Include="System.Collections" Version="4.3.0" />
4443
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
4544
<PackageReference Include="System.Reflection" Version="4.3.0" />
4645
</ItemGroup>
46+
<ItemGroup>
47+
<Compile Include="..\RestSharp\*.cs" />
48+
</ItemGroup>
4749
<ItemGroup>
4850
<None Include="..\..\Others\icon\icon.jpg">
4951
<Pack>True</Pack>

Source/RestSharp/Extensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace RestSharp
5+
{
6+
public static class RestSharpExtensions
7+
{
8+
public static void RemoveParameter(this IList<Parameter> parameters, string name)
9+
{
10+
var items = parameters.Where(p => p.Name == name).ToList();
11+
foreach (var item in items)
12+
parameters.Remove(item);
13+
}
14+
}
15+
}
16+
17+
namespace RestSharp.Serializers.NewtonsoftJson
18+
{
19+
public static class RestClientExtensions
20+
{
21+
public static RestSharp.RestClient UseNewtonsoftJson(this RestSharp.RestClient client) => client;
22+
}
23+
}

Source/RestSharp/Method.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace RestSharp
2+
{
3+
public enum Method
4+
{
5+
Get,
6+
Post,
7+
Put,
8+
Delete,
9+
Patch
10+
}
11+
}

Source/RestSharp/Parameter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace RestSharp
2+
{
3+
public class Parameter
4+
{
5+
public string Name { get; set; }
6+
public object Value { get; set; }
7+
public ParameterType Type { get; set; }
8+
}
9+
10+
public class HeaderParameter : Parameter {}
11+
}

Source/RestSharp/ParameterType.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace RestSharp
2+
{
3+
public enum ParameterType
4+
{
5+
QueryString,
6+
HttpHeader
7+
}
8+
}

Source/RestSharp/RestClient.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Newtonsoft.Json;
10+
11+
namespace RestSharp
12+
{
13+
public class RestClient
14+
{
15+
public RestClientOptions Options { get; }
16+
private readonly HttpClient _httpClient;
17+
private static readonly HttpMethod PatchMethod = new HttpMethod("PATCH");
18+
19+
public RestClient(RestClientOptions options, Action<RestClient> configureSerialization = null)
20+
{
21+
Options = options;
22+
var handler = new HttpClientHandler();
23+
if (options.Proxy != null)
24+
{
25+
handler.Proxy = options.Proxy;
26+
handler.UseProxy = true;
27+
}
28+
_httpClient = new HttpClient(handler) { BaseAddress = options.BaseUrl };
29+
configureSerialization?.Invoke(this);
30+
}
31+
32+
public RestClient(HttpClient httpClient, RestClientOptions options, Action<RestClient> configureSerialization = null)
33+
{
34+
Options = options;
35+
_httpClient = httpClient;
36+
if (Options.BaseUrl != null)
37+
_httpClient.BaseAddress = Options.BaseUrl;
38+
configureSerialization?.Invoke(this);
39+
}
40+
41+
private HttpMethod ToHttpMethod(Method method)
42+
{
43+
return method switch
44+
{
45+
Method.Get => HttpMethod.Get,
46+
Method.Post => HttpMethod.Post,
47+
Method.Put => HttpMethod.Put,
48+
Method.Delete => HttpMethod.Delete,
49+
Method.Patch => PatchMethod,
50+
_ => HttpMethod.Get
51+
};
52+
}
53+
54+
private HttpRequestMessage BuildRequest(RestRequest request)
55+
{
56+
var uriBuilder = new UriBuilder(new Uri(Options.BaseUrl, request.Resource));
57+
var queryParams = request.Parameters
58+
.Where(p => p.Type == ParameterType.QueryString)
59+
.Select(p => $"{Uri.EscapeDataString(p.Name)}={Uri.EscapeDataString(p.Value.ToString())}");
60+
var query = string.Join("&", queryParams);
61+
if (!string.IsNullOrEmpty(query))
62+
{
63+
if (string.IsNullOrEmpty(uriBuilder.Query))
64+
uriBuilder.Query = query;
65+
else
66+
uriBuilder.Query = $"{uriBuilder.Query.TrimStart('?')}&{query}";
67+
}
68+
var message = new HttpRequestMessage(ToHttpMethod(request.Method), uriBuilder.Uri);
69+
70+
foreach (var header in request.Parameters.Where(p => p.Type == ParameterType.HttpHeader))
71+
{
72+
message.Headers.TryAddWithoutValidation(header.Name, header.Value.ToString());
73+
}
74+
75+
if (request.JsonBody != null)
76+
{
77+
message.Content = new StringContent(request.JsonBody, Encoding.UTF8, "application/json");
78+
}
79+
80+
return message;
81+
}
82+
83+
private RestResponse CreateRestResponse(HttpResponseMessage httpResponse, string content)
84+
{
85+
var response = new RestResponse
86+
{
87+
StatusCode = httpResponse.StatusCode,
88+
Content = content,
89+
ResponseStatus = ResponseStatus.Completed,
90+
ResponseUri = httpResponse.RequestMessage?.RequestUri
91+
};
92+
93+
foreach (var header in httpResponse.Headers)
94+
{
95+
foreach (var val in header.Value)
96+
{
97+
response.Headers.Add(new HeaderParameter { Name = header.Key, Value = val });
98+
}
99+
}
100+
foreach (var header in httpResponse.Content.Headers)
101+
{
102+
foreach (var val in header.Value)
103+
{
104+
response.Headers.Add(new HeaderParameter { Name = header.Key, Value = val });
105+
}
106+
}
107+
108+
return response;
109+
}
110+
111+
public async Task<RestResponse> ExecuteAsync(RestRequest request, CancellationToken cancellationToken = default)
112+
{
113+
var message = BuildRequest(request);
114+
using var httpResponse = await _httpClient.SendAsync(message, cancellationToken).ConfigureAwait(false);
115+
var content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
116+
return CreateRestResponse(httpResponse, content);
117+
}
118+
119+
public async Task<RestResponse<T>> ExecuteAsync<T>(RestRequest request, CancellationToken cancellationToken = default)
120+
{
121+
var baseResponse = await ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
122+
var data = default(T);
123+
if (!string.IsNullOrEmpty(baseResponse.Content))
124+
{
125+
try
126+
{
127+
data = JsonConvert.DeserializeObject<T>(baseResponse.Content);
128+
}
129+
catch
130+
{
131+
// ignore deserialization errors
132+
}
133+
}
134+
return new RestResponse<T>
135+
{
136+
StatusCode = baseResponse.StatusCode,
137+
Content = baseResponse.Content,
138+
ResponseStatus = baseResponse.ResponseStatus,
139+
ResponseUri = baseResponse.ResponseUri,
140+
Headers = baseResponse.Headers,
141+
ErrorMessage = baseResponse.ErrorMessage,
142+
Data = data
143+
};
144+
}
145+
}
146+
}

Source/RestSharp/RestClientOptions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Net;
3+
4+
namespace RestSharp
5+
{
6+
public class RestClientOptions
7+
{
8+
public RestClientOptions() { }
9+
10+
public RestClientOptions(string baseUrl)
11+
{
12+
BaseUrl = new Uri(baseUrl);
13+
}
14+
15+
public Uri BaseUrl { get; set; }
16+
public IWebProxy Proxy { get; set; }
17+
}
18+
}

0 commit comments

Comments
 (0)