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

Easier logging #60

Closed
uddyami opened this issue Dec 4, 2019 · 11 comments · Fixed by #62
Closed

Easier logging #60

uddyami opened this issue Dec 4, 2019 · 11 comments · Fixed by #62
Labels
enhancement New feature or request

Comments

@uddyami
Copy link

uddyami commented Dec 4, 2019

When i'm trying to Alice https://pub.dev/packages/alice#-readme-tab-
this is following exception

Try changing the type of the parameter, or casting the argument to 'Response/*2*/'.

Can some one help show some working examples around this ?

@f3ath
Copy link
Owner

f3ath commented Dec 4, 2019

Hi @uddyami. I'm not sure what it has to do with this library. Could you please give me specific detail on what exactly you're doing? Also please provide the logs and stack traces you have.

Repository owner deleted a comment Dec 4, 2019
@uddyami
Copy link
Author

uddyami commented Dec 4, 2019

I'm trying to add logging to see raw request/ response. Alice adds logging and works on vanilla httpClient requests. From the api i expected to use it with JsonApiClient as well. But the type exception is something i'm getting stuck with.

this is an example for alice usage

httpClient
	.getUrl(Uri.parse("https://jsonplaceholder.typicode.com/posts"))
	.then((request) async {
		alice.onHttpClientRequest(request);
		var httpResponse = await request.close();
		var responseBody = await httpResponse.transform(utf8.decoder).join();
		alice.onHttpClientResponse(httpResponse, request, body: responseBody);
 });

@f3ath
Copy link
Owner

f3ath commented Dec 4, 2019

I'll try to figure it out. Meanwhile would you mind making an example dart code I could use to reproduce the issue?

@uddyami
Copy link
Author

uddyami commented Dec 4, 2019

sure i'll try and create something with example code

@uddyami
Copy link
Author

uddyami commented Dec 4, 2019

import 'package:http/http.dart';
import 'package:json_api/json_api.dart';
import 'package:alice/alice.dart';
void main() async {
  final httpClient = Client();
  final jsonApiClient = JsonApiClient(httpClient);
  Alice alice = Alice(showNotification: true);
  final companiesUri = Uri.parse('http://localhost:8080/companies');
  final response = await jsonApiClient.fetchCollection(companiesUri).then((request) async {
		alice.onHttpClientRequest(request);
		var httpResponse = await request.close();
		var responseBody = await httpResponse.transform(utf8.decoder).join();
		alice.onHttpClientResponse(httpResponse, request, body: responseBody);
 });
  
  httpClient.close();
  print('Status: ${response.status}');
  print('Headers: ${response.headers}');

  final resource = response.data.unwrap().first;

  print('The collection page size is ${response.data.collection.length}');
  print('The first element is ${resource}');
  print('Attributes:');
  resource.attributes.forEach((k, v) => print('$k=$v'));
  print('Relationships:');
  resource.toOne.forEach((k, v) => print('$k=$v'));
  resource.toMany.forEach((k, v) => print('$k=$v'));
}

these lines have the error

alice.onHttpClientRequest(request);
alice.onHttpClientResponse(httpResponse, request, body: responseBody);

@f3ath
Copy link
Owner

f3ath commented Dec 5, 2019

Well, this one is explainable. fetchCollection(companiesUri) returns an instance of Response, and not an instance of httpRequest which you're trying to send to Alice. I will add event hooks so you'd be able to hook up the logging more easily.

@f3ath
Copy link
Owner

f3ath commented Dec 5, 2019

@uddyami I have added the onHttpCall callback. You can use it to pass the request/response objects to Alice. Please let me know if it helps or if you need more details on it.

@f3ath f3ath reopened this Dec 5, 2019
@f3ath f3ath changed the title issue when trying to add logging Easier logging Dec 6, 2019
@f3ath f3ath added the enhancement New feature or request label Dec 6, 2019
@uddyami
Copy link
Author

uddyami commented Dec 6, 2019

I works perfectly! I love the fact you add it at client level.
This is how i used it

final jsonApiClient = JsonApiClient(httpClient,  onHttpCall: (req, resp) {
  print("request : ${req.method} ${req.url} \n ${req.headers}  \n ${req.body}");
  print("response: ${resp.headers} \n ${resp.body}");
  });

EDIT
actually something is off now. After api call is done and logged the response doesn't seem to work. Maybe because logger is already reading the response?

@f3ath
Copy link
Owner

f3ath commented Dec 7, 2019

I see. Going to pass the response body separately.

@f3ath
Copy link
Owner

f3ath commented Dec 7, 2019

@uddyami I was not able to reproduce the issue. Could you please post the steps to reproduce in #65

@uddyami
Copy link
Author

uddyami commented Dec 9, 2019

will do

@f3ath f3ath closed this as completed Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants