From 82014990727b88ac9358812aff44f2c3ecb5a54c Mon Sep 17 00:00:00 2001 From: Johanan Lancaon Date: Wed, 14 Nov 2012 12:11:17 -0800 Subject: [PATCH 1/3] Add ship from location to ItemListing. This will work with an updated version of the CSV file that has address info in it. --- .../scribe/builder/MagentoServiceBuilder.java | 170 ++++++++++++++++++ .../org/scribe/builder/ServiceBuilder.java | 16 +- .../scribe/builder/api/ApiInitializer.java | 13 ++ .../builder/api/MagentoDefaultApi10a.java | 15 ++ .../scribe/builder/api/MagentoOauth10a.java | 39 ++++ .../scribe/examples/Magento10aExample.java | 81 +++++++++ 6 files changed, 326 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/scribe/builder/MagentoServiceBuilder.java create mode 100644 src/main/java/org/scribe/builder/api/ApiInitializer.java create mode 100644 src/main/java/org/scribe/builder/api/MagentoDefaultApi10a.java create mode 100644 src/main/java/org/scribe/builder/api/MagentoOauth10a.java create mode 100644 src/test/java/org/scribe/examples/Magento10aExample.java diff --git a/src/main/java/org/scribe/builder/MagentoServiceBuilder.java b/src/main/java/org/scribe/builder/MagentoServiceBuilder.java new file mode 100644 index 000000000..415538bfd --- /dev/null +++ b/src/main/java/org/scribe/builder/MagentoServiceBuilder.java @@ -0,0 +1,170 @@ +package org.scribe.builder; + +import java.io.*; +import org.scribe.builder.api.*; +import org.scribe.exceptions.*; +import org.scribe.model.*; +import org.scribe.oauth.*; +import org.scribe.utils.*; + +/** + * Implementation of the Builder pattern, with a fluent interface that creates a + * {@link OAuthService} + * + * @author z + * + */ +public class MagentoServiceBuilder +{ + protected String apiKey; + protected String apiSecret; + protected String callback; + protected ApiInitializer api; + protected String scope; + protected SignatureType signatureType; + protected OutputStream debugStream; + + /** + * Default constructor + */ + public MagentoServiceBuilder() + { + this.callback = OAuthConstants.OUT_OF_BAND; + this.signatureType = SignatureType.Header; + this.debugStream = null; + } + + /** + * Configures the {@link Api} + * + * @param apiClass the class of one of the existent {@link Api}s on org.scribe.api package + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder provider(Class apiClass, String baseURL) + { + this.api = createApi(apiClass); + this.api.setBaseURL(baseURL); + return this; + } + + protected ApiInitializer createApi(Class apiClass) + { + Preconditions.checkNotNull(apiClass, "Api class cannot be null"); + ApiInitializer api; + try + { + api = apiClass.newInstance(); + } + catch(Exception e) + { + throw new OAuthException("Error while creating the Api object", e); + } + return api; + } + + /** + * Configures the {@link Api} + * + * Overloaded version. Let's you use an instance instead of a class. + * + * @param api instance of {@link Api}s + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder provider(ApiInitializer api) + { + Preconditions.checkNotNull(api, "Api cannot be null"); + this.api = api; + return this; + } + + /** + * Adds an OAuth callback url + * + * @param callback callback url. Must be a valid url or 'oob' for out of band OAuth + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder callback(String callback) + { + Preconditions.checkNotNull(callback, "Callback can't be null"); + this.callback = callback; + return this; + } + + /** + * Configures the api key + * + * @param apiKey The api key for your application + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder apiKey(String apiKey) + { + Preconditions.checkEmptyString(apiKey, "Invalid Api key"); + this.apiKey = apiKey; + return this; + } + + /** + * Configures the api secret + * + * @param apiSecret The api secret for your application + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder apiSecret(String apiSecret) + { + Preconditions.checkEmptyString(apiSecret, "Invalid Api secret"); + this.apiSecret = apiSecret; + return this; + } + + /** + * Configures the OAuth scope. This is only necessary in some APIs (like Google's). + * + * @param scope The OAuth scope + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder scope(String scope) + { + Preconditions.checkEmptyString(scope, "Invalid OAuth scope"); + this.scope = scope; + return this; + } + + /** + * Configures the signature type, choose between header, querystring, etc. Defaults to Header + * + * @param scope The OAuth scope + * @return the {@link MagentoServiceBuilder} instance for method chaining + */ + public MagentoServiceBuilder signatureType(SignatureType type) + { + Preconditions.checkNotNull(type, "Signature type can't be null"); + this.signatureType = type; + return this; + } + + public MagentoServiceBuilder debugStream(OutputStream stream) + { + Preconditions.checkNotNull(stream, "debug stream can't be null"); + this.debugStream = stream; + return this; + } + + public MagentoServiceBuilder debug() + { + this.debugStream(System.out); + return this; + } + + /** + * Returns the fully configured {@link OAuthService} + * + * @return fully configured {@link OAuthService} + */ + public OAuthService build() + { + Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method"); + Preconditions.checkEmptyString(apiKey, "You must provide an api key"); + Preconditions.checkEmptyString(apiSecret, "You must provide an api secret"); + return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope, debugStream)); + } +} diff --git a/src/main/java/org/scribe/builder/ServiceBuilder.java b/src/main/java/org/scribe/builder/ServiceBuilder.java index 27f3c8f63..715d2130d 100644 --- a/src/main/java/org/scribe/builder/ServiceBuilder.java +++ b/src/main/java/org/scribe/builder/ServiceBuilder.java @@ -16,13 +16,13 @@ */ public class ServiceBuilder { - private String apiKey; - private String apiSecret; - private String callback; - private Api api; - private String scope; - private SignatureType signatureType; - private OutputStream debugStream; + protected String apiKey; + protected String apiSecret; + protected String callback; + protected Api api; + protected String scope; + protected SignatureType signatureType; + protected OutputStream debugStream; /** * Default constructor @@ -46,7 +46,7 @@ public ServiceBuilder provider(Class apiClass) return this; } - private Api createApi(Class apiClass) + protected Api createApi(Class apiClass) { Preconditions.checkNotNull(apiClass, "Api class cannot be null"); Api api; diff --git a/src/main/java/org/scribe/builder/api/ApiInitializer.java b/src/main/java/org/scribe/builder/api/ApiInitializer.java new file mode 100644 index 000000000..e785ff683 --- /dev/null +++ b/src/main/java/org/scribe/builder/api/ApiInitializer.java @@ -0,0 +1,13 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.scribe.builder.api; + +/** + * + * @author javageek + */ +public interface ApiInitializer extends Api { + String setBaseURL(String baseURL); +} diff --git a/src/main/java/org/scribe/builder/api/MagentoDefaultApi10a.java b/src/main/java/org/scribe/builder/api/MagentoDefaultApi10a.java new file mode 100644 index 000000000..536355858 --- /dev/null +++ b/src/main/java/org/scribe/builder/api/MagentoDefaultApi10a.java @@ -0,0 +1,15 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.scribe.builder.api; + +/** + * + * @author javageek + */ +public abstract class MagentoDefaultApi10a extends DefaultApi10a implements ApiInitializer { + + public abstract String setBaseURL(String baseURL); + +} diff --git a/src/main/java/org/scribe/builder/api/MagentoOauth10a.java b/src/main/java/org/scribe/builder/api/MagentoOauth10a.java new file mode 100644 index 000000000..e9810ff36 --- /dev/null +++ b/src/main/java/org/scribe/builder/api/MagentoOauth10a.java @@ -0,0 +1,39 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.scribe.builder.api; +import org.scribe.exceptions.OAuthException; +import org.scribe.model.Token; + +/** + * + * @author javageek + */ +public class MagentoOauth10a extends MagentoDefaultApi10a { + private static String BASE_URL = null; + + @Override + public String getRequestTokenEndpoint() { + return BASE_URL + "oauth/initiate"; + } + + @Override + public String getAccessTokenEndpoint() { + return BASE_URL + "oauth/token"; + } + + @Override + public String getAuthorizationUrl(Token requestToken) { + return BASE_URL + "admin/oauth_authorize?oauth_token=" + + requestToken.getToken(); //this implementation is for admin roles only... + } + + @Override + public String setBaseURL(String baseURL) { + BASE_URL = baseURL; + if (BASE_URL == null) throw new OAuthException("The BASE_URL is still null. Please set it correctly"); + return BASE_URL; + } + +} diff --git a/src/test/java/org/scribe/examples/Magento10aExample.java b/src/test/java/org/scribe/examples/Magento10aExample.java new file mode 100644 index 000000000..0c2e4a806 --- /dev/null +++ b/src/test/java/org/scribe/examples/Magento10aExample.java @@ -0,0 +1,81 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.scribe.examples; + +import java.util.Scanner; +import org.scribe.builder.MagentoServiceBuilder; +import org.scribe.builder.api.MagentoOauth10a; +import org.scribe.model.OAuthRequest; +import org.scribe.model.Response; +import org.scribe.model.Token; +import org.scribe.model.Verb; +import org.scribe.model.Verifier; +import org.scribe.oauth.OAuthService; + +/** + * + * @author javageek + */ +public class Magento10aExample { + /** + * @param args + */ + public static void main(String[] args) { + final String MAGENTO_API_KEY = "43adm7jjkff1adm0gxs301oykmvyx8v7"; + final String MAGENTO_API_SECRET = "gxmkqpd0304csrn19tchcbw11pmz5ay4"; + final String MAGENTO_REST_API_URL = "http://ec2-23-23-25-84.compute-1.amazonaws.com/magento/api/rest/"; + + final String MAGENTO_BASE_URL_OAUTH = "http://ec2-23-23-25-84.compute-1.amazonaws.com/magento/index.php/"; + + // three-legged oauth + OAuthService service = new MagentoServiceBuilder() + .provider(MagentoOauth10a.class, MAGENTO_BASE_URL_OAUTH) + .apiKey(MAGENTO_API_KEY) + .apiSecret(MAGENTO_API_SECRET) + .debug() + .build(); + + + // start + Scanner in = new Scanner(System.in); + System.out.println("Magento's OAuth Workflow"); + System.out.println(); + // Obtain the Request Token + System.out.println("Fetching the Request Token..."); + Token requestToken = service.getRequestToken(); + System.out.println("Got the Request Token!"); + System.out.println(); + + // Obtain the Authorization URL + System.out.println("Fetching the Authorization URL..."); + String authorizationUrl = service.getAuthorizationUrl(requestToken); + System.out.println("Got the Authorization URL!"); + System.out.println("Now go and authorize Main here:"); + System.out.println(authorizationUrl); + System.out.println("And paste the authorization code here"); + System.out.print(">>"); + Verifier verifier = new Verifier(in.nextLine()); + System.out.println(); + + // Trade the Request Token and Verfier for the Access Token + System.out.println("Trading the Request Token for an Access Token..."); + Token accessToken = service.getAccessToken(requestToken, verifier); + System.out.println("Got the Access Token!"); + System.out.println("(if your curious it looks like this: " + + accessToken + " )"); + System.out.println(); + + // Now let's go and ask for a protected resource! + OAuthRequest request = new OAuthRequest(Verb.GET, MAGENTO_REST_API_URL+ "/products?limit=2&type=rest"); + service.signRequest(accessToken, request); + Response response = request.send(); + System.out.println(); + System.out.println(response.getCode()); + System.out.println(response.getBody()); + System.out.println(); + } + + +} From a104c2cff83fbfc22917ac57f3d62cfca89b3de3 Mon Sep 17 00:00:00 2001 From: Johanan Lancaon Date: Wed, 14 Nov 2012 14:19:26 -0800 Subject: [PATCH 2/3] OAuth stuff specific to magento 1.0a. Note: There is also a jar file. This is the scribe library. It too was updated to support Magento. --- src/test/java/org/scribe/examples/Magento10aExample.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/org/scribe/examples/Magento10aExample.java b/src/test/java/org/scribe/examples/Magento10aExample.java index 0c2e4a806..f74960c6f 100644 --- a/src/test/java/org/scribe/examples/Magento10aExample.java +++ b/src/test/java/org/scribe/examples/Magento10aExample.java @@ -1,6 +1,5 @@ /* - * To change this template, choose Tools | Templates - * and open the template in the editor. + Magento 1.0a Oauth specific example. */ package org.scribe.examples; From 2a9be1495747884482df3f41a2da8bdbed7b3acd Mon Sep 17 00:00:00 2001 From: Johanan Lancaon Date: Wed, 14 Nov 2012 14:20:23 -0800 Subject: [PATCH 3/3] Updated scribe for Magento 1.0a development. --- src/test/java/org/scribe/examples/Magento10aExample.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/scribe/examples/Magento10aExample.java b/src/test/java/org/scribe/examples/Magento10aExample.java index f74960c6f..0f12276b8 100644 --- a/src/test/java/org/scribe/examples/Magento10aExample.java +++ b/src/test/java/org/scribe/examples/Magento10aExample.java @@ -1,5 +1,6 @@ /* Magento 1.0a Oauth specific example. + * */ package org.scribe.examples;