refactor: modularize Beam into new projects and interfaces
- Introduced modularity by splitting Beam into new projects: Beam.Abstractions, Beam.Models, and Beam.Downloaders. - Refactored existing classes into appropriate namespaces and projects. - Replaced specific implementations with abstractions (e.g., SourceLinkBuilder to LinkBuilder, State to IState, etc.). - Updated interfaces: added ITemplate, IArticleData, IDownloadReport, and others for improved extensibility. - Removed deprecated classes like SourceLinkBuilder and StateChangerFactory. - Enhanced link handling in downloaders by refactoring to use `string` over `SourceLink`. - Consolidated shared logic under Beam.Abstractions.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Beam.Abstractions;
|
||||
|
||||
namespace Beam {
|
||||
public class ApiCallBuilder(HttpClient client) {
|
||||
HttpClient Client = client;
|
||||
string Uri;
|
||||
object? Data;
|
||||
object? Body;
|
||||
HttpMethod Method = HttpMethod.Get;
|
||||
List<KeyValuePair<string, List<string>>> Headers = [];
|
||||
HashSet<HttpStatusCode> SuccessCodes = [HttpStatusCode.OK];
|
||||
|
||||
public ApiCallBuilder WithUri(string uri) {
|
||||
Uri = uri;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithUri(Uri uri) {
|
||||
return WithUri(uri.AbsoluteUri);
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithRequestData(object? data) {
|
||||
Data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithBody(object? data) {
|
||||
Body = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithMethod(HttpMethod method) {
|
||||
Method = method;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithHeaders(IEnumerable<(string Key, IEnumerable<string> Value)> headers) {
|
||||
Headers = headers.Select((x) => new KeyValuePair<string, List<string>>(x.Key, x.Value.ToList())).ToList();
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithSuccessCodes(params HashSet<HttpStatusCode> codes) {
|
||||
SuccessCodes = codes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder WithSuccessCodes(params IEnumerable<int> codes) {
|
||||
SuccessCodes = codes.Cast<HttpStatusCode>().ToHashSet();
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder AddHeader(string key, string value) {
|
||||
if (Headers.Any((x) => x.Key == key))
|
||||
Headers.FirstOrDefault((x) => x.Key == key).Value.Add(value);
|
||||
else
|
||||
Headers.Add(new KeyValuePair<string, List<string>>(key, [value]));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ApiCallBuilder AddBearer(string value)
|
||||
=> AddHeader("Authorization", "Bearer " + value);
|
||||
|
||||
public ApiCall Build() {
|
||||
if (Uri is null)
|
||||
throw new InvalidOperationException();
|
||||
if (Method is null)
|
||||
throw new InvalidOperationException();
|
||||
if (Headers is null)
|
||||
Headers = [];
|
||||
|
||||
return new ApiCall(Client, Uri, Method, Headers.Select((x) => new KeyValuePair<string, string[]>(x.Key, x.Value.ToArray())).ToArray(), Data, Body, SuccessCodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user