Skip to content

Commit

Permalink
feat: add FETCH and (i)PATCH methods to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 4, 2022
1 parent 1bcf91e commit d091444
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
158 changes: 158 additions & 0 deletions lib/src/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoapResponse> fetch(
final String path, {
final CoapMediaType? accept,
final bool confirmable = true,
final List<CoapOption>? 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<CoapResponse> patch(
final String path, {
required final String payload,
final CoapMediaType? format,
final CoapMediaType? accept,
final bool confirmable = true,
final List<Uint8Buffer>? etags,
final MatchEtags matchEtags = MatchEtags.onMatch,
final List<CoapOption>? 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<CoapResponse> patchBytes(
final String path, {
required final Uint8Buffer payload,
final CoapMediaType? format,
final MatchEtags matchEtags = MatchEtags.onMatch,
final List<Uint8Buffer>? etags,
final CoapMediaType? accept,
final bool confirmable = true,
final List<CoapOption>? 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<CoapResponse> iPatch(
final String path, {
required final String payload,
final CoapMediaType? format,
final CoapMediaType? accept,
final bool confirmable = true,
final List<Uint8Buffer>? etags,
final MatchEtags matchEtags = MatchEtags.onMatch,
final List<CoapOption>? 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<CoapResponse> iPatchBytes(
final String path, {
required final Uint8Buffer payload,
final CoapMediaType? format,
final MatchEtags matchEtags = MatchEtags.onMatch,
final List<Uint8Buffer>? etags,
final CoapMediaType? accept,
final bool confirmable = true,
final List<CoapOption>? 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<CoapObserveClientRelation> observe(
final CoapRequest request, {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/coap_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit d091444

Please sign in to comment.