Creating a Cached Application

This example is the next step in the GetItem() call example described in the "Developing an Application with the SDK" section of the chapter. The main difference is that this example uses caching to reduce the need to request information from eBay. In addition, it uses the eBay SDK Integration library to reduce the amount of coding required to keep the database in synch with eBay Web Services. To ensure you have full SDK access, add all of the libraries used in the previous example, plus the eBay_SDK_Integration library that the previous example didn't use.

You'll need a copy of the eBaySdk.mdb database for this example. It's supplied in the \Program Fi1es\eBay\SDK\Database folder. This database contains information about the application, the application users, and the application data itself. It's also important to set up test users for the example. The easiest way to perform this task is to have the sample application do it for you using the procedure found in the "Configuring the Sample Application" section of Chapter 8.

Listing 7.4 shows the essential code for this example—it doesn't repeat the code already explained in Listing 7.2. You'll find the complete source for this example in the \Chapter 07\ Access folder of the source code located on the Sybex Web site.

^ Listing 7.4 Using Caching with the GetItem() Call

Public Sub GetData()

' Create a connection to the database. Set dbStore = New DataStore dbStore.Connect Sheet1.Ce11s(9, 2)

' Load the sandbox environment. Set dbEnv = _

dbStore.LoadEnvironment(EnvironmentEnum.EnvironmentEnum_SANDBOX)

' Load the user information.

Set dbUser = dbEnv.UserManager.LoadUser(l)

' Create the connection between eBay and the database. Set dbSession = dbStore.GetEBaySession(dbUser)

' Get all of the saved items.

Set Allltems = dbSession.LoadItems(AppStatusEnum_Saved)

' Check for the saved item.

For Counter = 0 To Allltems.ItemCount - 1

If A11Items.Item(Counter).ItemId = Sheet1.Ce11s(11, 2) Then

' Load the item when found.

Set Results = AllItems.ItemAt(Counter)

' Check the last time the data was updated. Set ThisApp = Results.AppData

If DateTime.DateDiff("d", ThisApp.AppModDate, DateTime.Now) > 2 Then

' Create the deleted item object. Set Item2De1ete = New IntCollection Item2De1ete.Add ThisApp.appItemId

' Remove the item from the database.

dbSession.SetItemStatus Item2De1ete, AppStatusEnum_De1eted

' Don't use these results. Set Results = Nothing End If

' Exit the loop. Exit For End If Next

' Verify the database contains the required data. If not, get it ' from eBay.

If Results Is Nothing Then

Set Results = Ca11_eBay

' Check for errors. If Results Is Nothing Then

' If there is an error, display it and exit the Sub.

MsgBox "Description: " + Err.Description + vbCrLf + _

"Source: " + Err.Source Err.Clear Exit Sub End If

' Create application update information. Set ThisApp = New ItemAppData

ThisApp.appEnvId = EnvironmentEnum.EnvironmentEnum_SANDBOX ThisApp.AppModDate = DateTime.Now ThisApp.AppStatus = AppStatusEnum_Saved ThisApp.AppUserld = 1

' Add application data to the results. Set Results.AppData = ThisApp

' Save the data to the database. AddResult = dbSession.Saveltem(Results) End If

' Display the information on screen. ... Display Code ...

End Sub

The code begins by creating a connection to the database. It requires a connection string to make the connection. The connection string contains a number of arguments separated by semicolons as shown here (the text normally appears on a single line, but appears on multiple lines in the book):

Provider=Microsoft.Jet.0LEDB.4.0;User ID=Admin;Data Source=D:\Chapter 07\Access\eBaySdk.mdb;Persist Security Info=Fa1se;Jet 0LEDB:Database Password=;Jet 0LEDB:Encrypt Database=True

The information you provide is important. The Provider is a device driver that accesses the database. The Provider shown is the most common for Access users. The User ID argument must include the name of a valid database user—Admin is the default user for many single user setups. If your database is password protected, you must also provide a Database Password argument. The Source argument shows the physical location of the database on your hard drive. These are the mandatory arguments. The remaining arguments are those that eBay suggests for testing and requires for certifying the application. Notice especially the Jet 0LEDB:Encrypt Database argument, which encrypts the content of the database when set to True.

Once the code has created a connection to the database, it uses the connection to load the application environment stored in the database. This environment includes information such as your developer keys and the URLs to use for calls to eBay. Notice that you must supply an environment type as part of the input to this call. The database stores information for several environment types, which means that you can use the same database for multiple applications if desired.

Now that the application knows about the environment in which it will work, the code loads a user. The example code shows a simple number. You obtain this number by looking in the AppUserId field of the AppUser table found in the database. The call is only successful if the user exists and is set up for the application environment. In other words, the call will fail if you try to load a user from the production environment into an application set up for sandbox use.

The code moves on to the next step of creating an eBay session. This session is set up to use the application environment and the user you've loaded. At this point, you can begin working with data.

The first date-oriented task is to load the appropriate items from the database—those that are saved from previous sessions—and check for the target item. The code uses the Load-Items(AppStatusEnum_Saved) method to load items with a specific status—saved. A loop checks each of the items in the AllItems collection for the target ItemId value. If the code locates the appropriate record, then it checks the date the record was last modified using the DateTime.DateDiffO method. At this point, the application has data to display in Results. However, if that data is too old, then the code sets the status of that item to deleted and removes the data from Results. Note that the Integration Library doesn't actually remove the record, so you'll need to perform database maintenance from time to time to remove all of the deleted records. Depending on how much you use eBay, performing maintenance weekly should provide good results.

The next step is to check the status of Results. If the code determines that Results is blank (either because the database didn't have the appropriate record or the data was too old), it obtains the data from eBay using the same technique described in Listing 7.2. The code also saves the data into the database. The data you receive from eBay lacks application data because eBay doesn't know anything about your application. Consequently, the code creates an ItemAppData that contains the application data, fills it with the appropriate information, and saves the record using the SaveItem() method.

Was this article helpful?

0 0
Ebay PowerSeller

Ebay PowerSeller

The Definitive Guide to Becoming an eBay Powerseller guides you through the rules, giving lists of prohibited items, behaviors, and other landmines.

Get My Free Ebook


Responses

  • MARINA
    How to creating an api on ebay?
    9 years ago

Post a comment