Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- initial import of php-pecl clients - add shapeways-node 116455c33f2…

…bd4c3a34ef2723d0a81be778dc889 as nodejs
commit d4d751160ba133fd0cc8f5e3fbe93a077986edac 1 parent d4ee960
@d33psky d33psky authored
Showing with 1,226 additions and 0 deletions.
  1. BIN  models/cube-1cm3-centered_in_meter.stl
  2. BIN  models/test2_Textures_wrl.zip
  3. +2 −0  nodejs/.gitignore
  4. +64 −0 nodejs/README.md
  5. +114 −0 nodejs/app.coffee
  6. +145 −0 nodejs/app.js
  7. +16 −0 nodejs/cfg/config.coffee
  8. +23 −0 nodejs/cfg/config.js
  9. +31 −0 nodejs/lib/auth.coffee
  10. +57 −0 nodejs/lib/auth.js
  11. +48 −0 nodejs/lib/model.coffee
  12. +66 −0 nodejs/lib/model.js
  13. +29 −0 nodejs/package.json
  14. +29 −0 nodejs/tests/auth.coffee
  15. +38 −0 nodejs/tests/auth.js
  16. +44 −0 nodejs/tests/model.coffee
  17. +49 −0 nodejs/tests/model.js
  18. +17 −0 nodejs/views/layout.jade
  19. +9 −0 nodejs/views/model/id.jade
  20. +13 −0 nodejs/views/model/index.jade
  21. +12 −0 nodejs/views/model/upload.jade
  22. +12 −0 nodejs/views/model/upload_success.jade
  23. +34 −0 php-pecl/ApiList-oauth1-pecl.php
  24. +60 −0 php-pecl/Authorize-oauth1-pecl.php
  25. +39 −0 php-pecl/CartAddModel-oauth1-pecl.php
  26. +32 −0 php-pecl/CartList-oauth1-pecl.php
  27. +33 −0 php-pecl/MaterialInfo-oauth1-pecl.php
  28. +33 −0 php-pecl/ModelDownload-oauth1-pecl.php
  29. +33 −0 php-pecl/ModelGet-oauth1-pecl.php
  30. +40 −0 php-pecl/ModelUpload-oauth1-pecl.php
  31. +33 −0 php-pecl/README.md
  32. +5 −0 php-pecl/access_token.php
  33. +4 −0 php-pecl/api_url_base.php
  34. +5 −0 php-pecl/consumer_key.php
  35. +57 −0 php-pecl/error.php
View
BIN  models/cube-1cm3-centered_in_meter.stl
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  models/test2_Textures_wrl.zip
Binary file not shown
View
2  nodejs/.gitignore
@@ -0,0 +1,2 @@
+node_modules/
+*.log
View
64 nodejs/README.md
@@ -0,0 +1,64 @@
+shapeways-node
+==============
+
+Sample application for the new Shapeways API in NodeJS
+
+
+## Installation
+
+```
+1) Download NodeJS - http://nodejs.org/
+2) Download npm - http://nodejs.org/download/
+3) Run `npm install` from the base directory to install necessary modules
+```
+
+## Configuration
+
+This sample app allows you to set configuration variables in one of two ways. The variables made available can be see in `cfg/config.cofee` which will compile to `cfg/config.js`.
+
+**Environment Variables (preferred)** - Node can easily read variables from your local environment, which allows you to securely set configuration variables like Consumer Key, etc locally rather than in the code.
+
+In your *nix console:
+```
+Run export CALLBACK_URL = 'http://localhost:3000/callback'
+```
+
+**Configuration File** - The file `cfg/config.coffee` contains a number of variables that by default pull from your environment but also fall back to defaults specified in the file.
+
+_Note: If you modify the configuration file you will need to recompile the coffee-script files into javascript by running `coffee -c .` in the base directory._
+
+
+**Variables**
+```
+# Shapeways OAuth URLs
+exports.REQUEST_TOKEN_URL = process.env.REQUEST_TOKEN_URL || 'http://api.test102.ehv.shapeways.net/oauth1/request_token/v1'
+exports.ACCESS_TOKEN_URL = process.env.ACCESS_TOKEN_URL || 'http://api.test102.ehv.shapeways.net/oauth1/access_token/v1'
+
+# Tokens from 'My Apps' on Shapeways
+exports.CUSTOMER_KEY = process.env.CUSTOMER_KEY || '0c4d90e3d93a690d1287086d066eb64c4c15914f'
+exports.CUSTOMER_SECRET = process.env.CUSTOMER_SECRET || '647939e8935d986413a83c6e428fd64fb7f668d5'
+
+# Your App's Callback URL
+exports.CALLBACK_URL = process.env.CALLBACK_URL || 'http://localhost:3000/callback'
+```
+
+
+## Usage
+
+```
+1) Run `node app.js`
+2) Load up http://localhost:3000/ in your browser to access the app (Note: must be connected to SW VPN)
+```
+
+
+
+## TODO
+* ~~Auth a user via OAuth~~
+* ~~Move hardcoded consumer keys into app's session variables~~
+* ~~Move auth logic into its own function~~
+* ~~View a list of the user's modules~~
+* ~~Upload a model~~
+* Edit a model's information
+* View a list of Materials
+* Add links to documentation
+* Create npm module
View
114 nodejs/app.coffee
@@ -0,0 +1,114 @@
+express = require 'express'
+Auth = (require './lib/auth.js').Auth
+Model = (require './lib/model.js').Model
+cfg = require './cfg/config.js'
+
+app = express()
+app.use express.bodyParser()
+app.use express.cookieParser()
+app.use express.session
+ secret: 'blahblahblah' # Random hash for session store
+app.set 'views', __dirname + '/views'
+app.engine 'jade', require('jade').__express
+
+
+### Controllers ###
+
+auth = new Auth
+model = new Model
+
+
+### Routes ###
+
+app.get '/', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # User is already auth'd in and should be taken to their models
+ res.redirect '/model'
+
+
+app.get '/login', (req, res) ->
+ # Allow user to login using Shapeways and collect request token
+ auth.login (callback) ->
+ # Store oauth_token + secret in session
+ req.session.oauth_token = callback.oauth_token
+ req.session.oauth_token_secret = callback.oauth_token_secret
+ res.redirect callback.url
+
+app.get '/callback', (req, res) ->
+ # Grab the OAuth access token and store it in session
+ auth.handleCallback req.query.oauth_token, req.session.oauth_token_secret, req.query.oauth_verifier, (callback) ->
+ # Store oauth_access_token + secret in session
+ req.session.oauth_access_token = callback.oauth_access_token
+ req.session.oauth_access_token_secret = callback.oauth_access_token_secret
+ res.redirect '/' # Send the access token to the browser
+
+app.get '/upload', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Display file upload dialog
+ res.render 'model/upload.jade'
+
+app.post '/model/upload', (req, res) ->
+ # Process model upload
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Upload a model
+ model.putModel req.files.modelUpload, req.session.oauth_access_token, req.session.oauth_access_token_secret, (callback) ->
+ res.render 'model/upload_success.jade', { "callback": JSON.parse callback }
+
+app.get '/model/:id', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Display a list of user's models
+
+ model.getModel req.params.id, req.session.oauth_access_token, req.session.oauth_access_token_secret, (callback) ->
+ if isJson req.url
+ res.send JSON.parse callback
+ else
+ res.render 'model/id.jade', { "callback": JSON.parse callback }
+
+app.get '/model*', (req, res) ->
+ if !isLoggedIn(req.session)
+ res.redirect '/login'
+ else
+ # Display a list of user's models
+
+ model.getModels req.session.oauth_access_token, req.session.oauth_access_token_secret, (callback) ->
+ if isJson req.url
+ res.send JSON.parse callback
+ else
+ res.render 'model/index.jade', { "callback": JSON.parse callback }
+
+
+
+
+app.get '/logout', (req, res) ->
+ # Allow the user to logout (clear local cookies)
+ console.log '--- LOGOUT ---'
+ console.log req.session
+ console.log '--- LOGOUT ---'
+ req.session.destroy()
+ res.redirect '/'
+
+### Start the App ###
+
+app.listen '3000'
+
+
+isLoggedIn = (session) ->
+ if !session.oauth_access_token
+ # User doesn't have any session data and therefore is not already logged in
+ return false
+ return true
+
+isJson = (url) ->
+ json = false
+ if (url.substring url.length-5) == ".json"
+ json = true
+ return json
+
View
145 nodejs/app.js
@@ -0,0 +1,145 @@
+// Generated by CoffeeScript 1.4.0
+(function() {
+ var Auth, Model, app, auth, cfg, express, isJson, isLoggedIn, model;
+
+ express = require('express');
+
+ Auth = (require('./lib/auth.js')).Auth;
+
+ Model = (require('./lib/model.js')).Model;
+
+ cfg = require('./cfg/config.js');
+
+ app = express();
+
+ app.use(express.bodyParser());
+
+ app.use(express.cookieParser());
+
+ app.use(express.session({
+ secret: 'blahblahblah'
+ }));
+
+ app.set('views', __dirname + '/views');
+
+ app.engine('jade', require('jade').__express);
+
+ /* Controllers
+ */
+
+
+ auth = new Auth;
+
+ model = new Model;
+
+ /* Routes
+ */
+
+
+ app.get('/', function(req, res) {
+ if (!isLoggedIn(req.session)) {
+ return res.redirect('/login');
+ } else {
+ return res.redirect('/model');
+ }
+ });
+
+ app.get('/login', function(req, res) {
+ return auth.login(function(callback) {
+ req.session.oauth_token = callback.oauth_token;
+ req.session.oauth_token_secret = callback.oauth_token_secret;
+ return res.redirect(callback.url);
+ });
+ });
+
+ app.get('/callback', function(req, res) {
+ return auth.handleCallback(req.query.oauth_token, req.session.oauth_token_secret, req.query.oauth_verifier, function(callback) {
+ req.session.oauth_access_token = callback.oauth_access_token;
+ req.session.oauth_access_token_secret = callback.oauth_access_token_secret;
+ return res.redirect('/');
+ });
+ });
+
+ app.get('/upload', function(req, res) {
+ if (!isLoggedIn(req.session)) {
+ return res.redirect('/login');
+ } else {
+ return res.render('model/upload.jade');
+ }
+ });
+
+ app.post('/model/upload', function(req, res) {
+ if (!isLoggedIn(req.session)) {
+ return res.redirect('/login');
+ } else {
+ return model.putModel(req.files.modelUpload, req.session.oauth_access_token, req.session.oauth_access_token_secret, function(callback) {
+ return res.render('model/upload_success.jade', {
+ "callback": JSON.parse(callback)
+ });
+ });
+ }
+ });
+
+ app.get('/model/:id', function(req, res) {
+ if (!isLoggedIn(req.session)) {
+ return res.redirect('/login');
+ } else {
+ return model.getModel(req.params.id, req.session.oauth_access_token, req.session.oauth_access_token_secret, function(callback) {
+ if (isJson(req.url)) {
+ return res.send(JSON.parse(callback));
+ } else {
+ return res.render('model/id.jade', {
+ "callback": JSON.parse(callback)
+ });
+ }
+ });
+ }
+ });
+
+ app.get('/model*', function(req, res) {
+ if (!isLoggedIn(req.session)) {
+ return res.redirect('/login');
+ } else {
+ return model.getModels(req.session.oauth_access_token, req.session.oauth_access_token_secret, function(callback) {
+ if (isJson(req.url)) {
+ return res.send(JSON.parse(callback));
+ } else {
+ return res.render('model/index.jade', {
+ "callback": JSON.parse(callback)
+ });
+ }
+ });
+ }
+ });
+
+ app.get('/logout', function(req, res) {
+ console.log('--- LOGOUT ---');
+ console.log(req.session);
+ console.log('--- LOGOUT ---');
+ req.session.destroy();
+ return res.redirect('/');
+ });
+
+ /* Start the App
+ */
+
+
+ app.listen('3000');
+
+ isLoggedIn = function(session) {
+ if (!session.oauth_access_token) {
+ return false;
+ }
+ return true;
+ };
+
+ isJson = function(url) {
+ var json;
+ json = false;
+ if ((url.substring(url.length - 5)) === ".json") {
+ json = true;
+ }
+ return json;
+ };
+
+}).call(this);
View
16 nodejs/cfg/config.coffee
@@ -0,0 +1,16 @@
+### Configuration file - Set your Shapeways/App specific variables here ###
+
+# Shapways API Version and server
+exports.API_VERSION = process.env.API_VERSION || 'v1'
+exports.API_SERVER = process.env.API_SERVER || 'api.shapeways.com'
+
+# Shapeways OAuth URLs
+exports.REQUEST_TOKEN_URL = process.env.REQUEST_TOKEN_URL || "http://#{@API_SERVER}/oauth1/request_token/#{@API_VERSION}"
+exports.ACCESS_TOKEN_URL = process.env.ACCESS_TOKEN_URL || "http://#{@API_SERVER}/oauth1/access_token/#{@API_VERSION}"
+
+# Tokens from 'My Apps' on Shapeways
+exports.CUSTOMER_KEY = process.env.CUSTOMER_KEY || '2c74e97709ac200da02222036df4ec3c5997ba51'
+exports.CUSTOMER_SECRET = process.env.CUSTOMER_SECRET || 'dc9bce571e5953db857356240b5e9778c641bd3e'
+
+# Your App's Callback URL
+exports.CALLBACK_URL = process.env.CALLBACK_URL || 'http://localhost:3000/callback'
View
23 nodejs/cfg/config.js
@@ -0,0 +1,23 @@
+// Generated by CoffeeScript 1.4.0
+
+/* Configuration file - Set your Shapeways/App specific variables here
+*/
+
+
+(function() {
+
+ exports.API_VERSION = process.env.API_VERSION || 'v1';
+
+ exports.API_SERVER = process.env.API_SERVER || 'api.shapeways.com';
+
+ exports.REQUEST_TOKEN_URL = process.env.REQUEST_TOKEN_URL || ("http://" + this.API_SERVER + "/oauth1/request_token/" + this.API_VERSION);
+
+ exports.ACCESS_TOKEN_URL = process.env.ACCESS_TOKEN_URL || ("http://" + this.API_SERVER + "/oauth1/access_token/" + this.API_VERSION);
+
+ exports.CUSTOMER_KEY = process.env.CUSTOMER_KEY || '2c74e97709ac200da02222036df4ec3c5997ba51';
+
+ exports.CUSTOMER_SECRET = process.env.CUSTOMER_SECRET || 'dc9bce571e5953db857356240b5e9778c641bd3e';
+
+ exports.CALLBACK_URL = process.env.CALLBACK_URL || 'http://localhost:3000/callback';
+
+}).call(this);
View
31 nodejs/lib/auth.coffee
@@ -0,0 +1,31 @@
+### Handle authentication of users via Shapeways ###
+cfg = require '../cfg/config.js'
+OAuth = (require 'oauth').OAuth
+
+exports.Auth = class Auth
+ constructor: ->
+ # Generate oauth object
+ @oa = oa = new OAuth cfg.REQUEST_TOKEN_URL, cfg.ACCESS_TOKEN_URL, cfg.CUSTOMER_KEY, cfg.CUSTOMER_SECRET, '1.0', cfg.CALLBACK_URL, 'HMAC-SHA1'
+
+ login: (callback) ->
+ console.log 'Getting OAuth Request Token'
+ @oa.getOAuthRequestToken (error, oauth_token, oauth_token_secret, results) ->
+ if error
+ console.log 'error :' + JSON.stringify error
+
+ console.log results
+ url = results.authentication_url
+
+ callback { oauth_token, oauth_token_secret, url }
+
+ handleCallback: (oauth_token, oauth_token_secret, oauth_verifier, callback) ->
+ # Grab Access Token
+ @oa.getOAuthAccessToken oauth_token, oauth_token_secret, oauth_verifier, (error, oauth_access_token, oauth_access_token_secret, response) ->
+ if error
+ console.log 'error :' + JSON.stringify error
+ if response is undefined
+ console.log 'error: ' + response
+
+ callback { oauth_access_token, oauth_access_token_secret }
+
+
View
57 nodejs/lib/auth.js
@@ -0,0 +1,57 @@
+// Generated by CoffeeScript 1.4.0
+
+/* Handle authentication of users via Shapeways
+*/
+
+
+(function() {
+ var Auth, OAuth, cfg;
+
+ cfg = require('../cfg/config.js');
+
+ OAuth = (require('oauth')).OAuth;
+
+ exports.Auth = Auth = (function() {
+
+ function Auth() {
+ var oa;
+ this.oa = oa = new OAuth(cfg.REQUEST_TOKEN_URL, cfg.ACCESS_TOKEN_URL, cfg.CUSTOMER_KEY, cfg.CUSTOMER_SECRET, '1.0', cfg.CALLBACK_URL, 'HMAC-SHA1');
+ }
+
+ Auth.prototype.login = function(callback) {
+ console.log('Getting OAuth Request Token');
+ return this.oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) {
+ var url;
+ if (error) {
+ console.log('error :' + JSON.stringify(error));
+ }
+ console.log(results);
+ url = results.authentication_url;
+ return callback({
+ oauth_token: oauth_token,
+ oauth_token_secret: oauth_token_secret,
+ url: url
+ });
+ });
+ };
+
+ Auth.prototype.handleCallback = function(oauth_token, oauth_token_secret, oauth_verifier, callback) {
+ return this.oa.getOAuthAccessToken(oauth_token, oauth_token_secret, oauth_verifier, function(error, oauth_access_token, oauth_access_token_secret, response) {
+ if (error) {
+ console.log('error :' + JSON.stringify(error));
+ }
+ if (response === void 0) {
+ console.log('error: ' + response);
+ }
+ return callback({
+ oauth_access_token: oauth_access_token,
+ oauth_access_token_secret: oauth_access_token_secret
+ });
+ });
+ };
+
+ return Auth;
+
+ })();
+
+}).call(this);
View
48 nodejs/lib/model.coffee
@@ -0,0 +1,48 @@
+### Object representing a model file on Shapeways ###
+cfg = require '../cfg/config.js'
+Auth = (require './auth.js').Auth
+fs = require 'fs'
+
+auth = new Auth
+
+exports.Model = class Model
+ getModels: (oauth_access_token, oauth_access_token_secret, callback) ->
+ # Note: getModels, getModel should utilize same function with overloaded optional parameter :modelId
+
+ auth.oa.getProtectedResource "http://#{cfg.API_SERVER}/model/#{cfg.API_VERSION}", 'GET', oauth_access_token, oauth_access_token_secret, (error, data, response) ->
+ if error
+ console.log 'error :' + JSON.stringify error
+
+ # Send model list
+ callback data
+
+ getModel: (id, oauth_access_token, oauth_access_token_secret, callback) ->
+ # Note: getModel should utilize same function with overloaded optional parameter :modelId
+ auth.oa.getProtectedResource "http://#{cfg.API_SERVER}/model/#{id}/#{cfg.API_VERSION}", 'GET', oauth_access_token, oauth_access_token_secret, (error, data, response) ->
+ if error
+ console.log 'error :' + JSON.stringify error
+
+ # Send model information
+ callback data
+
+ putModel: (file, oauth_access_token, oauth_access_token_secret, callback) ->
+ # Tests an upload - work in progress
+
+ model_upload = fs.readFile file.path, (err, fileData) -> ##
+ fileData = encodeURIComponent fileData.toString('base64')
+
+ upload = JSON.stringify {
+ file: fileData,
+ fileName: file.name,
+ ownOrAuthorizedModel: 1,
+ acceptTermsAndConditions: 1
+ }
+
+ auth.oa.post "http://#{cfg.API_SERVER}/model/#{cfg.API_VERSION}", oauth_access_token, oauth_access_token_secret, upload, (error, data, response) ->
+ if error
+ console.log 'ERROR:'
+ console.log error
+ # Redirect to error page?
+ else
+ # Send model information
+ callback data
View
66 nodejs/lib/model.js
@@ -0,0 +1,66 @@
+// Generated by CoffeeScript 1.4.0
+
+/* Object representing a model file on Shapeways
+*/
+
+
+(function() {
+ var Auth, Model, auth, cfg, fs;
+
+ cfg = require('../cfg/config.js');
+
+ Auth = (require('./auth.js')).Auth;
+
+ fs = require('fs');
+
+ auth = new Auth;
+
+ exports.Model = Model = (function() {
+
+ function Model() {}
+
+ Model.prototype.getModels = function(oauth_access_token, oauth_access_token_secret, callback) {
+ return auth.oa.getProtectedResource("http://" + cfg.API_SERVER + "/model/" + cfg.API_VERSION, 'GET', oauth_access_token, oauth_access_token_secret, function(error, data, response) {
+ if (error) {
+ console.log('error :' + JSON.stringify(error));
+ }
+ return callback(data);
+ });
+ };
+
+ Model.prototype.getModel = function(id, oauth_access_token, oauth_access_token_secret, callback) {
+ return auth.oa.getProtectedResource("http://" + cfg.API_SERVER + "/model/" + id + "/" + cfg.API_VERSION, 'GET', oauth_access_token, oauth_access_token_secret, function(error, data, response) {
+ if (error) {
+ console.log('error :' + JSON.stringify(error));
+ }
+ return callback(data);
+ });
+ };
+
+ Model.prototype.putModel = function(file, oauth_access_token, oauth_access_token_secret, callback) {
+ var model_upload;
+ return model_upload = fs.readFile(file.path, function(err, fileData) {
+ var upload;
+ fileData = encodeURIComponent(fileData.toString('base64'));
+ upload = JSON.stringify({
+ file: fileData,
+ fileName: file.name,
+ ownOrAuthorizedModel: 1,
+ acceptTermsAndConditions: 1
+ });
+ return auth.oa.post("http://" + cfg.API_SERVER + "/model/" + cfg.API_VERSION, oauth_access_token, oauth_access_token_secret, upload, function(error, data, response) {
+ if (error) {
+ console.log('ERROR:');
+ return console.log(error);
+ } else {
+ return callback(data);
+ }
+ });
+ });
+ };
+
+ return Model;
+
+ })();
+
+}).call(this);
View
29 nodejs/package.json
@@ -0,0 +1,29 @@
+{
+ "name": "shapeways"
+ , "version": "0.0.1"
+ , "description": "API Wrapper for the Shapeways API in NodeJS"
+ , "homepage": "http://www.shapeways.com"
+ , "keywords": ["shapeways", "3d", "printing"]
+ , "author": "Brad Dickason <brad@shapeways.com>"
+ , "contributors": [
+ { "name": "Brad Dickason", "email": "brad@shapeways.com" }
+ ]
+ , "repository":{
+ "type": "git"
+ , "url": "https://github.com/Shapeways/shapeways-node.git"
+ }
+ , "dependencies": {
+ "express": "3.0.1",
+ "coffee-script": "1.4.0",
+ "oauth": "0.9.8",
+ "jade": "0.27.7"
+ }
+ , "devDependencies": {
+ "mocha": "1.6.0"
+ }
+ , "main": "index"
+ , "engines": { "node": ">= 0.4.0" }
+ , "scripts": {
+ "test": "make test"
+ }
+}
View
29 nodejs/tests/auth.coffee
@@ -0,0 +1,29 @@
+### Tests for Models /model ###
+
+cfg = require '../cfg/config.js'
+
+describe 'app credentials', ->
+ it 'should have a valid Request Token', ->
+ it 'should have a valid Access Token', ->
+
+
+### Usage
+describe('test', function(){
+ it('should work with objects', function(){
+ var a = { name: 'tobi', age: 2, species: 'ferret' };
+ var b = { name: 'jane', age: 8, species: 'ferret' };
+ a.should.eql(b);
+ })
+
+ it('should work with arrays', function(){
+ var a = [1,2,{ name: 'tobi' },4,5]
+ var b = [1,2,{ name: 'jane' },4,4, 'extra stuff', 'more extra']
+ a.should.eql(b);
+ })
+
+ it('should work with strings', function(){
+ 'some\nfoo\nbar'.should.equal('some\nbar\nbaz');
+ })
+})
+
+###
View
38 nodejs/tests/auth.js
@@ -0,0 +1,38 @@
+// Generated by CoffeeScript 1.4.0
+
+/* Tests for Models /model
+*/
+
+
+(function() {
+ var cfg;
+
+ cfg = require('../cfg/config.js');
+
+ describe('app credentials', function() {
+ it('should have a valid Request Token', function() {});
+ return it('should have a valid Access Token', function() {});
+ });
+
+ /* Usage
+ describe('test', function(){
+ it('should work with objects', function(){
+ var a = { name: 'tobi', age: 2, species: 'ferret' };
+ var b = { name: 'jane', age: 8, species: 'ferret' };
+ a.should.eql(b);
+ })
+
+ it('should work with arrays', function(){
+ var a = [1,2,{ name: 'tobi' },4,5]
+ var b = [1,2,{ name: 'jane' },4,4, 'extra stuff', 'more extra']
+ a.should.eql(b);
+ })
+
+ it('should work with strings', function(){
+ 'some\nfoo\nbar'.should.equal('some\nbar\nbaz');
+ })
+ })
+ */
+
+
+}).call(this);
View
44 nodejs/tests/model.coffee
@@ -0,0 +1,44 @@
+### Tests for Models /model ###
+
+describe 'upload a model', ->
+ it 'should accept a valid .stl file'
+
+ it 'should display an error when an invalid model is specified'
+
+ it 'should receive a successful response from the server when a valid model is uplaoded '
+
+describe 'edit a model', ->
+
+describe 'view a list of models', ->
+ it 'should return a list of models when no parameter is specified'
+
+ it 'should list private models'
+
+ it 'should list public models'
+
+describe 'view model by id', ->
+ it 'should return a single model'
+
+ it 'should return null when an invalid model is specified'
+
+
+### Usage
+describe('test', function(){
+ it('should work with objects', function(){
+ var a = { name: 'tobi', age: 2, species: 'ferret' };
+ var b = { name: 'jane', age: 8, species: 'ferret' };
+ a.should.eql(b);
+ })
+
+ it('should work with arrays', function(){
+ var a = [1,2,{ name: 'tobi' },4,5]
+ var b = [1,2,{ name: 'jane' },4,4, 'extra stuff', 'more extra']
+ a.should.eql(b);
+ })
+
+ it('should work with strings', function(){
+ 'some\nfoo\nbar'.should.equal('some\nbar\nbaz');
+ })
+})
+
+###
View
49 nodejs/tests/model.js
@@ -0,0 +1,49 @@
+// Generated by CoffeeScript 1.4.0
+
+/* Tests for Models /model
+*/
+
+
+(function() {
+
+ describe('upload a model', function() {
+ it('should accept a valid .stl file');
+ it('should display an error when an invalid model is specified');
+ return it('should receive a successful response from the server when a valid model is uplaoded ');
+ });
+
+ describe('edit a model', function() {});
+
+ describe('view a list of models', function() {
+ it('should return a list of models when no parameter is specified');
+ it('should list private models');
+ return it('should list public models');
+ });
+
+ describe('view model by id', function() {
+ it('should return a single model');
+ return it('should return null when an invalid model is specified');
+ });
+
+ /* Usage
+ describe('test', function(){
+ it('should work with objects', function(){
+ var a = { name: 'tobi', age: 2, species: 'ferret' };
+ var b = { name: 'jane', age: 8, species: 'ferret' };
+ a.should.eql(b);
+ })
+
+ it('should work with arrays', function(){
+ var a = [1,2,{ name: 'tobi' },4,5]
+ var b = [1,2,{ name: 'jane' },4,4, 'extra stuff', 'more extra']
+ a.should.eql(b);
+ })
+
+ it('should work with strings', function(){
+ 'some\nfoo\nbar'.should.equal('some\nbar\nbaz');
+ })
+ })
+ */
+
+
+}).call(this);
View
17 nodejs/views/layout.jade
@@ -0,0 +1,17 @@
+// layout.jade - Main index layout
+
+// my-layout.jade
+doctype 5
+html
+ head
+ title Shapeways Sample App
+ block head
+ body
+ .nav
+ a(href='/') Home
+ | |
+ a(href='/upload') Upload
+ | |
+ a(href='/logout') Logout
+ .content
+ block content
View
9 nodejs/views/model/id.jade
@@ -0,0 +1,9 @@
+// id.jade - Display individual model details
+extends ../layout
+
+block content
+ h1= callback.modelId
+
+ a(href='http://www.shapeways.com/model/' + callback.modelId, target='_blank') View on Shapeways
+
+
View
13 nodejs/views/model/index.jade
@@ -0,0 +1,13 @@
+// index.jade - Handle base directory views
+extends ../layout
+
+block content
+ h1 Your Models
+
+ strong Total count:
+ !{callback.models.length}
+ each model in callback.models
+ li
+ a(href='/model/' + model.modelId) !{model.modelTitle}
+
+
View
12 nodejs/views/model/upload.jade
@@ -0,0 +1,12 @@
+// upload.jade - Allow a user to select a file and upload it to Shapeways
+extends ../layout
+
+block content
+ h1 Upload a file
+
+ form(action="/model/upload", method="post", enctype="multipart/form-data")
+ .browse
+ input(type="file", name="modelUpload")
+
+ .submit
+ input(type="submit", name="submit")
View
12 nodejs/views/model/upload_success.jade
@@ -0,0 +1,12 @@
+// upload.jade - Allow a user to select a file and upload it to Shapeways
+extends ../layout
+
+block content
+ h1 Upload Successful!
+
+ h3= callback.modelId
+
+ a(href='http://www.shapeways.com/model/' + callback.modelId, target='_blank') View on Shapeways
+
+ .back
+ a(href='/') Back to your models
View
34 php-pecl/ApiList-oauth1-pecl.php
@@ -0,0 +1,34 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+$verbose_debug = false;
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $oauth->fetch($api_url_base ."/api/v1", null, OAUTH_HTTP_METHOD_GET);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
60 php-pecl/Authorize-oauth1-pecl.php
@@ -0,0 +1,60 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "api_url_base.php";
+require "error.php";
+
+$verbose_debug = true;
+
+try {
+ $oauth_client = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth_client->enableDebug();
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $info = $oauth_client->getRequestToken("$api_url_base/oauth1/request_token/v1", "oob");
+ # work around our Pecl getRequestToken->array bug https://bugs.php.net/bug.php?id=63572 :
+ if ( array_key_exists('oauth_token_secret', $info) &&
+ array_key_exists('authentication_url', $info) &&
+ ! array_key_exists('oauth_token', $info)) {
+ $urlArray = parse_url($info['authentication_url']);
+ $info['authentication_url'] = $urlArray['scheme'] .'://'. $urlArray['host'] . $urlArray['path'];
+ parse_str($urlArray['query']);
+ $info['oauth_token'] = $oauth_token;
+ }
+ if ( array_key_exists('oauth_token', $info) &&
+ array_key_exists('oauth_token_secret', $info) &&
+ array_key_exists('authentication_url', $info) ) {
+ echo "Request token : ".$info['oauth_token']."\n";
+ echo "Request token secret : ".$info['oauth_token_secret']."\n";
+ echo "Next please authenticate yourself at ".$info['authentication_url']."?oauth_token=".$info['oauth_token']." and collect the PIN for the next step.\n";
+ $oauth_client->setToken( $info['oauth_token'] , $info['oauth_token_secret'] );
+ } else {
+ Error("getRequestToken", null, $info, $oauth_client->getLastResponseInfo(), null);
+ }
+} catch(OAuthException $E){
+ Error("getRequestToken", $E->getMessage(), null, $oauth_client->getLastResponseInfo(), $E->debugInfo);
+}
+
+$pin = readline("Pin: ");
+
+try {
+ $info = $oauth_client->getAccessToken("$api_url_base/oauth1/access_token/v1", null, $pin);
+ if ( array_key_exists('oauth_token', $info) &&
+ array_key_exists('oauth_token_secret', $info) ) {
+ echo "Access token : ".$info['oauth_token']."\n";
+ echo "Access token secret : ".$info['oauth_token_secret']."\n";
+ echo "\nYou can store these access token values in access_token.php for the other scripts to use.\n";
+ $oauth_client->setToken( $info['oauth_token'] , $info['oauth_token_secret'] );
+ } else {
+ Error("getAccessToken", null, $info, $oauth_client->getLastResponseInfo(), null);
+ }
+} catch(OAuthException $E){
+ Error("getAccessToken exception", $E->getMessage(), null, $oauth_client->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
39 php-pecl/CartAddModel-oauth1-pecl.php
@@ -0,0 +1,39 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $modelId = 1234567; # CHANGEME
+ $data = array("modelId" => "$modelId",
+ "materialId" => 6,
+ "quantity" => 1,
+ "scale" => 1,
+ );
+ $data_string = json_encode($data);
+ $oauth->fetch($api_url_base ."/cart/v1", $data_string, OAUTH_HTTP_METHOD_POST);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
32 php-pecl/CartList-oauth1-pecl.php
@@ -0,0 +1,32 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $oauth->fetch($api_url_base ."/cart/v1", null, OAUTH_HTTP_METHOD_GET);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
33 php-pecl/MaterialInfo-oauth1-pecl.php
@@ -0,0 +1,33 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $materialId = 26; # CHANGEME
+ $oauth->fetch($api_url_base ."/material/$materialId/v1", null, OAUTH_HTTP_METHOD_GET);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
33 php-pecl/ModelDownload-oauth1-pecl.php
@@ -0,0 +1,33 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $modelId = 1234567; # CHANGEME
+ $oauth->fetch($api_url_base ."/model/$modelId/v1", null, OAUTH_HTTP_METHOD_GET);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
33 php-pecl/ModelGet-oauth1-pecl.php
@@ -0,0 +1,33 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $modelId = 1234567; # CHANGEME
+ $oauth->fetch($api_url_base ."/model/$modelId/v1", null, OAUTH_HTTP_METHOD_GET);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
40 php-pecl/ModelUpload-oauth1-pecl.php
@@ -0,0 +1,40 @@
+#!/usr/bin/php
+<?php
+
+require "consumer_key.php";
+require "access_token.php";
+require "api_url_base.php";
+require "error.php";
+
+try {
+ $oauth = new Oauth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
+ $oauth->enableDebug();
+ $oauth->setToken($access_token, $access_secret);
+} catch(OAuthException $E) {
+ Error("setup exception", $E->getMessage(), null, null, $E->debugInfo);
+}
+
+try {
+ $filename = "cube-1cm3-centered_in_meter.stl";
+ $file = file_get_contents("../models/". $filename);
+ $data = array("fileName" => "$filename",
+ "file" => base64_encode($file),
+ "ownOrAuthorizedModel" => 1,
+ "acceptTermsAndConditions" => 1,
+ );
+ $data_string = json_encode($data);
+ $oauth->fetch($api_url_base ."/model/v1", $data_string, OAUTH_HTTP_METHOD_POST);
+ $response = $oauth->getLastResponse();
+ $json = json_decode($response);
+ if (null == $json) {
+ PrintJsonLastError();
+ var_dump($response);
+ } else {
+ print_r($json);
+ }
+} catch(OAuthException $E) {
+ Error("fetch exception", $E->getMessage(), null, $oauth->getLastResponseInfo(), $E->debugInfo);
+}
+
+?>
+
View
33 php-pecl/README.md
@@ -0,0 +1,33 @@
+Shapeways REST API sample PHP/PECL applications
+===============================================
+
+Sample applications for the Shapeways REST API in PHP/PECL
+
+## Installation
+
+1. install php
+2. install php-pear
+3. pecl install oauth
+
+## Configuration
+
+1. Get an oauth1 `Consumer Key` at <http://www.shapeways.com/myapps> and store the `Consumer Key` in `consumer_key.php`
+2. Run `Authorize-oauth1-pecl.php` to :
+ - get temporary credentials (aka a `Request Token`)
+ - authorize the `Consumer Key` with the `Request Token` via the presented URL to get the `Verifier` code
+ - enter the `Verifier code` to request an `Access Token`
+ - store the `Access Token` in `access_token.php`.
+
+## Sample scripts
+
+- ApiList-oauth1-pecl.php Shows your rate limiting data in JSON format.
+- CartAddModel-oauth1-pecl.php Add a model to your shopping cart.
+- CartList-oauth1-pecl.php Show your cart contents.
+- MaterialInfo-oauth1-pecl.php Show material properties.
+- ModelDownload-oauth1-pecl.php Download a model.
+- ModelGet-oauth1-pecl.php Show model properties.
+- ModelUpload-oauth1-pecl.php Upload a model.
+
+## TODO
+
+-
View
5 php-pecl/access_token.php
@@ -0,0 +1,5 @@
+<?php
+
+$access_token = ""; # CHANGEME
+$access_secret = ""; # CHANGEME
+
View
4 php-pecl/api_url_base.php
@@ -0,0 +1,4 @@
+<?php
+
+$api_url_base = "http://api.shapeways.com";
+
View
5 php-pecl/consumer_key.php
@@ -0,0 +1,5 @@
+<?php
+
+$consumer_key = ""; # CHANGEME
+$consumer_secret = ""; # CHANGEME
+
View
57 php-pecl/error.php
@@ -0,0 +1,57 @@
+<?php
+
+$verbose_debug = false;
+
+function Error($component, $exception, $info, $LastResponseInfo, $debugInfo) {
+ global $verbose_debug;
+ echo "A fatal error occurred during $component\n";
+ if ($exception) {
+ echo "Exception : $exception\n";
+ }
+ if ($info) {
+ if (array_key_exists('oauth_problem', $info)) {
+ echo "oauth_problem : [". $info['oauth_problem'] . "]\n";
+ }
+ if ($verbose_debug) {
+ echo "Query response body :\n";
+ var_dump($info);
+ }
+ }
+ if ($verbose_debug && $LastResponseInfo) {
+ echo "Query response headers :\n";
+ var_dump($LastResponseInfo);
+ }
+ if ($verbose_debug && $debugInfo) {
+ echo "Oauth debugInfo :\n";
+ var_dump($debugInfo);
+ }
+ exit(1);
+}
+
+function PrintJsonLastError() {
+ switch (json_last_error()) {
+ case JSON_ERROR_NONE:
+ echo ' - No errors';
+ break;
+ case JSON_ERROR_DEPTH:
+ echo ' - Maximum stack depth exceeded';
+ break;
+ case JSON_ERROR_STATE_MISMATCH:
+ echo ' - Underflow or the modes mismatch';
+ break;
+ case JSON_ERROR_CTRL_CHAR:
+ echo ' - Unexpected control character found';
+ break;
+ case JSON_ERROR_SYNTAX:
+ echo ' - Syntax error, malformed JSON';
+ break;
+ case JSON_ERROR_UTF8:
+ echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
+ break;
+ default:
+ echo ' - Unknown error';
+ break;
+ }
+ echo PHP_EOL;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.