Skip to content

Commit

Permalink
feat : add w3c feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedAlaaInstabug committed Dec 2, 2024
1 parent 6d6b022 commit 9f704b5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 71 deletions.
8 changes: 8 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ scripts:
flutter: true
dependsOn: pana
private: false
pods:
run: cd ios && pod install --repo-update
description: running pod install
exec:
concurrency: 1
orderDependents: true
packageFilters:
fileExists: 'ios/PodFile'
score:
run: dart run pana --no-warning --exit-code-threshold 0
exec:
Expand Down
7 changes: 4 additions & 3 deletions packages/instabug_http_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

## Unreleased
## [2.5.0] - 18/11/2024

- Enables `InstabugHttpClient` to wrap an internal `http` client.
- Add support for `http` v1 ([#20](https://github.com/Instabug/Instabug-Dart-http-Adapter/pull/20)).
### Added

- Add support for tracing network requests from Instabug to services like Datadog and New Relic ([#21](https://github.com/Instabug/Instabug-Dart-http-Adapter/pull/21)).

## [2.4.0] - 7/05/2024

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<application
android:label="example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
14 changes: 3 additions & 11 deletions packages/instabug_http_client/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ packages:
dependency: transitive
description:
name: http
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
url: "https://pub.dev"
source: hosted
version: "1.2.2"
version: "0.13.6"
http_parser:
dependency: transitive
description:
Expand All @@ -97,7 +97,7 @@ packages:
path: "../../instabug_flutter"
relative: true
source: path
version: "13.4.0"
version: "14.0.0"
instabug_http_client:
dependency: "direct main"
description:
Expand Down Expand Up @@ -246,14 +246,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "14.2.5"
web:
dependency: transitive
description:
name: web
sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb
url: "https://pub.dev"
source: hosted
version: "1.1.0"
sdks:
dart: ">=3.5.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
79 changes: 53 additions & 26 deletions packages/instabug_http_client/lib/instabug_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: invalid_use_of_internal_member

library instabug_http_client;

import 'dart:convert';
Expand All @@ -7,19 +9,16 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:http/http.dart' as http;
import 'package:instabug_flutter/instabug_flutter.dart';
import 'package:instabug_http_client/instabug_http_logger.dart';
import 'package:meta/meta.dart';

class InstabugHttpClient extends InstabugHttpLogger implements http.Client {
/// Constructs a new [InstabugHttpClient].
///
/// Provide a value for [client] in order to override the internal client used
/// by this class. This can be useful if you are working with other libraries
/// that require other custom client implementations
InstabugHttpClient({http.Client? client}) : client = client ?? http.Client() {
InstabugHttpClient() : client = http.Client() {
logger = this;
}

final NetworkLogger _networklogger = NetworkLogger();
@visibleForTesting
http.Client client;

Expand All @@ -31,66 +30,92 @@ class InstabugHttpClient extends InstabugHttpLogger implements http.Client {

@override
Future<http.Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
final DateTime startTime = DateTime.now();
final Map<String, String> requestHeader = headers ?? <String, String>{};
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client
.delete(url, body: body, headers: headers, encoding: encoding)
.delete(url, body: body, headers: requestHeader, encoding: encoding)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime);
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
return response;
});
}

Future<W3CHeader?> getW3cHeader(Map<String, String> requestHeader, DateTime startTime) async {
final W3CHeader? w3cHeader = await _networklogger.getW3CHeader(
requestHeader, startTime.millisecondsSinceEpoch);
if (w3cHeader?.isW3cHeaderFound == false &&
w3cHeader?.w3CGeneratedHeader != null) {
requestHeader['traceparent'] = w3cHeader!.w3CGeneratedHeader!;
}
return w3cHeader;
}

@override
Future<http.Response> get(Uri url, {Map<String, String>? headers}) {
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
final DateTime startTime = DateTime.now();
return client.get(url, headers: headers).then((http.Response response) {
logger.onLogger(response, startTime: startTime);
final Map<String, String> requestHeader = headers ?? <String, String>{};
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client
.get(url, headers: requestHeader)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
return response;
});
}

@override
Future<http.Response> head(Uri url, {Map<String, String>? headers}) {
Future<http.Response> head(Uri url, {Map<String, String>? headers}) async {
final DateTime startTime = DateTime.now();
return client.head(url, headers: headers).then((http.Response response) {
logger.onLogger(response, startTime: startTime);
final Map<String, String> requestHeader = headers ?? <String, String>{};
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client
.head(url, headers: requestHeader)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
return response;
});
}

@override
Future<http.Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
final DateTime startTime = DateTime.now();
final Map<String, String> requestHeader = headers ?? <String, String>{};
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client
.patch(url, headers: headers, body: body, encoding: encoding)
.patch(url, headers: requestHeader, body: body, encoding: encoding)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime);
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
return response;
});
}

@override
Future<http.Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
final DateTime startTime = DateTime.now();
final Map<String, String> requestHeader = headers ?? <String, String>{};
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client
.post(url, headers: headers, body: body, encoding: encoding)
.post(url, headers: requestHeader, body: body, encoding: encoding)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime);
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
return response;
});
}

@override
Future<http.Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) {
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
final DateTime startTime = DateTime.now();
final Map<String, String> requestHeader = headers ?? <String, String>{};
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client
.put(url, headers: headers, body: body, encoding: encoding)
.put(url, headers: requestHeader, body: body, encoding: encoding)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime);
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
return response;
});
}
Expand All @@ -104,12 +129,14 @@ class InstabugHttpClient extends InstabugHttpLogger implements http.Client {
client.readBytes(url, headers: headers);

@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
Future<http.StreamedResponse> send(http.BaseRequest request) async {
final DateTime startTime = DateTime.now();
final Map<String, String> requestHeader = request.headers;
final W3CHeader? w3cHeader = await getW3cHeader(requestHeader, startTime);
return client.send(request).then((http.StreamedResponse streamedResponse) =>
http.Response.fromStream(streamedResponse)
.then((http.Response response) {
logger.onLogger(response, startTime: startTime);
logger.onLogger(response, startTime: startTime, w3CHeader: w3cHeader);
// Need to return new StreamedResponse, as body only can be listened once
return http.StreamedResponse(
Stream<List<int>>.value(response.bodyBytes),
Expand Down
3 changes: 2 additions & 1 deletion packages/instabug_http_client/lib/instabug_http_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:http/http.dart' as http;
import 'package:instabug_flutter/instabug_flutter.dart';

class InstabugHttpLogger {
void onLogger(http.Response response, {DateTime? startTime}) {
void onLogger(http.Response response, {DateTime? startTime,W3CHeader? w3CHeader}) {
final NetworkLogger networkLogger = NetworkLogger();

final Map<String, dynamic> requestHeaders = <String, dynamic>{};
Expand All @@ -29,6 +29,7 @@ class InstabugHttpLogger {
url: request.url.toString(),
requestHeaders: requestHeaders,
requestBody: requestBody,
w3cHeader: w3CHeader
);

final DateTime endTime = DateTime.now();
Expand Down
2 changes: 1 addition & 1 deletion packages/instabug_http_client/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ packages:
path: "../instabug_flutter"
relative: true
source: path
version: "13.4.0"
version: "14.0.0"
io:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit 9f704b5

Please sign in to comment.