From d0914445bac0c716cbd3c1391b6a2c9d6d7e6d8e Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Wed, 10 Aug 2022 08:18:56 +0200 Subject: [PATCH] feat: add FETCH and (i)PATCH methods to the client --- lib/src/coap_client.dart | 158 ++++++++++++++++++++++++++++++++++++++ lib/src/coap_request.dart | 2 +- 2 files changed, 159 insertions(+), 1 deletion(-) diff --git a/lib/src/coap_client.dart b/lib/src/coap_client.dart index 7abf3e36..f8c65f3f 100644 --- a/lib/src/coap_client.dart +++ b/lib/src/coap_client.dart @@ -278,6 +278,164 @@ class CoapClient { return send(request, onMulticastResponse: onMulticastResponse); } + /// Sends a FETCH request. + /// + /// See [RFC 8132, section 2]. + /// + /// [RFC 8132, section 2]: https://www.rfc-editor.org/rfc/rfc8132.html#section-2 + Future fetch( + final String path, { + final CoapMediaType? accept, + final bool confirmable = true, + final List? options, + final bool earlyBlock2Negotiation = false, + final int maxRetransmit = 0, + final CoapMulticastResponseHandler? onMulticastResponse, + }) { + final request = CoapRequest.newFetch(confirmable: confirmable); + _build( + request, + path, + accept, + options, + earlyBlock2Negotiation, + maxRetransmit, + ); + return send(request, onMulticastResponse: onMulticastResponse); + } + + /// Sends a PATCH request. + /// + /// See [RFC 8132, section 3]. + /// + /// [RFC 8132, section 3]: https://www.rfc-editor.org/rfc/rfc8132.html#section-3 + Future patch( + final String path, { + required final String payload, + final CoapMediaType? format, + final CoapMediaType? accept, + final bool confirmable = true, + final List? etags, + final MatchEtags matchEtags = MatchEtags.onMatch, + final List? options, + final bool earlyBlock2Negotiation = false, + final int maxRetransmit = 0, + final CoapMulticastResponseHandler? onMulticastResponse, + }) { + final request = CoapRequest.newPatch(confirmable: confirmable) + ..setPayloadMedia(payload, format); + _build( + request, + path, + accept, + options, + earlyBlock2Negotiation, + maxRetransmit, + etags: etags, + matchEtags: matchEtags, + ); + return send(request, onMulticastResponse: onMulticastResponse); + } + + /// Sends a PATCH request with the specified byte payload. + /// + /// See [RFC 8132, section 3]. + /// + /// [RFC 8132, section 3]: https://www.rfc-editor.org/rfc/rfc8132.html#section-3 + Future patchBytes( + final String path, { + required final Uint8Buffer payload, + final CoapMediaType? format, + final MatchEtags matchEtags = MatchEtags.onMatch, + final List? etags, + final CoapMediaType? accept, + final bool confirmable = true, + final List? options, + final bool earlyBlock2Negotiation = false, + final int maxRetransmit = 0, + final CoapMulticastResponseHandler? onMulticastResponse, + }) { + final request = CoapRequest.newPatch(confirmable: confirmable) + ..setPayloadMediaRaw(payload, format); + _build( + request, + path, + accept, + options, + earlyBlock2Negotiation, + maxRetransmit, + etags: etags, + matchEtags: matchEtags, + ); + return send(request, onMulticastResponse: onMulticastResponse); + } + + /// Sends an iPATCH request. + /// + /// See [RFC 8132, section 3]. + /// + /// [RFC 8132, section 3]: https://www.rfc-editor.org/rfc/rfc8132.html#section-3 + Future iPatch( + final String path, { + required final String payload, + final CoapMediaType? format, + final CoapMediaType? accept, + final bool confirmable = true, + final List? etags, + final MatchEtags matchEtags = MatchEtags.onMatch, + final List? options, + final bool earlyBlock2Negotiation = false, + final int maxRetransmit = 0, + final CoapMulticastResponseHandler? onMulticastResponse, + }) { + final request = CoapRequest.newIPatch(confirmable: confirmable) + ..setPayloadMedia(payload, format); + _build( + request, + path, + accept, + options, + earlyBlock2Negotiation, + maxRetransmit, + etags: etags, + matchEtags: matchEtags, + ); + return send(request, onMulticastResponse: onMulticastResponse); + } + + /// Sends a iPATCH request with the specified byte payload. + /// + /// See [RFC 8132, section 3]. + /// + /// [RFC 8132, section 3]: https://www.rfc-editor.org/rfc/rfc8132.html#section-3 + Future iPatchBytes( + final String path, { + required final Uint8Buffer payload, + final CoapMediaType? format, + final MatchEtags matchEtags = MatchEtags.onMatch, + final List? etags, + final CoapMediaType? accept, + final bool confirmable = true, + final List? options, + final bool earlyBlock2Negotiation = false, + final int maxRetransmit = 0, + final CoapMulticastResponseHandler? onMulticastResponse, + }) { + final request = CoapRequest.newIPatch(confirmable: confirmable) + ..setPayloadMediaRaw(payload, format); + _build( + request, + path, + accept, + options, + earlyBlock2Negotiation, + maxRetransmit, + etags: etags, + matchEtags: matchEtags, + ); + return send(request, onMulticastResponse: onMulticastResponse); + } + /// Observe Future observe( final CoapRequest request, { diff --git a/lib/src/coap_request.dart b/lib/src/coap_request.dart index 1f324e43..50461182 100644 --- a/lib/src/coap_request.dart +++ b/lib/src/coap_request.dart @@ -126,6 +126,6 @@ class CoapRequest extends CoapMessage { CoapRequest(CoapCode.patch, confirmable: confirmable); /// Construct a iPATCH request. - factory CoapRequest.newIpatch({final bool confirmable = true}) => + factory CoapRequest.newIPatch({final bool confirmable = true}) => CoapRequest(CoapCode.ipatch, confirmable: confirmable); }