Пример #1
0
        public async Task <BaseResponseDto <UpdateOrderResponseDto> > UpdateOrder(
            UpdateOrderRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var payload = new UpdateOrderRequestRemoteDto
            {
                transport_type = TransportTypeToString(request.TransportType),
                cashed         = request.Cashed,
                credit         = request.Credit,
                delay          = request.Delay,
                has_return     = request.HasReturn,
                pay_at_dest    = request.PayAtDest
            };
            var payloadJson = JsonSerializer.Serialize(payload);

            var path = UpdateOrderV2EndpointPath.Replace(OrderIdPlaceholder, request.OrderId);

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            var content = new StringContent(payloadJson, Encoding.UTF8, ApplicationJsonMime);

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(
                        new HttpRequestMessage(HttpMethod.Put, path)
                    {
                        Content = content
                    },
                        cancellationToken
                        );

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <UpdateOrderResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result =
                JsonSerializer.Deserialize <RemoteBaseResponseDto <UpdateOrderResponseRemoteDto> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            UpdateOrderResponseDto resultObject = null;

            if (!(obj is null))
            {
                resultObject = new UpdateOrderResponseDto();
                MapGetOrderDetailsResponseRDtoToResponseDto(resultObject, obj);
            }

            var output = new BaseResponseDto <UpdateOrderResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = resultObject
            };

            BindBaseResponse(output, response);

            return(output);
        }
        public async Task <BaseResponseDto <IEnumerable <GetPriceResponseDto> > > GetPrices(
            IEnumerable <GetPriceRequestDto> requests,
            CancellationToken cancellationToken
            )
        {
            if (requests is null)
            {
                throw new ArgumentNullException(nameof(requests));
            }

            var payload     = requests.Select(GetPriceLocalRequestToRemoteRequest);
            var payloadJson = JsonSerializer.Serialize(payload);

            var path = GetPricesV2EndpointPath;

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            var content = new StringContent(payloadJson, Encoding.UTF8, ApplicationJsonMime);

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(
                        new HttpRequestMessage(HttpMethod.Post, path)
                    {
                        Content = content
                    },
                        cancellationToken
                        );

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <IEnumerable <GetPriceResponseDto> >(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result = JsonSerializer.Deserialize <RemoteBaseResponseDto <GetPriceResponseRemoteDto[]> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var output = new BaseResponseDto <IEnumerable <GetPriceResponseDto> >
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = result.@object?
                          .Select(GetPriceRemoteResponseToLocalResponse)
                          .ToArray()
            };

            BindBaseResponse(output, response);

            return(output);
        }
Пример #3
0
        public async Task <BaseResponseDto <GetOrderDetailsResponseDto> > GetOrderDetails(
            GetOrderDetailsRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var path = GetOrderDetailsV2EndpointPath.Replace(OrderIdPlaceholder, request.OrderId);

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(new HttpRequestMessage(HttpMethod.Get, path), cancellationToken);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <GetOrderDetailsResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result =
                JsonSerializer.Deserialize <RemoteBaseResponseDto <GetOrderDetailsResponseRemoteDto> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <GetOrderDetailsResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = obj is null
                    ? null
                    : MapGetOrderDetailsResponseRDtoToResponseDto(obj)
            };

            BindBaseResponse(output, response);

            return(output);
        }
        public async Task <BaseResponseDto <InsertOrderResponseDto> > InsertOrder(
            InsertOrderRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Addresses is null)
            {
                throw new AlopeykException("Addresses must have value for GetPrice()");
            }

            if (request.Addresses.Origin is null || request.Addresses.Destinations is null ||
                !request.Addresses.Destinations.Any())
            {
                throw new AlopeykException("Addresses must have origin and at least one destination for GetPrice()");
            }

            var payload = new InsertOrderRequestRemoteDto
            {
                transport_type = TransportTypeToString(request.TransportType),
                cashed         = request.Cashed,
                delay          = request.DelayInMinutes,
                has_return     = request.HasReturn,
                scheduled_at   = request.ScheduledAt,
                extra_param    = request.ExtraParam,

                addresses = request.Addresses.Destinations.Select(
                    dst => new InsertOrderAddressRequestRemoteDto
                {
                    type            = "destination",
                    lng             = dst.Longitude,
                    lat             = dst.Latitude,
                    description     = dst.Description,
                    number          = dst.Number ?? "",
                    person_fullname = dst.PersonFullName,
                    person_phone    = dst.PersonPhone,
                    unit            = dst.Unit ?? "",
                }
                    ).Prepend(new InsertOrderAddressRequestRemoteDto
                {
                    type            = "origin",
                    lng             = request.Addresses.Origin.Longitude,
                    lat             = request.Addresses.Origin.Latitude,
                    description     = request.Addresses.Origin.Description,
                    number          = request.Addresses.Origin.Number ?? "",
                    person_fullname = request.Addresses.Origin.PersonFullName,
                    person_phone    = request.Addresses.Origin.PersonPhone,
                    unit            = request.Addresses.Origin.Unit ?? "",
                }).ToArray()
            };
            var payloadJson = JsonSerializer.Serialize(payload);

            var path = InsertOrderV2EndpointPath;

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            var content = new StringContent(payloadJson, Encoding.UTF8, ApplicationJsonMime);

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(
                        new HttpRequestMessage(HttpMethod.Post, path)
                    {
                        Content = content
                    },
                        cancellationToken
                        );

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <InsertOrderResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result =
                JsonSerializer.Deserialize <RemoteBaseResponseDto <InsertOrderResponseRemoteDto> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <InsertOrderResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = obj is null
                    ? null
                    : new InsertOrderResponseDto
                {
                    Id            = obj.id,
                    TransportType = StringToTransportType(obj.transport_type),
                    Status        = FormatOrderStatusCode(obj.status),
                    Cashed        = obj.cashed,
                    City          = obj.city,
                    Credit        = obj.credit,
                    Delay         = obj.delay,
                    Distance      = obj.distance,
                    Duration      = obj.duration,
                    Price         = obj.price,
                    Signature     = obj.signature is null
                            ? null
                            : new ResourceDescriptorDto
                    {
                        Url = obj.signature.Url
                    },
                    Subsidy       = obj.subsidy,
                    Weight        = obj.weight,
                    CreatedAt     = obj.created_at,
                    CustomerId    = obj.customer_id,
                    DeviceId      = obj.device_id,
                    ExtraParam    = obj.extra_param,
                    FinalPrice    = obj.final_price,
                    HasReturn     = obj.has_return,
                    InvoiceNumber = obj.invoice_number,
                    IsApi         = obj.is_api,
                    IsVip         = obj.is_vip,
                    LaunchedAt    = obj.launched_at,
                    NPrice        = obj.nprice,
                    OrderDiscount = obj.order_discount,
                    OrderToken    = obj.order_token,
                    ScoreInfo     = obj.score_calc is null
                            ? null
                            : new InsertOrderScoreInfoDto
                    {
                        Score       = obj.score_calc.score,
                        ScoreDetail = obj.score_calc.score_detail
                    },
                    SignedBy              = obj.signed_by,
                    UpdatedAt             = obj.updated_at,
                    PayAtDest             = obj.pay_at_dest,
                    TrafficCongestionZone = obj.traffic_congestion_zone,
                    TrafficOddEvenZone    = obj.traffic_odd_even_zone
                }
            };

            BindBaseResponse(output, response);

            return(output);
        }
        public async Task <BaseResponseDto <RateOrderResponseDto> > RateOrder(
            RateOrderRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var path = RateOrderV2EndpointPath.Replace(OrderIdPlaceholder, request.OrderId);

            var payload = new RateOrderRequestRemoteDto
            {
                rate    = request.Rate,
                comment = request.Comment
            };
            var payloadJson = JsonSerializer.Serialize(payload);

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            var content = new StringContent(payloadJson, Encoding.UTF8, ApplicationJsonMime);

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(
                        new HttpRequestMessage(HttpMethod.Post, path)
                    {
                        Content = content
                    },
                        cancellationToken
                        );

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <RateOrderResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result = JsonSerializer.Deserialize <RemoteBaseResponseDto <RateOrderResponseRemoteDto> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <RateOrderResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = obj is null
                    ? null
                    : new RateOrderResponseDto
                {
                    TransportType = StringToTransportType(obj.transport_type),
                    Id            = obj.id,
                    City          = obj.city,
                    Price         = obj.price,
                    Rate          = obj.rate,
                    Signature     = obj.signature is null
                            ? null
                            : new ResourceDescriptorDto
                    {
                        Url = obj.signature.Url
                    },
                    Status         = obj.status,
                    Subsidy        = obj.subsidy,
                    AcceptedAt     = obj.accepted_at,
                    CourierId      = obj.courier_id,
                    CustomerId     = obj.customer_id,
                    FinalPrice     = obj.final_price,
                    NPrice         = obj.nprice,
                    OrderToken     = obj.order_token,
                    SignedBy       = obj.signed_by,
                    NextAddressAny = obj.next_address_any
                }
            };

            BindBaseResponse(output, response);

            return(output);
        }
        public async Task <BaseResponseDto <DeleteHiddenDescriptionResponseDto> > DeleteHiddenDescription(
            DeleteHiddenDescriptionRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var path = DeleteHiddenDescriptionV2EndpointPath.Replace(OrderIdPlaceholder, request.OrderId);

            path = path.Replace(AddressIdPlaceholder, request.AddressId);
            path = path.Replace(HiddenDescriptionIdPlaceholder, request.HiddenDescriptionId);

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(
                        new HttpRequestMessage(HttpMethod.Delete, path),
                        cancellationToken
                        );

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <DeleteHiddenDescriptionResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result =
                JsonSerializer.Deserialize <RemoteBaseResponseDto <DeleteHiddenDescriptionResponseRemoteDto> >(
                    responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <DeleteHiddenDescriptionResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = new DeleteHiddenDescriptionResponseDto
                {
                    Id          = obj.id,
                    Description = obj.description,
                    AddressId   = obj.address_id,
                    CreatedAt   = obj.created_at,
                    OrderId     = obj.order_id,
                    UpdatedAt   = obj.updated_at,
                    UserId      = obj.user_id
                }
            };

            BindBaseResponse(output, response);

            return(output);
        }
Пример #7
0
        public async Task <BaseResponseDto <GetLocationSuggestionsResponseDto[]> > GetLocationSuggestions(
            GetLocationSuggestionsRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var input = Uri.EscapeDataString(request.Input);

            var path = GetLocationSuggestionsV2EndpointPath;

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            path = CreatePath(path);

            do
            {
                try
                {
                    path = $"{path}?input={input}";

                    response = await Send(new HttpRequestMessage(HttpMethod.Get, path), cancellationToken);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <GetLocationSuggestionsResponseDto[]>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result = JsonSerializer.Deserialize <RemoteBaseResponseDto <GetLocationSuggestionsResponseRemoteDto[]> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <GetLocationSuggestionsResponseDto[]>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = obj?.Select(o => new GetLocationSuggestionsResponseDto
                {
                    Title     = o.title,
                    City      = o.city,
                    CityFa    = o.city_fa,
                    District  = o.district,
                    Region    = o.region,
                    Latitude  = o.lat,
                    Longitude = o.lng
                }).ToArray()
            };

            BindBaseResponse(output, response);

            return(output);
        }
        public virtual async Task <BaseResponseDto <GetLocationResponseDto> > GetLocation(
            GetLocationRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var latlng = Uri.EscapeDataString($"{request.Latitude} {request.Longitude}");

            var path = GetLocationV2EndpointPath;

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            path = CreatePath(path);

            do
            {
                try
                {
                    path = $"{path}?latlng={latlng}";

                    response = await Send(new HttpRequestMessage(HttpMethod.Get, path), cancellationToken);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <GetLocationResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result = JsonSerializer.Deserialize <RemoteBaseResponseDto <GetLocationResponseRemoteDto> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <GetLocationResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = obj is null
                    ? null
                    : new GetLocationResponseDto
                {
                    Province    = obj.province,
                    City        = obj.city,
                    CityFa      = obj.city_fa,
                    District    = obj.district,
                    Region      = obj.region,
                    Address     = obj.address,
                    TrafficZone = obj.traffic_zone is null
                            ? null
                            : new TrafficZoneDto
                    {
                        Congestion = obj.traffic_zone.congestion,
                        OddEven    = obj.traffic_zone.odd_even
                    }
                }
            };

            BindBaseResponse(output, response);

            return(output);
        }
        public async Task <BaseResponseDto <CancelOrderResponseDto> > CancelOrder(
            CancelOrderRequestDto request,
            CancellationToken cancellationToken
            )
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var path = CancelOrderV2EndpointPath.Replace(OrderIdPlaceholder, request.OrderId);

            var retryContext = await RetryHandler.BeginTry(cancellationToken);

            HttpResponseMessage response = null;
            bool retry = false;

            path = CreatePath(path);

            do
            {
                try
                {
                    response = await Send(
                        new HttpRequestMessage(HttpMethod.Get, path),
                        cancellationToken
                        );

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await ThrowOnInvalidStatusCode <CancelOrderResponseDto>(response));
                    }

                    await RetryHandler.EndTry(retryContext, cancellationToken);
                }
                catch (Exception ex)
                {
                    retry = await RetryHandler.CatchException(retryContext, ex, cancellationToken);
                }
            } while (retry);

            if (response is null)
            {
                throw new InvalidOperationException();
            }

            var responseStream = await response.Content.ReadAsStreamAsync();

            var result =
                JsonSerializer.Deserialize <RemoteBaseResponseDto <CancelOrderResponseRemoteDto> >(responseStream);

            if (result is null)
            {
                throw new AlopeykException("Object was empty in alopeyk's response.");
            }

            var obj = result.@object;

            var output = new BaseResponseDto <CancelOrderResponseDto>
            {
                Status  = FormatStatusCode(result.status),
                Message = result.message,
                Object  = new CancelOrderResponseDto
                {
                    Id         = obj.id,
                    Status     = FormatOrderStatusCode(obj.status),
                    SignedBy   = obj.signed_by,
                    Subsidy    = obj.subsidy,
                    CourierId  = obj.courier_id,
                    CustomerId = obj.customer_id,
                    FinalPrice = obj.final_price,
                    NPrice     = obj.nprice,
                    OrderToken = obj.order_token,
                    Signature  = obj.signature is null
                        ? null
                        : new ResourceDescriptorDto
                    {
                        Url = obj.signature.Url
                    }
                }
            };

            BindBaseResponse(output, response);

            return(output);
        }