<file_sep>from flask import Flask, request
from newsapi import NewsApiClient
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
import logging, xmlrpc.client, requests, json, pandas as pd,pika

from flask import request
app = Flask(__name__)

#additional setup
def setup_logger(name, log_file, level=logging.INFO):
    """To setup as many loggers as you want"""

    handler = logging.FileHandler(log_file)  
    handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger

#log file setup
userLog = 'user.log'
cmdLog = 'calls.log'

formatter = logging.Formatter('%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')

logging.basicConfig(filename=cmdLog,level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')

users = setup_logger('user', userLog)

@app.route('/')
def first():

    return 'Hello, World!'

@app.route('/insertStudent')
def insertstudent():
    studentid = request.args.get('studentid')
    studentname = request.args.get('studentname')
    studentdob = request.args.get('studentdob')

    users.info('Insert student' + studentid + '-' + studentname + '-' + studentdob)
    return 'Insert student' + studentid + '-' + studentname + '-' + studentdob

@app.route('/justweather')
def weather():
    api_key = "<KEY>"
  
    # base_url variable to store url 
    base_url = "http://api.openweathermap.org/data/2.5/forecast?"
    
    # Give city name 
    city_name = request.args.get('city')
    
    # complete_url variable to store 
    # complete url address 
    complete_url = base_url + "appid=" + api_key + "&q=" + city_name  +"&units=metric"
    
    # get method of requests module 
    # return response object 
    response = requests.get(complete_url) 
    
    # json method of response object  
    # convert json format data into 
    # python format data 
    x = response.json() 
    
    # declare rabbitmq queue
    # Now x contains list of nested dictionaries 
    # Check the value of "cod" key is equal to 
    # "404", means city is found otherwise, 
    # city is not found 
    if x["cod"] != "404": 
        i=0
        ret = "{"
        while(i<= 39):
            y=x["list"]
            z=y[i]
            main = z["main"]
            curtemp = main["temp"]
            weather = z["weather"]
            q=weather[0]
            desc = q["description"]
            req = requests.post("http://localhost/rabbitmq/send.php/?date="+z["dt_txt"] +"&description="+desc+"&temperature="+ str(curtemp))
            jsondata = '{"date" : "'+z["dt_txt"]+'", "current temp" : "'+str(curtemp)+'", "description" :"'+ desc+'"},'
            
            ret = ret + jsondata
            i = i+8
        ret = json.dumps(ret[:-1]+"}")
        ret = json.loads(ret)
        return (ret)    
    else: 
        return(" City Not Found ")

@app.route('/updates')
def updates():
    f = open('update.txt', 'r')
    x = f.readlines()

    output = '{'

    for item in x:
        #   "line1": "item1",
        output = output + '"line": "'+item + '",'
    f.close()

    output = output[:-1]

    output = output + '}'

    return output   

@app.route('/ping')
def ping():
    return 'Pong'

@app.route('/callClient')
def call_rpc():
    temp = int(request.args.get('temp'))
    with xmlrpc.client.ServerProxy("http://localhost:8001/") as proxy:
        return proxy.temperature(temp)

@app.route('/students')
def call_graphql(): 
    # Select your transport with a defined url endpoint
    transport = AIOHTTPTransport(url="http://localhost:4000/graphql")

    # Create a GraphQL client using the defined transport
    client = Client(transport=transport, fetch_schema_from_transport=True)

    studentid = request.args.get('studentid')
    studentname = request.args.get('studentname')
    studentdob = request.args.get('studentdob')
    if(studentdob is not None):
        #query by dob
        query = gql(
                    """
                    query QueryByDob($dob:String!)
                    {
                    studentQueryByDob(studentdob:$dob)
                    {
                        studentid
                        studentdob
                        studentname
                    } 
                    }                  
                    """   ) 
        params = {
            "dob" : studentdob
        }
        result = client.execute(query, variable_values= params)
        return(result)
    elif(studentid is not None):
        #query by id
        query = gql(
                    """
                    query QueryById($id:String!)
                    {
                    studentQueryById(studentid:$id)
                    {
                        studentid
                        studentdob
                        studentname
                    } 
                    }                  
                    """   ) 
        params = {
            "id" : studentid
        }
        result = client.execute(query, variable_values= params)
        return(result)
    elif(studentname is not None):
        query = gql(
                    """
                    query QueryByName($name:String!)
                    {
                    studentQueryByName(studentname:$name)
                    {
                        studentid
                        studentdob
                        studentname
                    } 
                    }                  
                    """   ) 
        params = {
            "name" : studentname
        }
        result = client.execute(query, variable_values= params)
        return(result)
    else:
        return "please enter a single attribute" + studentid + '-' + studentname + '-' + studentdob<file_sep><?php

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('hello', false, false, false, false);

if(!isset($_REQUEST['date'],
            $_REQUEST['description'],
            $_REQUEST['temperature']))
      return 0;
   //sanitize data

$date = filter_var($_REQUEST['date'], FILTER_SANITIZE_SPECIAL_CHARS);
$description = filter_var($_REQUEST['description'], FILTER_SANITIZE_SPECIAL_CHARS); 
$temp = filter_var($_REQUEST['temperature'], FILTER_SANITIZE_SPECIAL_CHARS);

$msg = new AMQPMessage("from php call Weather for " . $date . " is " . $description . "with temperature of " . (string)$temp);
$channel->basic_publish($msg, '', 'hello');<file_sep>from xmlrpc.server import SimpleXMLRPCServer

def temperature(temp):
    if (0 <= temp <= 10):
            return "cold"
    elif(11 <= temp <=20):
        return "warm"
    else:
        return "out of scope of specifications"

server = SimpleXMLRPCServer(("localhost", 8001))
print("Listening on port 8001...")
server.register_function(temperature, "temperature")
server.serve_forever()<file_sep>var express = require('express');
var { graphqlHTTP } = require('express-graphql');
var { buildSchema } = require('graphql');
 
var schema = buildSchema(`
  type User {
    id: String
    name: String
  }
  
   type Student {
    studentid: String
    studentname: String
    studentdob: String
  }
 

  type Query {
    usercall(id: String): User
    studentQueryById(studentid: String): Student
    studentQueryByName(studentname: String): [Student]
    studentQueryByDob(studentdob: String): [Student]
    othercall: String
  }
`);
 
// Maps id to User object
var fakeDatabase = {
  'a': {
    id: 'a',
    name: 'alice',
  },
  'b': {
    id: 'b',
    name: 'bob',
  },
  'c': {
    id: 'c',
    name: 'carl',
  },
  
};

var studentDatabase = {
 'a': {
    studentid: 'b00001',
    studentname: 'alice',
    studentdob: '99'
  },
  'b': {
    studentid: 'b00002',
    studentname: 'bob',
    studentdob: '88'
  },
  'c': {
    studentid: 'b00003',
    studentname: 'tom',
    studentdob: '91'
  },
  'd': {
    studentid: 'b00004',
    studentname: 'jim',
    studentdob: '94'
  },
  'e': {
    studentid: 'b00005',
    studentname: 'bill',
    studentdob: '99'
  },
    
};
 
var root = {
  usercall: ({id}) => {
    return fakeDatabase[id]; // mapping to the data
  },
  
  studentQueryById: ({studentid}) => {
     // send back the data  
     
    for (let [key, value] of Object.entries(studentDatabase)) {
      if(value.studentid == studentid)
      {
        return studentDatabase[key]
      }
    }
  },
  studentQueryByName: ({studentname}) => {
     // send back the data   
     var output = []; 
     for (let [key, value] of Object.entries(studentDatabase)) {
      if(value.studentname == studentname)
      {
        output.push(studentDatabase[key])
      }
    }
    return output
  },
  studentQueryByDob: ({studentdob}) => {
     // send back the data 
     var output = []; 
     for (let [key, value] of Object.entries(studentDatabase)) {
      if(value.studentdob == studentdob)
      {
        output.push(studentDatabase[key])
      }
    }
    return output
  },
  
  
   othercall: ({}) => {
  return 'This is the other call';
  }
};
 
var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');