From cf721cc4af7fa129cbbd901a3b5110d385200391 Mon Sep 17 00:00:00 2001 From: Whizel Date: Sat, 6 Apr 2019 18:02:27 +0200 Subject: [PATCH 1/3] Added post and get function --- .../my-button/my-button.component.css | 8 +++++++ .../my-button/my-button.component.html | 3 +++ .../my-button/my-button.component.ts | 23 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 client/Nightr/src/app/component/my-button/my-button.component.css create mode 100644 client/Nightr/src/app/component/my-button/my-button.component.html create mode 100644 client/Nightr/src/app/component/my-button/my-button.component.ts diff --git a/client/Nightr/src/app/component/my-button/my-button.component.css b/client/Nightr/src/app/component/my-button/my-button.component.css new file mode 100644 index 0000000..7e60ba7 --- /dev/null +++ b/client/Nightr/src/app/component/my-button/my-button.component.css @@ -0,0 +1,8 @@ +.myButtonComponent { + background-color: #30bcff; + border-radius: 100; + width: 200; + height: 200; + text-align: center; + vertical-align: middle; +} diff --git a/client/Nightr/src/app/component/my-button/my-button.component.html b/client/Nightr/src/app/component/my-button/my-button.component.html new file mode 100644 index 0000000..a0bcea7 --- /dev/null +++ b/client/Nightr/src/app/component/my-button/my-button.component.html @@ -0,0 +1,3 @@ + + + diff --git a/client/Nightr/src/app/component/my-button/my-button.component.ts b/client/Nightr/src/app/component/my-button/my-button.component.ts new file mode 100644 index 0000000..01fb236 --- /dev/null +++ b/client/Nightr/src/app/component/my-button/my-button.component.ts @@ -0,0 +1,23 @@ +import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core'; +import { TouchGestureEventData, GestureEventData } from 'tns-core-modules/ui/gestures' + +@Component({ + selector: 'ns-my-button', + templateUrl: './my-button.component.html', + styleUrls: ['./my-button.component.css'], + moduleId: module.id, +}) +export class MyButtonComponent implements OnInit { + + @Output() tap: EventEmitter = new EventEmitter(); + @Input() text: string; + + constructor() { } + + ngOnInit() { + } + + public onTap(args: GestureEventData): any { + this.tap.emit(args); + } +} From ca86d48e133a78e3a2fd068620e193fe111dc3f9 Mon Sep 17 00:00:00 2001 From: Whizel Date: Sat, 6 Apr 2019 18:03:40 +0200 Subject: [PATCH 2/3] Added post and get function --- client/Nightr/src/app/app.component.css | 5 +++ client/Nightr/src/app/app.component.html | 10 ++++-- client/Nightr/src/app/app.component.ts | 36 +++++++++++++++++-- client/Nightr/src/app/app.module.ts | 4 ++- .../src/app/my-button/my-button.component.css | 1 - .../app/my-button/my-button.component.html | 3 -- .../src/app/my-button/my-button.component.ts | 16 --------- .../src/app/services/my-http-get.service.ts | 9 +++++ .../src/app/services/my-http-post-service.ts | 24 +++++++++++++ 9 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 client/Nightr/src/app/app.component.css delete mode 100644 client/Nightr/src/app/my-button/my-button.component.css delete mode 100644 client/Nightr/src/app/my-button/my-button.component.html delete mode 100644 client/Nightr/src/app/my-button/my-button.component.ts create mode 100644 client/Nightr/src/app/services/my-http-get.service.ts create mode 100644 client/Nightr/src/app/services/my-http-post-service.ts diff --git a/client/Nightr/src/app/app.component.css b/client/Nightr/src/app/app.component.css new file mode 100644 index 0000000..18496f5 --- /dev/null +++ b/client/Nightr/src/app/app.component.css @@ -0,0 +1,5 @@ +.float-btn-container + { + margin-top: 35%; + margin-left: 20%; + } diff --git a/client/Nightr/src/app/app.component.html b/client/Nightr/src/app/app.component.html index 6dea362..7aaab14 100644 --- a/client/Nightr/src/app/app.component.html +++ b/client/Nightr/src/app/app.component.html @@ -1,3 +1,9 @@ - - + + + + + + + + diff --git a/client/Nightr/src/app/app.component.ts b/client/Nightr/src/app/app.component.ts index d88b2fe..76d0c64 100644 --- a/client/Nightr/src/app/app.component.ts +++ b/client/Nightr/src/app/app.component.ts @@ -1,8 +1,40 @@ import { Component } from "@angular/core"; +import * as dialogs from "tns-core-modules/ui/dialogs"; +import { MyHttpPostService } from './services/my-http-post-service' +import { TouchGestureEventData, GestureEventData } from 'tns-core-modules/ui/gestures' @Component({ selector: "ns-app", moduleId: module.id, - templateUrl: "./app.component.html" + templateUrl: "./app.component.html", + styleUrls: ['./app.component.css'], + providers: [MyHttpPostService] }) -export class AppComponent { } +export class AppComponent { + public user: string = ""; + public pass: string = ""; + returnMessage: string = ""; + + constructor(private myHttpPostSerivce: MyHttpPostService) { } + + public onTap(args: GestureEventData): any { + this.submit(); + dialogs.confirm("Should be result").then(result => { + console.log("Dialog result: " + result); + }); + } + + public submit(): void { + this.makePostRequest(); + } + + private makePostRequest(): void { + console.log('Reached makepostRequest'); + this.myHttpPostSerivce + .postData({ username: this.user, password: this.pass }) + .subscribe(res => { + console.log('This is res', res); + this.returnMessage = (res).json.data.username; + }); + } +} diff --git a/client/Nightr/src/app/app.module.ts b/client/Nightr/src/app/app.module.ts index d5d8d40..3b24722 100644 --- a/client/Nightr/src/app/app.module.ts +++ b/client/Nightr/src/app/app.module.ts @@ -2,7 +2,8 @@ import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core"; import { NativeScriptModule } from "nativescript-angular/nativescript.module"; import { AppComponent } from "./app.component"; -import { MyButtonComponent } from './my-button/my-button.component'; +import { MyButtonComponent } from './component/my-button/my-button.component'; +import { NativeScriptHttpClientModule } from "nativescript-angular/http-client"; // Uncomment and add to NgModule imports if you need to use two-way binding // import { NativeScriptFormsModule } from "nativescript-angular/forms"; @@ -16,6 +17,7 @@ import { MyButtonComponent } from './my-button/my-button.component'; ], imports: [ NativeScriptModule, + NativeScriptHttpClientModule, ], declarations: [ AppComponent, diff --git a/client/Nightr/src/app/my-button/my-button.component.css b/client/Nightr/src/app/my-button/my-button.component.css deleted file mode 100644 index bcd4764..0000000 --- a/client/Nightr/src/app/my-button/my-button.component.css +++ /dev/null @@ -1 +0,0 @@ -/* Add mobile styles for the component here. */ diff --git a/client/Nightr/src/app/my-button/my-button.component.html b/client/Nightr/src/app/my-button/my-button.component.html deleted file mode 100644 index 59259f8..0000000 --- a/client/Nightr/src/app/my-button/my-button.component.html +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/client/Nightr/src/app/my-button/my-button.component.ts b/client/Nightr/src/app/my-button/my-button.component.ts deleted file mode 100644 index d18c116..0000000 --- a/client/Nightr/src/app/my-button/my-button.component.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Component, OnInit } from '@angular/core'; - -@Component({ - selector: 'ns-my-button', - templateUrl: './my-button.component.html', - styleUrls: ['./my-button.component.css'], - moduleId: module.id, -}) -export class MyButtonComponent implements OnInit { - - constructor() { } - - ngOnInit() { - } - -} diff --git a/client/Nightr/src/app/services/my-http-get.service.ts b/client/Nightr/src/app/services/my-http-get.service.ts new file mode 100644 index 0000000..e26ecc4 --- /dev/null +++ b/client/Nightr/src/app/services/my-http-get.service.ts @@ -0,0 +1,9 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class MyHttpGetService { + + constructor() { } +} diff --git a/client/Nightr/src/app/services/my-http-post-service.ts b/client/Nightr/src/app/services/my-http-post-service.ts new file mode 100644 index 0000000..8dca261 --- /dev/null +++ b/client/Nightr/src/app/services/my-http-post-service.ts @@ -0,0 +1,24 @@ +import { Injectable } from "@angular/core"; +import { HttpClient, HttpHeaders } from "@angular/common/http"; + +@Injectable({ + providedIn: 'root' + }) +export class MyHttpPostService { + private serverUrl = "https://nightr.caspervk.net"; + + constructor(private http: HttpClient) { } + + postData(data: any) { + console.log('logged data is', data); + let options = this.createRequestOptions(); + return this.http.post(this.serverUrl, { data }, { headers: options }); + } + + private createRequestOptions() { + let headers = new HttpHeaders({ + "Content-Type": "application/json" + }); + return headers; + } +} From e809c1ccd2a17503eb314904d4f37a364a29dc4f Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 18:07:10 +0200 Subject: [PATCH 3/3] Log POST requests. --- server/nightr/app.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index 3f8b923..f1f534e 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -1,4 +1,5 @@ import inspect +import json import statistics import timeit from dataclasses import asdict @@ -7,7 +8,7 @@ from logging import DEBUG from typing import List import requests_cache -from flask import Flask, jsonify, logging +from flask import Flask, jsonify, logging, request from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour from .util import Context @@ -33,8 +34,10 @@ strategies = { @app.route("/", methods=["GET", "POST"]) def probabilities(): - phone_data = {} # TODO: get from POST request + phone_data = request.get_json(force=True) context = Context(**phone_data) + logger.debug("phone_data:\n%s", json.dumps(phone_data, indent=2)) + logger.debug("Context: %s", context) predictions: List[dict] = [] for name, strategy in strategies.items():