Hexer10 / youtube_explode_dart

Dart library to interact with many Youtube APIs
https://pub.dev/packages/youtube_explode_dart
BSD 3-Clause "New" or "Revised" License
299 stars 122 forks source link

use _httpClient instead of super #236

Closed abdelaziz-mahdy closed 1 year ago

abdelaziz-mahdy commented 1 year ago

this may help with the web problem my idea is pass an http client in which

web: all urls gets send with a cors middleware other: send same url

I am avoiding breaking changes by making people implement their own http clients if you dont mind a breaking change in YoutubeHttpClient let me know

  /// Initialize an instance of [YoutubeHttpClient]
  YoutubeHttpClient({http.Client? httpClient, dynamic corsUrl})
      : _httpClient = httpClient ?? http.Client(),
        corsUri = (corsUrl is String ? Uri.parse(corsUrl) : corsUrl);

most probably it will be like this

Hexer10 commented 1 year ago

Hi, thanks for the PR, although this is already how the code works, ie. when calling super.get, super.post, internally the this.send method is called so each request uses the passed http client. If you want to use a custom url to bypass the CORS, here is an example if you are using https://github.com/Rob--W/cors-anywhere .

class CorsBypassClient extends http.BaseClient {
  final _client = http.Client();

  @override
  Future<http.StreamedResponse> send(covariant http.Request request) {
    final uri = request.url;
    final http.BaseRequest newRequest = http.Request(
        request.method,
        request.url.replace(
            host: 'cors-anywhere.myinstance.com',
            pathSegments: [uri.host, ...uri.pathSegments]))
      ..headers.addAll({
        ...request.headers,
        'origin': 'https://www.youtube.com',
        'x-requested-with': 'https://www.youtube.com',
      })
      ..bodyBytes = request.bodyBytes;

    return _client.send(newRequest);
  }
}

...

final yt = YoutubeExplode(YoutubeHttpClient(CorsBypassClient()));

Note that it might still need some tweaking, and if a lot of users are using the same CORS server it might be rate limited.

abdelaziz-mahdy commented 1 year ago

Thank you very much, for the explanation.

Will check the code you mentioned.