/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using System; using System.ComponentModel; using System.Runtime.Serialization; using OpenSearch.Net; using OpenSearch.Net.Utf8Json; namespace OpenSearch.Client { [InterfaceDataContract] public interface IRequest { [IgnoreDataMember] string ContentType { get; } [IgnoreDataMember] HttpMethod HttpMethod { get; } [IgnoreDataMember] RouteValues RouteValues { get; } [IgnoreDataMember] IRequestParameters RequestParameters { get; } string GetUrl(IConnectionSettingsValues settings); } public interface IRequest : IRequest where TParameters : class, IRequestParameters, new() { /// /// Used to describe request parameters that are not part of the body. e.g. query string, connection configuration /// overrides, etc. /// [IgnoreDataMember] new TParameters RequestParameters { get; } } public abstract class RequestBase : IRequest where TParameters : class, IRequestParameters, new() { // ReSharper disable once VirtualMemberCallInConstructor protected RequestBase() { _parameters = new TParameters(); // ReSharper disable once VirtualMemberCallInConstructor RequestDefaults(_parameters); } protected RequestBase(Func pathSelector) { pathSelector(RequestState.RouteValues); _parameters = new TParameters(); // ReSharper disable once VirtualMemberCallInConstructor RequestDefaults(_parameters); } protected virtual HttpMethod HttpMethod => RequestState.RequestParameters.DefaultHttpMethod; [IgnoreDataMember] protected IRequest RequestState => this; [IgnoreDataMember] HttpMethod IRequest.HttpMethod => HttpMethod; [IgnoreDataMember] string IRequest.ContentType => ContentType; protected virtual string ContentType { get; } = null; private readonly TParameters _parameters; [IgnoreDataMember] TParameters IRequest.RequestParameters => _parameters; IRequestParameters IRequest.RequestParameters => _parameters; [IgnoreDataMember] RouteValues IRequest.RouteValues { get; } = new RouteValues(); internal abstract ApiUrls ApiUrls { get; } string IRequest.GetUrl(IConnectionSettingsValues settings) => ResolveUrl(RequestState.RouteValues, settings); protected virtual string ResolveUrl(RouteValues routeValues, IConnectionSettingsValues settings) => ApiUrls.Resolve(routeValues, settings); /// /// Allows a request implementation to set certain request parameter defaults, use sparingly! /// protected virtual void RequestDefaults(TParameters parameters) { } protected TOut Q(string name) => RequestState.RequestParameters.GetQueryStringValue(name); protected void Q(string name, object value) => RequestState.RequestParameters.SetQueryString(name, value); protected void SetAcceptHeader(string format) { if (RequestState.RequestParameters.RequestConfiguration == null) RequestState.RequestParameters.RequestConfiguration = new RequestConfiguration(); RequestState.RequestParameters.RequestConfiguration.Accept = RequestState.RequestParameters.AcceptHeaderFromFormat(format); } } public abstract partial class PlainRequestBase : RequestBase where TParameters : class, IRequestParameters, new() { protected PlainRequestBase() { } protected PlainRequestBase(Func pathSelector) : base(pathSelector) { } /// /// Specify settings for this request alone, handy if you need a custom timeout or want to bypass sniffing, retries /// public IRequestConfiguration RequestConfiguration { get => RequestState.RequestParameters.RequestConfiguration; set => RequestState.RequestParameters.RequestConfiguration = value; } } /// /// Base class for all Request descriptor types /// public abstract partial class RequestDescriptorBase : RequestBase, IDescriptor where TDescriptor : RequestDescriptorBase, TInterface where TParameters : RequestParameters, new() { private readonly TDescriptor _descriptor; protected RequestDescriptorBase() => _descriptor = (TDescriptor)this; protected RequestDescriptorBase(Func pathSelector) : base(pathSelector) => _descriptor = (TDescriptor)this; protected TInterface Self => _descriptor; protected TDescriptor Assign(TValue value, Action assign) => Fluent.Assign(_descriptor, value, assign); protected TDescriptor Qs(string name, object value) { Q(name, value); return _descriptor; } /// /// Specify settings for this request alone, handy if you need a custom timeout or want to bypass sniffing, retries /// public TDescriptor RequestConfiguration(Func configurationSelector) { var rc = RequestState.RequestParameters.RequestConfiguration; RequestState.RequestParameters.RequestConfiguration = configurationSelector?.Invoke(new RequestConfigurationDescriptor(rc)) ?? rc; return _descriptor; } /// /// Hides the method. /// [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] // ReSharper disable BaseObjectEqualsIsObjectEquals public override bool Equals(object obj) => base.Equals(obj); /// /// Hides the method. /// [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] // ReSharper disable once BaseObjectGetHashCodeCallInGetHashCode public override int GetHashCode() => base.GetHashCode(); // ReSharper restore BaseObjectEqualsIsObjectEquals /// /// Hides the method. /// [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] public override string ToString() => base.ToString(); } }