//Use RestSharp Call API var client = new RestClient($"http://ptx.transportdata.tw/MOTC/v2/Bus/StopOfRoute/City/{city}/{routeName}?%24top=1&%24format=JSON"); var request = new RestRequest(Method.GET); request.AddHeader("cache-control", "no-cache"); IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK) { var APIResult = JsonConvert.DeserializeObject<List<PTXBusRouteResult>>(response.Content);
if (APIResult != null && APIResult.Count > 0) { var Route = APIResult.First(); Result = new BusRouteDTO { Name = Route.RouteName.Zh_tw, BusStops = new List<BusRouteDTO.BusStop>() };
foreach (var stop in Route.Stops) { Result.BusStops.Add(new BusRouteDTO.BusStop { ID = stop.StopUID, Name = stop.StopName.Zh_tw }); } } }
[TestClass()] publicclassPTXTests { [TestMethod()] publicvoid GetTest_傳入縣市和公車路線名稱_如果查的到資料_應回傳該路線的BusRouteDTO() { //arrange var Sut = new PTX(); var City = "Taipei"; var RouteName = "307";
var expected = "307"; //act var actual = Sut.Get(City, RouteName);
if (!string.IsNullOrEmpty(JsonResult)) { var APIResult = JsonConvert.DeserializeObject<List<PTXBusRouteResult>>(JsonResult);
if (APIResult != null && APIResult.Count > 0) { var Route = APIResult.First(); Result = new BusRouteDTO { Name = Route.RouteName.Zh_tw, BusStops = new List<BusRouteDTO.BusStop>() };
foreach (var stop in Route.Stops) { Result.BusStops.Add(new BusRouteDTO.BusStop { ID = stop.StopUID, Name = stop.StopName.Zh_tw }); } } }
return Result; }
///<summary> /// Call API ///</summary> ///<param name="city">縣市</param> ///<param name="routeName">巴士路線名稱</param> ///<returns></returns> privatestringCallAPI(string city, string routeName) { //Use RestSharp Call API var client = new RestClient($"http://ptx.transportdata.tw/MOTC/v2/Bus/StopOfRoute/City/{city}/{routeName}?%24top=1&%24format=JSON"); var request = new RestRequest(Method.GET); request.AddHeader("cache-control", "no-cache"); IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK) { return response.Content; }
[TestMethod()] publicvoid GetTest_傳入縣市和公車路線名稱_如果查的到資料_應回傳該路線的BusRouteDTO() { //arrange var Sut = new PTXStub(); //改成PTXStub var City = "Taipei"; var RouteName = "307";
//API應該回傳的結果 var sb = new System.Text.StringBuilder(12766); sb.AppendLine(@"[{""RouteUID"":""TPE16111"",""RouteID"":""16111"",""RouteName"":{""Zh_tw"":""307"",""En"":""307""},""KeyPattern"":false,""SubRouteUID"":""TPE157462"",""SubRouteID"":""157462"",""SubRouteName"":{""Zh_tw"":""307莒光經板橋前站"",""En"":""307""},""Direction"":0,""Stops"":[{""StopUID"":""TPE15294"",""StopID"":""15294"",""StopName"":{""Zh_tw"":""莊敬里"",]............");