private void AddRequestHeader(PostmanRequest postmanRequest, string tokenType)
        {
            string postmanToken = PostmanToken(tokenType);

            postmanRequest.Request.AddHeader("Postman-Token", postmanToken);
            postmanRequest.Request.AddHeader("Cache-Control", "no-cache");
        }
Exemple #2
0
        public PostmanCollection GetDiscovery(string name, Uri uri)
        {
            var explorer          = GlobalConfiguration.Configuration.Services.GetApiExplorer();
            var postManCollection = new PostmanCollection();

            postManCollection.Name      = name;
            postManCollection.Id        = Guid.NewGuid();
            postManCollection.Timestamp = DateTime.Now.Ticks;
            postManCollection.Requests  = new Collection <PostmanRequest>();

            foreach (var apiDescription in explorer.ApiDescriptions)
            {
                var relativePath = apiDescription.RelativePath;

                var request = new PostmanRequest
                {
                    CollectionId = postManCollection.Id,
                    Id           = Guid.NewGuid(),
                    Method       = apiDescription.HttpMethod.Method,
                    Url          = uri.GetRootDomain().TrimEnd('/') + "/" + relativePath,
                    Description  = apiDescription.Documentation,
                    Name         = apiDescription.RelativePath,
                    Data         = "",
                    Headers      = "",
                    DataMode     = "params",
                    Timestamp    = 0
                };

                postManCollection.Requests.Add(request);
            }

            return(postManCollection);
        }
        // STATUS: this works
        // STEP 1
        /// <summary>
        ///     Get the current seasons pitching leaders; Endpoint parameters passed as parameters to method
        /// </summary>
        /// <remarks>
        ///     Parameters for 'PitchingLeadersEndPoint' (i.e. numberToReturn, year, sortColumn) are passed as parameters to the method
        ///     See: 'LeadingPitcher' model for options that you can sort by for this method
        /// </remarks>
        /// <param name="numberToReturn">
        ///     The number of pitchers to return in the results (e.g. 50 would show you the top 50 leaders)
        /// </param>
        /// <param name="year">
        ///     The year that you want to retrieve the leaders for (e.g. 2018 gets you leaders for 2018 mlb season)
        /// </param>
        ///  <param name="sortColumn">
        ///     This is the stat you want to retrieve the leaders for (e.g., Era, Wins, etc)
        /// </param>
        /// <returns>
        ///     A list of instantiated 'LeadingPitching' for 'numberToReturn' number of pitchers
        /// </returns>
        public PitchingLeaders CreatePitchingLeadersModel(int numberToReturn, string year, string sortColumn)
        {
            // retrieve the 'PitchingLeaders' end point
            // * param 1: number of pitchers to include in search
            // * param 2: season that you want to query
            // * param 3: stat that you would like to sort by
            MlbDataEndPoint newEndPoint = _endPoints.PitchingLeadersEndPoint(numberToReturn, year, sortColumn);

            PostmanRequest  postmanRequest  = _postman.CreatePostmanRequest(newEndPoint, "PitchingLeaders");
            PostmanResponse postmanResponse = _postman.GetPostmanResponse(postmanRequest);
            IRestResponse   response        = postmanResponse.Response;

            JObject leadersJObject = _apiInfrastructure.CreateModelJObject(response);
            JToken  leadersJToken  = _apiInfrastructure.CreateModelJToken(leadersJObject, "PitchingLeaders");

            // * Returns object with many 'LeadingPitcher' instances
            // * The number returned depends on first parameter passed when retrieving the 'PitchingLeadersEndPoint' (see above)
            PitchingLeaders newPitchingLeadersInstance = new PitchingLeaders();

            _apiInfrastructure.CreateInstanceOfModel(
                leadersJToken,
                newPitchingLeadersInstance,
                "PitchingLeaders"
                );

            LeadingPitcher newLeadingPitcherInstance = new LeadingPitcher();

            _apiInfrastructure.CreateMultipleInstancesOfModelByLooping(
                leadersJToken,
                newLeadingPitcherInstance,
                "LeadingPitcher"
                );

            return(newPitchingLeadersInstance);
        }
 // STATUS: this works
 public PostmanResponse GetPostmanResponse(PostmanRequest request)
 {
     return(new PostmanResponse
     {
         Response = request.Client.Execute(request.Request),
     });
 }
        // May 22, 2019 status: this works
        // https://appac.github.io/mlb-data-api-docs/#player-data-player-teams-get
        // _pT.GetTeamsForPlayerAllSeasons("493316");
        // http://lookup-service-prod.mlb.com/json/named.player_teams.bam?player_id='493316'
        // MlbDataEndPoint newEndPoint = _endPoints.PlayerTeamsEndPoint("493316");
        public List <PlayerTeam> GetTeamsForPlayerAllSeasons(string playerId)
        {
            MlbDataEndPoint newEndPoint     = _endPoints.PlayerTeamsEndPoint(playerId);
            PostmanRequest  postmanRequest  = _postman.CreatePostmanRequest(newEndPoint, "PlayerTeams");
            PostmanResponse postmanResponse = _postman.GetPostmanResponse(postmanRequest);
            IRestResponse   response        = postmanResponse.Response;

            // jObject is all of the json: player_teams > copyRight, queryResults > created, totalSize, row
            JObject jObject = _apI.CreateModelJObject(response);

            // totalSize --> the size of "row" which is equal to number of teams (e.g., a totalSize of 2 means there are two teams shown for the player in the "row" json header)
            int totalSize = Convert.ToInt32(jObject["player_teams"]["queryResults"]["totalSize"], CultureInfo.CurrentCulture);

            // returns all keys & values for all teams the player played for
            JToken allTeamValuesJToken = _apI.CreateModelJToken(jObject, "PlayerTeam");
            // JToken allTeamValues = jObject["player_teams"]["queryResults"]["row"];

            List <PlayerTeam> ptList = new List <PlayerTeam>();

            for (var teamIndex = 0; teamIndex <= totalSize - 1; teamIndex++)
            {
                PlayerTeam pTeam = new PlayerTeam();

                var pTeamInstance = _apI.CreateInstanceOfModel(allTeamValuesJToken[teamIndex], pTeam, "PlayerTeam") as PlayerTeam;
                ptList.Add(pTeamInstance);
            }
            // Console.WriteLine($"ptList Count: {ptList.Count}");
            return(ptList);
        }
        // STATUS [ July 15, 2019 ] : this works
        public IRestResponse GetPlayerSearchModelPostmanResponse(string playerLastName)
        {
            var             newEndPoint     = _endPoints.PlayerSearchEndPoint(playerLastName);
            PostmanRequest  postmanRequest  = _postman.CreatePostmanRequest(newEndPoint, "PlayerSearch");
            PostmanResponse postmanResponse = _postman.GetPostmanResponse(postmanRequest);
            IRestResponse   response        = postmanResponse.Response;

            return(response);
        }
        public PostmanRequest CreatePostmanRequestFromSwitch(string endPointUri, string tokenTypeForSwitch)
        {
            PostmanRequest postmanRequest = new PostmanRequest
            {
                Client  = new RestClient(endPointUri),
                Request = new RestRequest(Method.GET),
            };

            AddRequestHeader(postmanRequest, tokenTypeForSwitch);
            return(postmanRequest);
        }
        // request from string endPointUri
        public PostmanRequest CreatePostmanRequest(string endPointUri, string postmanToken)
        {
            PostmanRequest postmanRequest = new PostmanRequest
            {
                Client  = new RestClient(endPointUri),
                Request = new RestRequest(Method.GET),
            };

            postmanRequest.Request.AddHeader("Postman-Token", postmanToken);
            postmanRequest.Request.AddHeader("Cache-Control", "no-cache");
            return(postmanRequest);
        }
Exemple #9
0
        // /json/named.sport_pitching_tm.bam?league_list_id='mlb'&game_type={game_type}&season={season}&player_id={player_id}
        // http://lookup-service-prod.mlb.com/json/named.sport_hitting_tm.bam?league_list_id='mlb'&game_type='R'&season='2017'&player_id='592789'
        public HitterSeasonStats CreateHitterSeasonStatsInstance(string year, int playerId)
        {
            MlbDataEndPoint newEndPoint     = _endPoints.HitterSeasonEndPoint("R", year, playerId.ToString(CultureInfo.InvariantCulture));
            PostmanRequest  postmanRequest  = _postman.CreatePostmanRequest(newEndPoint, "HitterSeasonStats");
            PostmanResponse postmanResponse = _postman.GetPostmanResponse(postmanRequest);
            IRestResponse   response        = postmanResponse.Response;

            var jObject = _apiInfrastructure.CreateModelJObject(response);
            var jToken  = _apiInfrastructure.CreateModelJToken(jObject, "HitterSeasonStats");
            var hitter  = _apiInfrastructure.CreateInstanceOfModel(jToken, _hSS, "HitterSeasonStats") as HitterSeasonStats;

            return(hitter);
        }
        // STATUS: this works
        // request from MlbDataEndPoint
        public PostmanRequest CreatePostmanRequest(MlbDataApiEndPoints.MlbDataEndPoint endPoint, string tokenType)
        {
            string endPointUri = endPoint.EndPointUri;

            PostmanRequest postmanRequest = new PostmanRequest
            {
                Client  = new RestClient(endPointUri),
                Request = new RestRequest(Method.GET),
            };

            AddRequestHeader(postmanRequest, tokenType);
            return(postmanRequest);
        }
        public PostmanResponse CreatePostmanRequestGetResponse(string endPointUri, string tokenType)
        {
            PostmanRequest postmanRequest = new PostmanRequest
            {
                Client  = new RestClient(endPointUri),
                Request = new RestRequest(Method.GET),
            };

            AddRequestHeader(postmanRequest, tokenType);

            return(new PostmanResponse
            {
                Response = postmanRequest.Client.Execute(postmanRequest.Request),
            });
        }
Exemple #12
0
 private HttpContent GetContent(PostmanRequest request, PostmanEnvironment environment)
 {
     if (request.Body.Mode == "raw")
     {
         var content = request.Body.Raw.ReplaceWithEnvironment(environment);
         _output.WriteLine(content);
         return(new StringContent(content, System.Text.Encoding.UTF8, request.Header.FirstOrDefault(x => string.Equals(x.Key, "Content-Type", StringComparison.InvariantCultureIgnoreCase))?.Value ?? "application/json"));
     }
     else if (request.Body.Mode == "urlencoded")
     {
         var content = request.Body.Urlencoded.Select(x => KeyValuePair.Create(x.Key, x.Value.ReplaceWithEnvironment(environment)));
         _output.WriteLine(JsonConvert.SerializeObject(content));
         return(new FormUrlEncodedContent(content));
     }
     else if (request.Body.Mode == "formdata")
     {
         var method        = new MultipartFormDataContent();
         var streamContent = new StreamContent(File.Open("AppData/images/bg.jpeg", FileMode.Open));
         method.Add(streamContent, "file", "bg.jpeg");
         return(method);
     }
     return(new StringContent(""));
 }
Exemple #13
0
        public PostmanCollection GetDescriptionForPostman()
        {
            var collection = Configuration.Properties.GetOrAdd("postmanCollection", k =>
            {
                var requestUri = Request.RequestUri;
                var baseUri    = requestUri.Scheme + "://" + requestUri.Host + ":" + requestUri.Port + HttpContext.Current.Request.ApplicationPath;

                var postManCollection       = new PostmanCollection();
                postManCollection.Name      = "Bloodhound API Service";
                postManCollection.Id        = Guid.NewGuid();
                postManCollection.Timestamp = DateTime.UtcNow.Ticks;
                postManCollection.Requests  = new Collection <PostmanRequest>();

                foreach (var apiDescription in Configuration.Services.GetApiExplorer().ApiDescriptions)
                {
                    var request = new PostmanRequest
                    {
                        CollectionId = postManCollection.Id,
                        Id           = Guid.NewGuid(),
                        Method       = apiDescription.HttpMethod.Method,
                        Url          = baseUri.TrimEnd('/') + "/" + apiDescription.RelativePath,
                        Description  = apiDescription.Documentation,
                        Name         = apiDescription.RelativePath,
                        Data         = "",
                        Headers      = "",
                        DataMode     = "params",
                        Timestamp    = 0
                    };

                    postManCollection.Requests.Add(request);
                }

                return(postManCollection);
            }) as PostmanCollection;

            return(collection);
        }