-
Notifications
You must be signed in to change notification settings - Fork 0
/
WithingsAPIDataStore.m
86 lines (61 loc) · 3.14 KB
/
WithingsAPIDataStore.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
//
// WithingsAPIDataStore.m
// PulseBeat
//
// Created by Omar Metwally on 10/9/13.
// Copyright (c) 2013 Logisome. All rights reserved.
//
#import "WithingsAPIDataStore.h"
#import "WithingsJSONData.h"
#import "WithingsOAuth1Controller.h"
#import "MetasomeDataPointStore.h"
@implementation WithingsApiDataStore
+(WithingsApiDataStore *)sharedStore
{
static WithingsApiDataStore *sharedStore = nil;
if (!sharedStore) {
sharedStore = [[super allocWithZone:nil] init];
}
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone
{
return [self sharedStore];
}
-(id)init
{
self = [super init];
if (self) {
}
return self;
}
- (void)getBloodPressureData:(NSString *)oauthTokenIn oauthSecretIn:(NSString *)oauthSecretIn userID:(NSString *)userid withCompletion:(void (^)(void))completion
{
NSString *path = @"measure";
NSMutableDictionary *moreParams = [[NSMutableDictionary alloc] init];
[moreParams setValue:@"getmeas" forKey:@"action"];
[moreParams setValue:userid forKey:@"userid"];
NSURLRequest *preparedRequest = [WithingsOAuth1Controller preparedRequestForPath:path
parameters:moreParams
HTTPmethod:@"GET"
oauthToken:oauthTokenIn
oauthSecret:oauthSecretIn];
[NSURLConnection sendAsynchronousRequest:preparedRequest
queue:NSOperationQueue.mainQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(@"path35 %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
if (error) {
NSLog(@"Error in API request: %@", error.localizedDescription);
return;
}
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
WithingsJSONData *withingsJSONData = [[WithingsJSONData alloc] initWithDictionary:d dataName:@"body"];
// clear pre-existing Withings data before populating DB with new data
[[MetasomeDataPointStore sharedStore] deletePointsFromApi:@"Withings" fromDate:nil toDate:nil];
[withingsJSONData saveToDataPointStore:@"test"];
completion();
});
}];
}
@end