Skip to content

Commit d0eccd4

Browse files
committed
add files
0 parents  commit d0eccd4

16 files changed

+2841
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Files and directories created by pub.
2+
.dart_tool/
3+
.packages
4+
5+
# Conventional directory for build outputs.
6+
build/
7+
8+
# Omit committing pubspec.lock for library packages; see
9+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
10+
pubspec.lock
11+
12+
doc/

CHANGELOG.md

Whitespace-only changes.

LICENSE

+661
Large diffs are not rendered by default.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# xdnmb_api

analysis_options.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options

example/xdnmb_api_example.dart

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO

lib/src/client.dart

+252
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
import 'dart:io';
2+
3+
import 'package:http/http.dart';
4+
import 'package:http/io_client.dart';
5+
import 'package:http_parser/http_parser.dart';
6+
7+
class HttpStatusException implements Exception {
8+
final int statusCode;
9+
10+
final String? body;
11+
12+
const HttpStatusException(this.statusCode, [this.body]);
13+
14+
@override
15+
String toString() {
16+
var result =
17+
"HttpStatusException: the HTTP response's status code is $statusCode";
18+
if (body != null) {
19+
result += ", the body is '$body'";
20+
}
21+
22+
return result;
23+
}
24+
}
25+
26+
class Multipart extends MultipartRequest {
27+
Multipart(String url) : super('POST', Uri.parse(url));
28+
29+
void add(String field, Object value) => fields[field] = value.toString();
30+
31+
void addBytes(String field, List<int> value,
32+
{String? filename, String? contentType}) =>
33+
files.add(MultipartFile.fromBytes(field, value,
34+
filename: filename,
35+
contentType:
36+
contentType != null ? MediaType.parse(contentType) : null));
37+
}
38+
39+
class Client extends IOClient {
40+
static const Duration _idleTimeout = Duration(seconds: 90);
41+
42+
static const String _userAgent = 'xdnmb';
43+
44+
Client({Duration timeout = const Duration(seconds: 15)})
45+
: super(HttpClient()
46+
..connectionTimeout = timeout
47+
..idleTimeout = _idleTimeout);
48+
49+
@override
50+
Future<IOStreamedResponse> send(BaseRequest request) {
51+
request.headers[HttpHeaders.userAgentHeader] = _userAgent;
52+
53+
return super.send(request);
54+
}
55+
56+
Future<Response> xGet(String url, [String? cookie]) async {
57+
final response = await this.get(Uri.parse(url),
58+
headers: cookie != null ? {HttpHeaders.cookieHeader: cookie} : null);
59+
_checkStatusCode(response);
60+
61+
return response;
62+
}
63+
64+
Future<Response> xPostForm(String url, Map<String, String>? form,
65+
[String? cookie]) async {
66+
final response = await this.post(Uri.parse(url),
67+
headers: cookie != null ? {HttpHeaders.cookieHeader: cookie} : null,
68+
body: form);
69+
_checkStatusCode(response);
70+
71+
return response;
72+
}
73+
74+
Future<Response> xPostMultipart(Multipart multipart, [String? cookie]) async {
75+
if (cookie != null) {
76+
multipart.headers[HttpHeaders.cookieHeader] = cookie;
77+
}
78+
final streamedResponse = await send(multipart);
79+
final response = await Response.fromStream(streamedResponse);
80+
_checkStatusCode(response);
81+
82+
return response;
83+
}
84+
}
85+
86+
void _checkStatusCode(Response response) {
87+
if (response.statusCode != HttpStatus.ok) {
88+
final body = response.body;
89+
if (body.isEmpty) {
90+
throw HttpStatusException(response.statusCode);
91+
}
92+
throw HttpStatusException(response.statusCode, body);
93+
}
94+
}
95+
96+
/* import 'dart:collection';
97+
import 'dart:convert';
98+
import 'dart:io';
99+
100+
import 'package:form_data/form_data.dart';
101+
102+
class HttpStatusException implements Exception {
103+
final int statusCode;
104+
105+
final String? body;
106+
107+
const HttpStatusException(this.statusCode, [this.body]);
108+
109+
@override
110+
String toString() {
111+
var result =
112+
"HttpStatusException: the HTTP response's status code is $statusCode";
113+
if (body != null) {
114+
result += ", the body is '$body'";
115+
}
116+
117+
return result;
118+
}
119+
}
120+
121+
enum Method { get, post }
122+
123+
class Request {
124+
final Method method;
125+
126+
final String url;
127+
128+
final List<String>? cookies;
129+
130+
late final Map<String, String>? headers;
131+
132+
late final List<int>? body;
133+
134+
Request(
135+
{required this.method,
136+
required this.url,
137+
this.cookies,
138+
this.headers,
139+
this.body});
140+
141+
Request.get(this.url, {this.cookies, this.headers})
142+
: method = Method.get,
143+
body = null;
144+
145+
Request.postForm(this.url, Map<String, String> form,
146+
{this.cookies, Map<String, String>? headers})
147+
: method = Method.post {
148+
var pairs = <List<String>>[];
149+
form.forEach((key, value) {
150+
pairs.add(
151+
[Uri.encodeQueryComponent(key), Uri.encodeQueryComponent(value)]);
152+
});
153+
final bodyString = pairs.map((pair) => '${pair[0]}=${pair[1]}').join('&');
154+
body = utf8.encode(bodyString);
155+
156+
this.headers = headers ?? HashMap()
157+
..[HttpHeaders.contentTypeHeader] = 'application/x-www-form-urlencoded';
158+
}
159+
160+
Request.postMultipart(this.url, FormData multipart,
161+
{this.cookies, Map<String, String>? headers})
162+
: method = Method.post {
163+
body = multipart.body;
164+
165+
this.headers = headers ?? HashMap()
166+
..[HttpHeaders.contentTypeHeader] = multipart.contentType;
167+
}
168+
}
169+
170+
class Response {
171+
final HttpHeaders headers;
172+
173+
final String body;
174+
175+
const Response(this.headers, this.body);
176+
}
177+
178+
class Client {
179+
final HttpClient _httpClient;
180+
181+
static const Duration _idleTimeout = Duration(seconds: 90);
182+
183+
static const String _userAgent = 'xdnmb';
184+
185+
Client({Duration timeout = const Duration(seconds: 15)})
186+
: _httpClient = HttpClient()
187+
..connectionTimeout = timeout
188+
..idleTimeout = _idleTimeout;
189+
190+
Future<Response> send(Request request) async {
191+
final httpRequest =
192+
await _httpClient.openUrl(request.method.name, Uri.parse(request.url));
193+
194+
httpRequest.headers.set(HttpHeaders.userAgentHeader, _userAgent);
195+
196+
if (request.cookies != null) {
197+
for (final cookie in request.cookies!) {
198+
httpRequest.headers.add(HttpHeaders.cookieHeader, cookie);
199+
}
200+
}
201+
202+
if (request.headers != null) {
203+
request.headers!
204+
.forEach((key, value) => httpRequest.headers.add(key, value));
205+
}
206+
207+
if (request.body != null) {
208+
httpRequest.headers
209+
.set(HttpHeaders.contentLengthHeader, request.body!.length);
210+
httpRequest.add(request.body!);
211+
}
212+
213+
final response = await httpRequest.close();
214+
final body = await response.transform(utf8.decoder).join();
215+
216+
if (response.statusCode != HttpStatus.ok) {
217+
if (body.isEmpty) {
218+
throw HttpStatusException(response.statusCode);
219+
}
220+
throw HttpStatusException(response.statusCode, body);
221+
}
222+
223+
return Response(response.headers, body);
224+
}
225+
226+
Future<Response> get(String url, [String? cookie]) async {
227+
final request = Request.get(url, cookies: cookie == null ? null : [cookie]);
228+
229+
return await send(request);
230+
}
231+
232+
Future<Response> postForm(String url, Map<String, String> form,
233+
[String? cookie]) async {
234+
final request =
235+
Request.postForm(url, form, cookies: cookie == null ? null : [cookie]);
236+
237+
return await send(request);
238+
}
239+
240+
Future<Response> postMultipart(String url, FormData multipart,
241+
[String? cookie]) async {
242+
final request = Request.postMultipart(url, multipart,
243+
cookies: cookie == null ? null : [cookie]);
244+
245+
return await send(request);
246+
}
247+
248+
void close({bool force = false}) {
249+
_httpClient.close(force: force);
250+
}
251+
}
252+
*/

lib/src/cookie.dart

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
part of 'xdnmb.dart';
2+
3+
class XdnmbCookie {
4+
late final String userHash;
5+
6+
late final String? name;
7+
8+
final int? id;
9+
10+
String get cookie => 'userhash=$userHash';
11+
12+
XdnmbCookie(this.userHash, {this.name, this.id});
13+
14+
XdnmbCookie._fromJson(String data, {this.id}) {
15+
final Map<String, dynamic> decoded = json.decode(data);
16+
17+
userHash = decoded['cookie'];
18+
name = decoded['name'];
19+
}
20+
}
21+
22+
class CookieList {
23+
final bool canGetCookie;
24+
25+
final int currentCookiesNum;
26+
27+
final int totalCookiesNum;
28+
29+
final List<XdnmbCookie> cookiesList;
30+
31+
const CookieList._internal(
32+
{required this.canGetCookie,
33+
required this.currentCookiesNum,
34+
required this.totalCookiesNum,
35+
required this.cookiesList});
36+
}
37+
38+
extension CookieExtension on Cookie {
39+
String get toCookie => '$name=$value';
40+
}
41+
42+
String _toCookies(Iterable<String> cookies) => cookies.join('; ');

0 commit comments

Comments
 (0)