This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Controller.m
113 lines (96 loc) · 3.07 KB
/
Controller.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// WindowController.m
// WrapperTest
//
// Created by Adrian on 10/18/08.
// Copyright 2008 Adrian Kosmaczewski. All rights reserved.
//
#import "Controller.h"
#import "Wrapper.h"
@implementation Controller
#pragma mark -
#pragma mark Constructor and destructor
- (id)init
{
if (self = [super init])
{
engine = [[Wrapper alloc] init];
engine.delegate = self;
}
return self;
}
- (void)dealloc
{
[engine release];
engine = nil;
[super dealloc];
}
- (void)awakeFromNib
{
[popUpButton removeAllItems];
[popUpButton addItemsWithTitles:[NSArray arrayWithObjects:@"GET", @"POST", @"PUT", @"DELETE", nil]];
}
#pragma mark -
#pragma mark WrapperDelegate methods
- (void)wrapper:(Wrapper *)wrapper didRetrieveData:(NSData *)data
{
NSString *text = [engine responseAsText];
if (text != nil)
{
[output setStringValue:text];
}
[indicator stopAnimation:self];
}
- (void)wrapperHasBadCredentials:(Wrapper *)wrapper
{
[indicator stopAnimation:self];
NSAlert *alert = [NSAlert alertWithMessageText:@"Bad credentials!"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:nil];
[alert runModal];
}
- (void)wrapper:(Wrapper *)wrapper didCreateResourceAtURL:(NSString *)url
{
[indicator stopAnimation:self];
NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"Resource created at %@!", url]
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:nil];
[alert runModal];
}
- (void)wrapper:(Wrapper *)wrapper didFailWithError:(NSError *)error
{
[indicator stopAnimation:self];
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
}
- (void)wrapper:(Wrapper *)wrapper didReceiveStatusCode:(int)statusCode
{
[indicator stopAnimation:self];
NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"Status code not OK: %d!", statusCode]
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:nil];
[alert runModal];
}
#pragma mark -
#pragma mark IBAction methods
- (IBAction)launch:(id)sender
{
NSURL *url = [NSURL URLWithString:[address stringValue]];
NSDictionary *parameters = nil;
if ([[parameter stringValue] length] > 0)
{
NSArray *keys = [NSArray arrayWithObjects:@"parameter", nil];
NSArray *values = [NSArray arrayWithObjects:[parameter stringValue], nil];
parameters = [NSDictionary dictionaryWithObjects:values forKeys:keys];
}
[indicator startAnimation:self];
[output setStringValue:@""];
[engine sendRequestTo:url usingVerb:[popUpButton titleOfSelectedItem] withParameters:parameters];
}
@end