<?php
if($_SERVER["REQUEST_METHOD"] == "GET"){
$sql_count = mysql_query("SELECT count(*) FROM products");
$total = mysql_fetch_array($sql_count);
$sql_result = mysql_query("SELECT * FROM products LIMIT 50 OFFSET ".(int)$_GET["offset"]);
$response = new stdClass();
$response->paging = new stdClass();
$response->products = array();
$response->paging->itemsTotal = $total;
$response->paging->pageSize = 50;
$response->paging->offset = $_GET["offset"];
while($row = mysql_fetch_array($sql_result)){
$product = new stdClass();
$product->sku = $row['id'];
$product->title = $row['title'];
$product->url = 'https://www.example.com/shop/'.$row['slug'];
$product->brand = 'My Brand';
$product->mpn = $row['mpn'];
$product->model = $row['model'];
$product->description = $row['description'];
$product->variations = array();
$variation = new stdClass();
$variation->availabilities = array();
$stock = new stdClass();
$stock->tag = 'default';
$stock->quantity = (int)$row['stock'];
$variation->availabilities[] = $stock;
$variation->prices = array();
$price = new stdClass();
$price->tag = 'default';
$price->currency = 'USD';
$price->number = (float)$row['price'];
$variation->prices[] = $price;
$variation->images = array();
$image = new stdClass();
$image->url = $row['imgurl'];
$variation->images[] = $image;
$variation->videos = array();
$video = new stdClass();
$video->url = $row['video'];
$variation->videos[] = $video;
$variation->barcode = $row['ean'];
$variation->size = $row['size'];
$variation->color = $row['color'];
$product->variations[] = $variation;
$response->products[] = $product;
}
echo json_encode($response);
}else if($_SERVER["REQUEST_METHOD"] == "POST"){
echo 'unimplemented method';
exit();
}else{
echo 'unsupported request method';
exit();
}
const express = require('express')
const app = express()
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if(!err){
app.get('/api/products', function (req, res) {
var offset = 0;
if(req.query.offset){
offset = req.query.offset;
}
db.collection("products").count({},function(err, count){
if(!err){
db.collection("products").find({}).limit(50).skip(offset).toArray(function(err, result) {
if (!err){
var response = {
paging:{
pageSize:50,
itemsTotal:count,
offset:0
},
products:[]
};
for(var i=0; i<result.length; i+=1){
var dbproduct = result[i];
var product = {};
product.sku = dbproduct._id;
product.title = dbproduct.title;
product.url = dbproduct.url;
product.brand = dbproduct.brand;
product.mpn = dbproduct.mpn;
product.model = dbproduct.model;
product.description = dbproduct.description;
product.variations = [{
availabilities:[{
tag:'default',
quantity:dbproduct.stock
}],
prices:[{
tag:'default',
currency:'USD',
number:dbproduct.price
}],
images:[{
url:dbproduct.imgurl
}],
videos:[{
url:dbproduct.youtubeurl
}],
barcode:dbproduct.upc,
size:dbproduct.size,
color:dbproduct.color
}];
response.products.push(product);
}
res.json(response);
}else{
res.json({'msg':'could not query collections'});
}
});
}else{
res.json({'msg':'could not count products'});
}
});
})
app.listen(3000, function () {
console.log('Click2Sync app listening on port 3000!')
})
}else{
console.log('Could not connect to database ):');
}
});
package com.example.c2s;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.apache.commons.io.IOUtils;
import java.util.*;
public class C2SAdapterServer {
String userName = "username";
String password = "password";
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB";
Connection conn;
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/api/products", new MyProductsHandler());
server.createContext("/api/orders", new MyOrdersHandler());
this.adapter = this;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.conn = DriverManager.getConnection(this.url, this.userName, this.password);
server.start();
}catch(SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
public Map<String, String> queryToMap(String query){
Map<String, String> result = new HashMap<String, String>();
for (String param : query.split("&")) {
String pair[] = param.split("=");
if (pair.length>1) {
result.put(pair[0], pair[1]);
}else{
result.put(pair[0], "");
}
}
return result;
}
static class MyProductsHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "";
if("get".equalsIgnoreCase(t.getRequestMethod())){
int count = 0;
Map<String, String> params = C2SAdapterServer.this.queryToMap(t.getRequestURI().getQuery());
int offset = Integer.parseInt(params.get("offset"));
Statement statement = C2SAdapterServer.this.conn.createStatement();
String queryCount = "select count(*) from products";
String queryString = "select * from products order by last_updated ASC LIMIT 50 OFFSET "+offset;
ResultSet rsCount = statement.executeQuery(queryCount);
while(rsCount.next()){
count = rsCount.getInt(1);
}
ResultSet rsProds = statement.executeQuery(queryString);
JSONObject jsonresponse = new JSONObject();
JSONObject paging = new JSONObject();
JSONObject products = new JSONArray();
paging.put("pageSize",50);
paging.put("itemsTotal",count);
paging.put("offset",offset);
jsonresponse.put("paging",paging);
while (rsProds.next()) {
JSONObject product = new JSONObject();
product.put("sku",rsProds.getString("sku"));
product.put("title",rsProds.getString("productname"));
product.put("url","https://www.example.com/"+rsProds.getString("slug"));
product.put("brand",rsProds.getString("brandname"));
product.put("mpn",rsProds.getString("mpn"));
product.put("model",rsProds.getString("model"));
product.put("description",rsProds.getString("description"));
JSONArray variations = new JSONArray();
JSONObject variation = new JSONObject();
JSONArray stocks = new JSONArray();
JSONObject stock = new JSONObject();
stock.put("tag","default");
stock.put("quantity",rsProds.getDouble("stock"));
stocks.put(stock);
variation.put("availabilities",stocks);
JSONArray prices = new JSONArray();
JSONObject price = new JSONObject();
price.put("tag","default");
price.put("number",rsProds.getDouble("price"));
price.put("currency","USD");
prices.put(price);
variation.put("prices",prices);
JSONArray images = new JSONArray();
JSONObject image = new JSONObject();
image.put("url","https://www.example.com/images/"+rsProds.getString("sku")+"-large.jpg");
images.put(image);
variation.put("images",images);
JSONArray videos = new JSONArray();
JSONObject video = new JSONObject();
video.put("url","https://www.youtube.com/watch?v="+rsProds.getString("videoId"));
videos.put(video);
variation.put("videos",videos);
variation.put("barcode",rsProds.getString("upc"));
variations.put(variation);
products.put(product);
}
jsonresponse.put("products",products);
response = jsonresponse.toString();
}else if("post".equalsIgnoreCase(t.getRequestMethod())){
response = "Unimplemented method";
}else{
response = "Unsupported method";
}
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class MyOrdersHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String response = "Unimplemented endpoint";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
Update your browser to view this website correctly. Update my browser now