Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12291 manish.sha 1
package com.google.api.services.content.utils;
2
 
3
import com.google.api.client.auth.oauth2.Credential;
4
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
5
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
6
import com.google.api.client.http.HttpTransport;
7
import com.google.api.client.json.JsonFactory;
8
import com.google.api.client.json.jackson2.JacksonFactory;
9
import com.google.api.services.content.ShoppingContent;
10
 
11
import java.io.IOException;
12
import java.math.BigInteger;
13
 
14
/**
15
 * Base class for the API samples.
16
 */
17
public abstract class BaseSample {
18
  protected BigInteger merchantId;
19
  protected ShoppingContent content;
20
 
21
  private final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
22
  private final Credential credential;
23
  private final HttpTransport httpTransport;
24
  private final Config config;
25
 
26
  public BaseSample() {
27
    config = loadConfig();
28
    merchantId = config.getMerchantId();
29
    httpTransport = createHttpTransport();
30
    credential = createCredential();
31
    credential.setRefreshToken(config.getRefreshToken());
32
    content = createContentService();
33
  }
34
 
35
  protected Config loadConfig() {
36
    try {
37
      return Config.load();
38
    } catch (IOException e) {
39
      System.out.println("There was an error while loading configuration.");
40
      e.printStackTrace();
41
      System.exit(1);
42
    }
43
    return null;
44
  }
45
 
46
  protected HttpTransport createHttpTransport() {
47
    try {
48
      return GoogleNetHttpTransport.newTrustedTransport();
49
    } catch (Exception e) {
50
      e.printStackTrace();
51
      System.exit(1);
52
    }
53
    return null;
54
  }
55
 
56
  protected ShoppingContent createContentService() {
57
    return new ShoppingContent.Builder(httpTransport, jsonFactory, credential)
58
        .setApplicationName(config.getApplicationName())
59
        .build();
60
  }
61
 
62
  protected Credential createCredential() {
63
    return new GoogleCredential.Builder()
64
        .setClientSecrets(config.getClientId(), config.getClientSecret())
65
        .setJsonFactory(jsonFactory)
66
        .setTransport(httpTransport)
67
        .build();
68
  }
69
 
70
  public abstract void execute() throws IOException;
71
}