Exemplo n.º 1
0
        /// <summary>
        /// Endpoint for sending text messages and picture messages using V2 messaging.
        /// </summary>
        /// <param name="accountId">Required parameter: User's account ID.</param>
        /// <param name="body">Required parameter: Example: .</param>
        /// <returns>Returns the ApiResponse of Models.BandwidthMessage response from the API call.</returns>
        public ApiResponse <Models.BandwidthMessage> CreateMessage(
            string accountId,
            Models.MessageRequest body)
        {
            Task <ApiResponse <Models.BandwidthMessage> > t = this.CreateMessageAsync(accountId, body);

            ApiHelper.RunTaskSynchronously(t);
            return(t.Result);
        }
Exemplo n.º 2
0
        public virtual IActionResult Post([FromBody] Models.MessageRequest messageRequest)
        {
            if (messageRequest == null)
            {
                return(BadRequest("No message data"));
            }

            var id          = Guid.NewGuid().ToString();
            var messageInfo = new Models.MessageInfo
            {
                Id      = id,
                Source  = messageRequest.Source,
                Entity  = messageRequest.Entity,
                Time    = messageRequest.Time,
                Payload = messageRequest.Payload,
                Uri     = this.BuildLink($"/messages/{id}")
            };

            //await messsagesRepository.CreateAsync(messageInfo);
            SaveMessage(messageInfo);
            return(Ok(messageInfo));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Endpoint for sending text messages and picture messages using V2 messaging.
        /// </summary>
        /// <param name="accountId">Required parameter: User's account ID.</param>
        /// <param name="body">Required parameter: Example: .</param>
        /// <param name="cancellationToken"> cancellationToken. </param>
        /// <returns>Returns the ApiResponse of Models.BandwidthMessage response from the API call.</returns>
        public async Task <ApiResponse <Models.BandwidthMessage> > CreateMessageAsync(
            string accountId,
            Models.MessageRequest body,
            CancellationToken cancellationToken = default)
        {
            // the base uri for api requests.
            string baseUri = this.Config.GetBaseUri(Server.MessagingDefault);

            // prepare query string for API call.
            StringBuilder queryBuilder = new StringBuilder(baseUri);

            queryBuilder.Append("/users/{accountId}/messages");

            // process optional template parameters.
            ApiHelper.AppendUrlWithTemplateParameters(queryBuilder, new Dictionary <string, object>()
            {
                { "accountId", accountId },
            });

            // append request with appropriate headers and parameters
            var headers = new Dictionary <string, string>()
            {
                { "user-agent", this.UserAgent },
                { "accept", "application/json" },
                { "content-type", "application/json; charset=utf-8" },
            };

            // append body params.
            var bodyText = ApiHelper.JsonSerialize(body);

            // prepare the API call request to fetch the response.
            HttpRequest httpRequest = this.GetClientInstance().PostBody(queryBuilder.ToString(), headers, bodyText);

            if (this.HttpCallBack != null)
            {
                this.HttpCallBack.OnBeforeHttpRequestEventHandler(this.GetClientInstance(), httpRequest);
            }

            httpRequest = await this.AuthManagers["messaging"].ApplyAsync(httpRequest).ConfigureAwait(false);

            // invoke request and get response.
            HttpStringResponse response = await this.GetClientInstance().ExecuteAsStringAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            HttpContext context = new HttpContext(httpRequest, response);

            if (this.HttpCallBack != null)
            {
                this.HttpCallBack.OnAfterHttpResponseEventHandler(this.GetClientInstance(), response);
            }

            if (response.StatusCode == 400)
            {
                throw new MessagingException("400 Request is malformed or invalid", context);
            }

            if (response.StatusCode == 401)
            {
                throw new MessagingException("401 The specified user does not have access to the account", context);
            }

            if (response.StatusCode == 403)
            {
                throw new MessagingException("403 The user does not have access to this API", context);
            }

            if (response.StatusCode == 404)
            {
                throw new MessagingException("404 Path not found", context);
            }

            if (response.StatusCode == 415)
            {
                throw new MessagingException("415 The content-type of the request is incorrect", context);
            }

            if (response.StatusCode == 429)
            {
                throw new MessagingException("429 The rate limit has been reached", context);
            }

            // handle errors defined at the API level.
            this.ValidateResponse(response, context);

            var result = ApiHelper.JsonDeserialize <Models.BandwidthMessage>(response.Body);
            ApiResponse <Models.BandwidthMessage> apiResponse = new ApiResponse <Models.BandwidthMessage>(response.StatusCode, response.Headers, result);

            return(apiResponse);
        }
Exemplo n.º 4
0
        /// <summary>
        /// createMessage
        /// </summary>
        /// <param name="userId">Required parameter: Example: </param>
        /// <param name="body">Optional parameter: Example: </param>
        /// <return>Returns the ApiResponse<Models.BandwidthMessage> response from the API call</return>
        public async Task<ApiResponse<Models.BandwidthMessage>> CreateMessageAsync(string userId, Models.MessageRequest body = null, CancellationToken cancellationToken = default)
        {
            //the base uri for api requests
            string _baseUri = config.GetBaseUri(Server.MessagingDefault);

            //prepare query string for API call
            StringBuilder _queryBuilder = new StringBuilder(_baseUri);
            _queryBuilder.Append("/users/{userId}/messages");

            //process optional template parameters
            ApiHelper.AppendUrlWithTemplateParameters(_queryBuilder, new Dictionary<string, object>()
            {
                { "userId", userId }
            });

            //append request with appropriate headers and parameters
            var _headers = new Dictionary<string, string>()
            {
                { "user-agent", userAgent },
                { "accept", "application/json" },
                { "content-type", "application/json; charset=utf-8" }
            };

            //append body params
            var _body = ApiHelper.JsonSerialize(body);

            //prepare the API call request to fetch the response
            HttpRequest _request = GetClientInstance().PostBody(_queryBuilder.ToString(), _headers, _body);

            _request = await authManagers["messaging"].ApplyAsync(_request).ConfigureAwait(false);

            //invoke request and get response
            HttpStringResponse _response = await GetClientInstance().ExecuteAsStringAsync(_request, cancellationToken).ConfigureAwait(false);
            HttpContext _context = new HttpContext(_request, _response);

            //Error handling using HTTP status codes
            if (_response.StatusCode == 400)
            {
                throw new MessagingException("400 Request is malformed or invalid", _context);
            }

            if (_response.StatusCode == 401)
            {
                throw new MessagingException("401 The specified user does not have access to the account", _context);
            }

            if (_response.StatusCode == 403)
            {
                throw new MessagingException("403 The user does not have access to this API", _context);
            }

            if (_response.StatusCode == 404)
            {
                throw new MessagingException("404 Path not found", _context);
            }

            if (_response.StatusCode == 415)
            {
                throw new MessagingException("415 The content-type of the request is incorrect", _context);
            }

            if (_response.StatusCode == 429)
            {
                throw new MessagingException("429 The rate limit has been reached", _context);
            }

            //handle errors defined at the API level
            base.ValidateResponse(_response, _context);

            var _result = ApiHelper.JsonDeserialize<Models.BandwidthMessage>(_response.Body);
            ApiResponse<Models.BandwidthMessage> apiResponse = new ApiResponse<Models.BandwidthMessage>(_response.StatusCode, _response.Headers, _result);
            return apiResponse;
        }
Exemplo n.º 5
0
 /// <summary>
 /// createMessage
 /// </summary>
 /// <param name="userId">Required parameter: Example: </param>
 /// <param name="body">Optional parameter: Example: </param>
 /// <return>Returns the ApiResponse<Models.BandwidthMessage> response from the API call</return>
 public ApiResponse<Models.BandwidthMessage> CreateMessage(string userId, Models.MessageRequest body = null)
 {
     Task<ApiResponse<Models.BandwidthMessage>> t = CreateMessageAsync(userId, body);
     ApiHelper.RunTaskSynchronously(t);
     return t.Result;
 }