Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec#
firstline1
titleExtract all books
linenumberstrue
class ExtractBooksComponent : IExternalComponent2
{
    //---- Implement IExternalComponent2 

    /// <summary>
    /// Fired after a response is received.
    /// </summary>
    /// <param name="session">The session object</param>
    public void OnAfterResponse(RuntimeSession session)
    {
		var listOfBooks = new List<string>();
		
		var body = session.GetResponseBody();
		
		var regex = new System.Text.RegularExpressions.Regex("{some regular expression}");
		
		//--- regex.Matches(body) ... Match all books and add them to listOfBooks
		
		session.SetObject("ListOfBooks", listOfBooks);		
    }
}


Set the Component hook property of the request to ExtractBooksComponent

Image Added


The next step is to create a scriptable variable, ReturnFirstBook that will be used to parameterize /CheckoutBook request. We will create a scriptable variable that will read the VU storage for the list of available books, remove the first one, and return it.

Code Block
languagec#
firstline1
titleReturn first book
linenumberstrue
class ReturnFirstBook : IExternalVariable
{
    public ExternalConditionReturnFirstBook()
    {
        //Do not edit this section.
    }

    #region IExternalVariable Members

    /// <summary>
    /// Return true if the variable will be evaluated once per VU iteration. Otherwise will be evaluated on every parameter.
    /// </summary>
    bool IExternalVariable.EvaluateOnIteration
    {
        get 
        {
            return false;
        }
    }

    /// <summary>
    /// Return a value for the given session context.
    /// </summary>
    /// <param name="session">The session context object of the request consuming the variable.</param>
    /// <returns>The source variable value.</returns>
    string IExternalVariable.GetValue(SessionContext context)
    {
		var listOfBooks = context.GetObject("ListOfBooks") as List<string>;
		
		string book = null;
		
		if (listOfBooks.Count > 0)
		{
			book = listOfBooks[0];
			listOfBooks.RemoveAt(0);
		}
		return book;
    }

    #endregion
}


Then use the scriptable variable as a parameter in /CheckoutBook.

Image Added

Finally, we will add a scriptable variable that will return the number of available books remaining that have not yet been checked out.

...