Skip to content

Commit

Permalink
Merge branch 'feature/api-header' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredfrancis committed May 9, 2018
2 parents 7690bbd + b6b0c53 commit 9028d61
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
5 changes: 3 additions & 2 deletions app/endpoint/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def api():
if intent.apiTrigger:
isJson = False
parameters = result_json["extractedParameters"]

headers = intent.apiDetails.get_headers()
app.logger.info("headers %s"%headers)
url_template = Template(
intent.apiDetails.url, undefined=SilentUndefined)
rendered_url = url_template.render(**context)
Expand All @@ -181,7 +182,7 @@ def api():

try:
result = call_api(rendered_url,
intent.apiDetails.requestType,
intent.apiDetails.requestType,headers,
parameters, isJson)
except Exception as e:
app.logger.warn("API call failed", e)
Expand Down
14 changes: 7 additions & 7 deletions app/endpoint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_synonyms():
app.logger.info("loaded synonyms %s",synonyms)
return synonyms

def call_api(url, type, parameters = {}, is_json=False):
def call_api(url, type,headers={}, parameters = {}, is_json=False):
"""
Call external API
:param url:
Expand All @@ -33,19 +33,19 @@ def call_api(url, type, parameters = {}, is_json=False):
"""
app.logger.info("Initiating API Call with following info: url => {} payload => {}".format(url,parameters))
if "GET" in type:
response = requests.get(url, params=parameters)
response = requests.get(url,headers=headers, params=parameters)
elif "POST" in type:
if is_json:
response = requests.post(url, json=parameters)
response = requests.post(url,headers=headers, json=parameters)
else:
response = requests.post(url, params=parameters)
response = requests.post(url,headers=headers, params=parameters)
elif "PUT" in type:
if is_json:
response = requests.put(url, json=parameters)
response = requests.put(url,headers=headers, json=parameters)
else:
response = requests.put(url, params=parameters)
response = requests.put(url,headers=headers, params=parameters)
elif "DELETE" in type:
response = requests.delete(url, params=parameters)
response = requests.delete(url,headers=headers, params=parameters)
else:
raise Exception("unsupported request method.")
result = json.loads(response.text)
Expand Down
1 change: 1 addition & 0 deletions app/intents/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def create_intent():
api_details.jsonData = content.get("apiDetails").get("jsonData")

api_details.url = content.get("apiDetails").get("url")
api_details.headers = content.get("apiDetails").get("headers")
api_details.requestType = content.get("apiDetails").get("requestType")
intent.apiDetails = api_details
else:
Expand Down
6 changes: 6 additions & 0 deletions app/intents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ class ApiDetails(EmbeddedDocument):
"DELETE",
"PUT"],
required=True)
headers = ListField(default=[])
isJson = BooleanField(default=False)
jsonData = StringField(default="{}")

def get_headers(self):
headers = {}
for header in self.headers:
headers[header["headerKey"]]=header["headerValue"]
return headers

class Intent(Document):
name = StringField(max_length=100, required=True, unique=True)
Expand Down
20 changes: 19 additions & 1 deletion frontend/src/app/agent/intent/intent.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,27 @@ <h2>Parameters
<h3>
<mat-checkbox formControlName="apiTrigger">API trigger</mat-checkbox>
</h3>
<section [formGroup]="intentForm.controls.apiDetails" *ngIf="apiTrigger()" fxLayout="column" fxLayoutGap="10px">

<section [formGroup]="intentForm.controls.apiDetails" *ngIf="apiTrigger()" fxLayout="column" fxLayoutGap="10px">
<h3>HTTP Headers
<button type="button" mat-mini-fab (click)="addHeader()" color="primary">
<mat-icon aria-label="Add header">add</mat-icon>
</button>
</h3>
<div class="api-headers" *ngFor="let header of intentForm.controls.apiDetails.controls.headers.controls; let j=index;" fxLayoutGap="10px">
<div fxLayout="row" [formGroup]="header" fxLayoutGap="10px">
<mat-form-field fxFlex="40">
<input matInput formControlName="headerKey" placeholder="new key">
</mat-form-field>
<mat-form-field fxFlex="40">
<input matInput formControlName="headerValue" placeholder="value">
</mat-form-field>
<button mat-icon-button (click)="deleteHeader(j)">
<mat-icon aria-label="Delete this Header">delete</mat-icon>
</button>
</div>

</div>

<div fxLayout="row" fxLayoutGap="20px">
<mat-form-field fxFlex="90">
Expand Down
25 changes: 23 additions & 2 deletions frontend/src/app/agent/intent/intent.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class IntentComponent implements OnInit {
intentId: [''],
userDefined: [true],
speechResponse: [''],
apiTrigger: [''],
apiTrigger: [false],
apiDetails: this.initApiDetails(),
parameters: this.fb.array(
this.intent && this.intent.parameters ? this.intent.parameters.map(n => {
Expand Down Expand Up @@ -99,8 +99,12 @@ export class IntentComponent implements OnInit {

initApiDetails(parameter = null) {
const fields = {
isJson: [''],
isJson: [false],
url: [''],
headers: this.fb.array(
this.intent && this.intent.apiTrigger ? this.intent.apiDetails.headers.map(n => {
return this.initApiHeaders();
}) : []),
requestType: [''],
jsonData: ['']
};
Expand All @@ -110,7 +114,24 @@ export class IntentComponent implements OnInit {
}
return g;
}
initApiHeaders() {
const fields = {
headerKey: [''],
headerValue: [''],
};
const g = this.fb.group(fields);
return g;
}

addHeader(){
const header = <FormArray>this.intentForm.controls["apiDetails"]["controls"]["headers"];
header.push(this.initApiHeaders());

}
deleteHeader(j) {
const control = <FormArray>this.intentForm.controls["apiDetails"]["controls"]["headers"];
control.removeAt(j);
}
save() {
const form = this.intentForm.value;
if (form._id && form._id.$oid) {
Expand Down

0 comments on commit 9028d61

Please sign in to comment.