-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIAutomation.cs
92 lines (71 loc) · 2.83 KB
/
APIAutomation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using Xunit;
using RestSharp;
using RestSharp.Deserializers;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Diagnostics;
namespace RestAPIAutomation
{
public class APIAutomation :BaseClass
{
ResponseClass _responseclass;
List<ResponseClass> _responselist = new List<ResponseClass>();
public APIAutomation() : base()
{
}
[Fact]
public void GetAPIAutomation()
{
BaseAction action = new BaseAction();
var AllRequests = action.ReadAllRequests();
foreach(var item in AllRequests)
{
var requestname = item.Key;
var value = item.Value.ToString();
var _requestdetails = JsonConvert.DeserializeObject<Dictionary<string,dynamic>>(value);
_responseclass = GetResponse(_requestdetails);
//getting the response of each request
_responselist.Add(_responseclass);
}
var createhtml = GenerateJSON(_responselist);
}
public ResponseClass GetResponse(Dictionary<string, dynamic> item)
{
RestClient _client = new RestClient(BaseURL);
_responseclass = new ResponseClass();
IRestResponse _response;
RestRequest _request ;
var requestmethod = string.Empty;
if(item.Keys.Count>1)
{
requestmethod = item["requestmethod"];
}
switch(requestmethod)
{
case "POST":
_request = new RestRequest(item["requesturl"],Method.POST);
break;
case "PUT":
_request = new RestRequest(item["requesturl"],Method.PUT);
break;
case "DELETE":
_request = new RestRequest(item["requesturl"],Method.DELETE);
break;
default:
_request = new RestRequest(item["requesturl"],Method.GET);
break;
}
Stopwatch _stopwatch = new Stopwatch();
_stopwatch.Start();
_response = _client.Execute(_request);
_stopwatch.Stop();
_responseclass.ResponseTime = _stopwatch.Elapsed.TotalSeconds.ToString();
_responseclass.StatusCode = _response.StatusCode.ToString();
_responseclass.Result = _response.ResponseStatus.ToString();
_responseclass.APIURL = _response.ResponseUri.ToString();
_responseclass.Response = _response.Content;
return _responseclass;
}
}
}