0

I have an API to find data, and this how it looks : "/api/v1/ProjectList/find"

The BE will create this link with 1 field to search

http://localhost:8081/api/v1/ProjectList/find?fields=Id&values=10&types=Int32&operators=%3D

and this link with 2 or more fields

http://localhost:8081/api/v1/ProjectList/find?fields=Id&fields=Name&values=10&values=Test&types=Int32&types=string&operators=%3D&operators=%3D

I have GetMethod() to get my List.

public List<T> GetMethod<T>(string uri, Dictionary<string, string> headers = null)
    {
        var client = new RestClient(uri);
        var request = new RestRequest(uri,Method.Get) { RequestFormat = DataFormat.Json };
        var result = GetResult<T>(client, request, null, headers);
        return result;
    }
private List<T> GetResult<T>(RestClient client, RestRequest request, object requestObject = null, Dictionary<string, string> headers = null)
    {
        if (headers != null)
        {
            foreach (var header in headers)
            {
                request.AddHeader(header.Key, header.Value);
            }
        }

        if (requestObject != null)
        {
            request.AddJsonBody(requestObject);
        }
        request.AddHeader("authorization", $"bearer {SettingsModule.CurrentUser.AccessToken}");

        var response = client.Execute(request);

        //ar jObject = JObject.Parse(response.Content);
        

        return JsonConvert.DeserializeObject<List<T>>(response.Content, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

    }

But I don't know how to insert these fields inside the URI with parameters. and how to call it inside the URI.

const string uri = "http://localhost:8081/api/v1";
IApiService client = new ApiServiceRestSharp();
var getUri = $"{uri}/{"MdBenefitMain/find"}";
var a = client.GetMethod<MdBenefitMain>(getUri);

What is missing from my code to use find API? and 1 more question, if the sort order should applied or not?

MedAmin
  • 35
  • 6

1 Answers1

0

I found how it works and I update my code, and it works with one example.

var body = new
                {
                    fields = "Id",
                    values = "20",
                    types = "Int32",
                    operators = "="
                    
                };

var a = client.GetMethod<MdBenefitMain>(getUri, body);

then I fetch the Object bbody and I add the parameter to Reques

foreach (var item in parameterRequest.ToKeyValue())
        {
            request.AddParameter(item.Key, item.Value);
        }

In this way I get the filtered data. But My problem if I have a generic Model and more than 1 field to filter, how should I proceed. For example I wan to use LunaSearch with creating the object to filter, but they are different output.

MedAmin
  • 35
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 13 '22 at 13:12