forked from Bynder/bynder-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppSample.java
136 lines (116 loc) · 5.32 KB
/
AppSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright (c) 2019 Bynder B.V. All rights reserved.
*
* Licensed under the MIT License. See LICENSE file in the project root for full license
* information.
*/
package com.bynder.sdk.sample;
import com.bynder.sdk.configuration.Configuration;
import com.bynder.sdk.configuration.OAuthSettings;
import com.bynder.sdk.model.Brand;
import com.bynder.sdk.model.Derivative;
import com.bynder.sdk.model.Media;
import com.bynder.sdk.model.MediaType;
import com.bynder.sdk.model.oauth.RefreshTokenCallback;
import com.bynder.sdk.model.oauth.Token;
import com.bynder.sdk.model.upload.SaveMediaResponse;
import com.bynder.sdk.query.MediaQuery;
import com.bynder.sdk.query.OrderBy;
import com.bynder.sdk.query.upload.UploadQuery;
import com.bynder.sdk.service.BynderClient;
import com.bynder.sdk.service.asset.AssetService;
import com.bynder.sdk.service.oauth.OAuthService;
import com.bynder.sdk.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
/**
* Sample class to display some of the SDK functionality.
*/
public class AppSample {
private static final Logger LOG = LoggerFactory.getLogger(AppSample.class);
public static void main(final String[] args) throws URISyntaxException, IOException {
/**
* Loads app.properties file under src/main/resources
*/
Properties appProperties = Utils.loadConfig("app");
// Initialize BynderClient with a permanent token
BynderClient client = BynderClient.Builder.create(
new Configuration.Builder(new URL(appProperties.getProperty("BASE_URL")))
.setPermanentToken(appProperties.getProperty("PERMANENT_TOKEN")).build());
AssetService assetService = client.getAssetService();
// Call the API to request for media assets
List<Media> mediaList = assetService.getMediaList(
new MediaQuery().setType(MediaType.IMAGE).setOrderBy(OrderBy.NAME_DESC).setLimit(10)
.setPage(1)).blockingSingle().body();
for (Media media : mediaList) {
LOG.info(media.getName());
}
// Upload a file
String filePath = "your-absolute-filepath";
String brandId = "your-brand-id";
UploadQuery uploadQuery = new UploadQuery(filePath, brandId);
// Add the filename you want specifiy in this manner
uploadQuery.setFileName("your-filename");
SaveMediaResponse saveMediaResponse = assetService.uploadFile(uploadQuery).blockingSingle();
// Optional: define callback function to be triggered after access token is auto
// refreshed
RefreshTokenCallback callback = new RefreshTokenCallback() {
@Override
public void execute(Token token) {
LOG.info("Auto refresh triggered!");
LOG.info(String.format("Refresh token used: %s", token.getRefreshToken()));
LOG.info(String.format("New access token: %s", token.getAccessToken()));
LOG.info(String.format("New access token expiration date: %s", token.getAccessTokenExpiration()));
}
};
// Initialize BynderClient with oauth settings to perform OAuth 2.0
// authorization flow
client = BynderClient.Builder.create(
new Configuration.Builder(new URL(appProperties.getProperty("BASE_URL")))
.setOAuthSettings(new OAuthSettings(appProperties.getProperty("CLIENT_ID"),
appProperties.getProperty("CLIENT_SECRET"), new URI(appProperties.getProperty("REDIRECT_URI")),
callback)).build());
// Initialize OAuthService
OAuthService oauthService = client.getOAuthService();
List<String> scopes = Arrays.asList("offline", "asset:read");
URL authorizationUrl = oauthService.getAuthorizationUrl("state example", scopes);
// Open browser with authorization URL
Desktop desktop = Desktop.getDesktop();
desktop.browse(authorizationUrl.toURI());
// Ask for the code returned in the redirect URI
System.out.println("Insert the code: ");
Scanner scanner = new Scanner(System.in);
String code = scanner.nextLine();
scanner.close();
// Get the access token
oauthService.getAccessToken(code, scopes).blockingSingle();
// Call the API to request for the account information
List<Derivative> derivatives = client.getDerivatives().blockingSingle().body();
for (Derivative derivative : derivatives) {
LOG.info(derivative.getPrefix());
}
// Get the asset service
assetService = client.getAssetService();
// Call the API to request for brands
List<Brand> brands = assetService.getBrands().blockingSingle().body();
for (Brand brand : brands) {
LOG.info(brand.getName());
}
// Call the API to request for media assets
mediaList = assetService.getMediaList(
new MediaQuery().setType(MediaType.IMAGE).setOrderBy(OrderBy.NAME_DESC).setLimit(10)
.setPage(1)).blockingSingle().body();
for (Media media : mediaList) {
LOG.info(media.getName());
}
}
}