Giter VIP home page Giter VIP logo

functions-dart's Issues

Map of objects is not supported by http.post

Hi there,
Thanks alot for implementing this quickly on dart.

I have a function that accepts

{
    "creator": {
        "id": "id",
        "name": "Kosh ",
        "picture": ""
    },
    "receiver": {
        "id": "id",
        "name": "Kosh2 ",
        "picture": ""
    },
    "notificationType": "FOLLOW",
    "body": "",
    "challengeId": "",
    "notificationId": ""
}

while calling function.invoke(body: myObject.toMap()) throws an error and after following the source of the error I found that dart http does not really support Map<String, dynamic> however only accepts Map<String, String> which resulted in an error that is hard to figure.

the solution was to create an extension on Functions and do below:

extension BoltsFunction on FunctionsClient {
  Future<FunctionResponse> stringInvoke(
    String url,
    String functionName, {
    required Map<String, String> headers,
    required String body,
  }) async {
    try {
      final response = await http.post(
        Uri.parse('$url/$functionName'),
        headers: headers,
        body: body,
      );

      dynamic data = json.decode(response.body);
      return FunctionResponse(data: data);
    } catch (error) {
      return FunctionResponse(error: error);
    }
  }
}

And body in here is just a

json.encode({
      'receiver': receiver.toShortMap(),
      'creator': creator.toShortMap(),
      'notificationId': notificationId,
      'challengeId': challengeId,
      'body': body,
      'notificationType': notificationType.name,
    })

I will open a PR that fixes this and instead of using map<String, dynamic> we should accept dynamic allowing callers to determine the type themselves.

here is what http does eventually with body.

if (body != null) {
      if (body is String) {
        request.body = body;
      } else if (body is List) {
        request.bodyBytes = body.cast<int>();
      } else if (body is Map) {
        request.bodyFields = body.cast<String, String>();
      } else {
        throw ArgumentError('Invalid request body "$body".');
      }
    }

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.