Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/network interface #292

Merged
merged 10 commits into from
May 17, 2021
Merged

Feature/network interface #292

merged 10 commits into from
May 17, 2021

Conversation

wssgcg1213
Copy link
Member

@wssgcg1213 wssgcg1213 commented May 8, 2021

Close #172

  • 判断是否为 Kraken 内部发起的请求, 以及区分 contextId
  • interceptor 需要传入 Kraken widget 实例参数
  • 测试通过

@wssgcg1213 wssgcg1213 requested a review from andycall May 8, 2021 04:46
@wssgcg1213 wssgcg1213 self-assigned this May 8, 2021
@wssgcg1213
Copy link
Member Author

wssgcg1213 commented May 8, 2021

DEMO

class CustomHttpClientInterceptor implements HttpClientInterceptor {
  DateTime _startTime;
  DateTime _endTime;

  @override
  Future<HttpClientRequest> beforeRequest(HttpClientRequest request) {
    request.headers.set('x-foo', 'bar');
    print('beforeRequest, request headers: ${request.headers}');
    if (request.uri.path == '/posts/1') {
      _startTime = DateTime.now();
    }
    return null;
  }

  @override
  Future<HttpClientResponse> afterResponse(HttpClientRequest request, HttpClientResponse response) {
    print('afterResponse, response headers: ${response.headers}');
    if (request.uri.path == '/posts/1') {
      _endTime = DateTime.now();
      print('/posts/1 cost: ${_endTime.millisecondsSinceEpoch - _startTime.millisecondsSinceEpoch}ms');
    }
    return null;
  }

  @override
  Future<HttpClientResponse> shouldInterceptRequest(HttpClientRequest request) async {
    if (request.uri.path == '/posts/1') {
      // Direct output string, transform to stream.
      String replaced = '{ "foo": "bar" }';
      Uint8List data = Uint8List.fromList(replaced.codeUnits);
      Stream<Uint8List> stream = Stream<Uint8List>.value(data);
      return SimpleHttpClientResponse('text/html', 'utf8', stream, responseHeaders: { 'x-kraken': 'hey', 'a': '1' });
    }
    return null;
  }
}


void main() {
  runApp(Kraken(
    viewportWidth: viewportSize.width - queryData.padding.horizontal,
    viewportHeight: viewportSize.height - appBar.preferredSize.height - queryData.padding.vertical,
    // bundlePath: 'assets/bundle.js',
    // bundleURL: 'https://kraken.oss-cn-hangzhou.aliyuncs.com/demo/richtext.js',
    bundleContent: '''
      fetch('https://jsonplaceholder.typicode.com/posts/1')
        .then(function(response) {
          return response.json();
        })
        .then(function(data) {
          // This is the object from API.
          console.log('LOG:', data);
        })
        .catch(err => {
          console.error('error:', err);
        });
    ''',
    httpClientInterceptor: CustomHttpClientInterceptor(),
  ));
}

===>

image

@wssgcg1213 wssgcg1213 changed the title [WIP] Feature/network interface Feature/network interface May 10, 2021
@wssgcg1213 wssgcg1213 requested a review from yuanyan May 13, 2021 17:06
andycall
andycall previously approved these changes May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

讨论是否需要增加网络拦截的切面接口
4 participants