Giter VIP home page Giter VIP logo

Comments (3)

xellDart avatar xellDart commented on September 14, 2024 1

Hi, thanks for your response, yes I already understood the logic of the implementation, which allowed me to solve my doubts.
for generic response I modify NetworkDecoder class:

class NetworkDecoder {
  static var shared = NetworkDecoder();

  K decode<T extends BaseNetworkModel, K>(
      {required Response<dynamic> response, required T responseType}) {
    try {
      // deserialize, fails if response message is another type
      var data = BaseResponse.fromJson(
          response.data, (json) => responseType.fromJson(response.data) as K);
      return data.payload!;
    } on TypeError {
      // throw error with response error message
      throw response.data['payload'];
    }
  }
}

Bu later I change this, I remove base_response model

import 'package:dio/dio.dart';

import '../interfaces/base_network_model.dart';

class NetworkDecoder {
  static var shared = NetworkDecoder();

  K decode<T extends BaseNetworkModel, K>(
      {required Response<dynamic> response, required T responseType}) {
    try {
      // check status
      var data = response.data;
      if(!data['status']) {
        // throw error response
        throw response.data['payload'];
      }
      
      // get payload body
      var payload = data['payload'];
      
      // deserialize, fails if response message is another type
      if(payload is List) {
        var list = response.data as List;
        return List<T>.from(list.map((item) => responseType.fromJson(item)).toList()) as K;
      } else {
        var data = responseType.fromJson(payload) as K;
        return data;
      }
    } on TypeError {
      // throw error with response error message
      rethrow;
    }
  }
}

I think this is not the cleanest way but it works.

the only thing I'm trying to implement is reading a JWT without having to pass it in every request in services class

 Future<Result<UserModel, NetworkError>> login(UserModel user) async {
    return  NetworkExecuter.execute<UserModel, UserModel>(
        route: UserClient.login(user), responseType: UserModel());
  }

Worse I think we're fine

from cleannetwork.

i-Senku avatar i-Senku commented on September 14, 2024

actually, the problem is not in BaseResponse, it may be because it cannot convert "T" data from json data. Did you solve the problem

from cleannetwork.

i-Senku avatar i-Senku commented on September 14, 2024

Can you have all the requests related to users, for example, registration, login, etc. within a client?

  • Yes you can.

How would the example of the previous case be?

  • Actually, you need to write 1 case for each query and edit fields such as path-method-body-query.

How do I use your easyclient library within VS Code?

  • easyclient is a client builder. It is developed so that you do not copy and paste the client from your old project in each new project. You can install easyclient from the VSCODE extension section. It will work when you type "easyclient".

What is the best way to pass data to client, for example json body or JWT header

  • As an example, let's create a test route with a "POST" request. YourClient.addPost(title : "Test Title", body : "Test Body")
  • You should handle this route in your http client. E.g
The "body" section should be:
addPost(title,body) => {"title" : title, "body" : body}
method :
addPost(title,body) => "POST"
path :
addPost(title,body) => "/addPost"

I think that's exactly what you're asking?

from cleannetwork.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.