30
Chapter 3: Common Tasks
Reviewing the code
The following table describes the highlighted code and its function:
Code
Description
<cfif IsDefined("FORM.username")>
Checks whether the
username
variable is defined. If
it is, the next block of code executes. If it isn’t, a
form is displayed that allows a user to provide a
user name (and password).
<cfset baseurl = "http://
breezeserveraddress
">
<cfhttp url="#baseurl#/api/
xml?action=login&login=#FORM.username#&
password=#FORM.password#" method="GET"/
>
If the
username
variable is defined, sets the
baseurl
variable to the name of the Breeze server. Uses the
baseurl
variable in the
<cfhttp>
tag to call the
login
API on the Breeze server. The
login
API
requires two parameters,
login
and
password
,
which a user enters into a form.
<cfset response = CFHTTP.FileContent/>
<cfset xml = XMLParse(response)>
Dumps the data returned by the
<cfhttp>
call into
the
response
variable. The second line converts the
string data in the
response
variable into an XML
document object and stores it in the
xml
variable.
<cfset loginHeader = CFHTTP.header>
<cfset loginCookie =
ListLast(ListFirst(loginHeader, ";"),
"=")>
Dumps the header information from the
<cfhttp>
call into the
loginHeader
variable. The second line
parses the header to pull out the value of the
BREEZESESSION
cookie and store it in the
loginCookie
variable.
<cfif
xml.XmlRoot.status.XMLAttributes['code'
] EQ "OK">
<!-- Login succeeded. -->
Checks whether the status code attribute in the
XML response is
ok
.
<cfhttp url="#baseurl#/api/
xml?action=report-my-
meetings&session=#loginCookie#"
method="GET"/>
<cfset meetings = CFHTTP.FileContent/
>
<cfset meetings_xml =
XMLParse(meetings)/>
<cfdump var="#meetings_xml#">
If the status code is
ok
, the
login
API call was
successful and a user is logged in. You can now
pass the
loginCookie
variable as the
session
parameter to act as the logged-in user and call any
API.
This code calls the
report-my-meetings
API,
dumps the response into the
meetings
variable, and
converts the
meetings
variable into an XML
document object.
<cfelse>
<!-- Login failed. -->
Login Failed.
</cfif>
If the status code attribute in the
login
API’s XML
response was not
ok
Login Failed
.
<cfelse>
<form action="#CGI.Script_Name#"
method="POST">
Username: <input type="text"
name="username" size="25"><br>
Password: <input type="password"
name="password" size="25"><br>
<input type="submit"
name="submit" value="Submit"><br>
</form>
</cfif>
If
FORM.username
is not defined, a form is displayed
to gather a user name and password.
Содержание BREEZE 5
Страница 1: ...Breeze Integration Guide ...
Страница 40: ...40 Chapter 3 Common Tasks ...