private _sendFeedback()

in device/transport/http/src/http.ts [680:744]


  private _sendFeedback(action: 'abandon' | 'reject' | 'complete', message: Message, done: (err?: Error, result?: any) => void): void {
    /*Codes_SRS_NODE_DEVICE_HTTP_16_032: [All HTTP requests shall obtain the credentials necessary to execute the request by calling `getDeviceCredentials` on the `AuthenticationProvider` object passed to the `Http` constructor.]*/
    this._authenticationProvider.getDeviceCredentials((err, config) => {
      if (err) {
        /*Codes_SRS_NODE_DEVICE_HTTP_16_033: [if the `getDeviceCredentials` fails with an error, the Http request shall call its callback with that error]*/
        done(err);
      } else {
        let method;
        let resultConstructor = null;
        let path = endpoint.deviceFeedbackPath(encodeUriComponentStrict(config.deviceId), message.lockToken);
        const httpHeaders = {
          'If-Match': message.lockToken,
          'User-Agent': this._userAgentString
        };

        this._insertAuthHeaderIfNecessary(httpHeaders, config);

        /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_009: [abandon shall construct an HTTP request using information supplied by the caller, as follows:
        POST <config.host>/devices/URI_ENCODED(<config.deviceId>)/messages/devicebound/<lockToken>/abandon?api-version=<version> HTTP/1.1
        Authorization: <config.sharedAccessSignature>
        If-Match: <lockToken>
        Host: <config.host>]
        */
        if (action === 'abandon') {
          path += '/abandon' + endpoint.versionQueryString();
          method = 'POST';
          resultConstructor = results.MessageAbandoned;
        } else if (action === 'reject') {
          /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_010: [reject shall construct an HTTP request using information supplied by the caller, as follows:
          DELETE <config.host>/devices/URI_ENCODED(<config.deviceId>)/messages/devicebound/<lockToken>?api-version=<version>&reject HTTP/1.1
          Authorization: <config.sharedAccessSignature>
          If-Match: <lockToken>
          Host: <config.host>]*/
          path += endpoint.versionQueryString() + '&reject';
          method = 'DELETE';
          resultConstructor = results.MessageRejected;
        } else {
          /*Codes_SRS_NODE_DEVICE_HTTP_RECEIVER_16_011: [complete shall construct an HTTP request using information supplied by the caller, as follows:
          DELETE <config.host>/devices/URI_ENCODED(<config.deviceId>)/messages/devicebound/<lockToken>?api-version=<version> HTTP/1.1
          Authorization: <config.sharedAccessSignature>
          If-Match: <lockToken>
          Host: <config.host>]*/
          path += endpoint.versionQueryString();
          method = 'DELETE';
          resultConstructor = results.MessageCompleted;
        }

        /*Codes_SRS_NODE_DEVICE_HTTP_05_008: [If any Http method encounters an error before it can send the request, it shall invoke the done callback function and pass the standard JavaScript Error object with a text description of the error (err.message).]*/
        const request = this._http.buildRequest(method, path, httpHeaders, config.host, config.x509, (err, body, response) => {
          if (done) {
            if (!err && response.statusCode < 300) {
              const result = new resultConstructor(response);
              done(null, result);
            } else {
              (<any>err).response = response;
              (<any>err).responseBody = body;
              done(err);
            }
          }
        });

        request.end();
      }
    });
  }