Skip to content

Commit 7eb056a

Browse files
authored
[ISSUE-45] Fix bug allowing library to automatically renew expired sessions (#46)
1 parent 8cba394 commit 7eb056a

File tree

10 files changed

+1606
-1239
lines changed

10 files changed

+1606
-1239
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/)
33
and this project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 1.1.2 (02/20/2020)
6+
- Fixed bug preventing the library from auto-renewing a session when it expires.
7+
58
## 1.1.1 (01/07/2020)
69
- Fixed bug in User create endpoint where setting a new user's role could only be set via a RoleId. Now you can set the role by name or id.
710

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.darksci</groupId>
88
<artifactId>pardot-api-client</artifactId>
9-
<version>1.1.1</version>
9+
<version>1.1.2</version>
1010
<packaging>jar</packaging>
1111

1212
<!-- Require Maven 3.5.0 -->

src/main/java/com/darksci/pardot/api/PardotClient.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import com.darksci.pardot.api.request.visitor.VisitorReadRequest;
122122
import com.darksci.pardot.api.request.visitoractivity.VisitorActivityQueryRequest;
123123
import com.darksci.pardot.api.request.visitoractivity.VisitorActivityReadRequest;
124+
import com.darksci.pardot.api.response.ErrorCode;
124125
import com.darksci.pardot.api.response.ErrorResponse;
125126
import com.darksci.pardot.api.response.account.Account;
126127
import com.darksci.pardot.api.response.campaign.Campaign;
@@ -236,9 +237,19 @@ private <T> T submitRequest(final Request request, ResponseParser<T> responsePar
236237
// Parse error response
237238
final ErrorResponse error = new ErrorResponseParser().parseResponse(restResponse.getResponseStr());
238239

240+
// Inspect error code
241+
if (ErrorCode.INVALID_API_OR_USER_KEY.getCode() == error.getCode()) {
242+
// This means the user session has expired. Lets attempt to renew it.
243+
configuration.setApiKey(null);
244+
checkLogin();
245+
246+
// Replay original request
247+
return submitRequest(request, responseParser);
248+
}
249+
239250
// throw exception
240251
throw new InvalidRequestException(error.getMessage(), error.getCode());
241-
} catch (IOException exception) {
252+
} catch (final IOException exception) {
242253
throw new ParserException(exception.getMessage(), exception);
243254
}
244255
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2017, 2018, 2019, 2020 Stephen Powis https://github.com/Crim/pardot-java-client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package com.darksci.pardot.api.response;
19+
20+
/**
21+
* List of Pardot API Response Error Codes.
22+
*
23+
* Incomplete List.
24+
*/
25+
public enum ErrorCode {
26+
// Returned if API Session becomes invalid.
27+
INVALID_API_OR_USER_KEY(1),
28+
INVALID_ACTION(2),
29+
INVALID_PROSPECT_ID(3),
30+
INVALID_PROSPECT_EMAIL_ADDRESS(4),
31+
INVALID_USER_ID(10),
32+
INVALID_ID(11),
33+
// Returned if authentication credentials are invalid.
34+
LOGIN_FAILED(15),
35+
INVALID_CAMPAIGN_ID(38),
36+
EMAIL_ADDRESS_IS_ALREADY_IN_USE(54),
37+
INVALID_LIST_ID(55),
38+
INVALID_EMAIL_FORMAT(65);
39+
40+
private final int code;
41+
42+
ErrorCode(final int code) {
43+
this.code = code;
44+
}
45+
46+
public int getCode() {
47+
return code;
48+
}
49+
}

0 commit comments

Comments
 (0)