-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSRPochtaManager.m
88 lines (72 loc) · 3.38 KB
/
PSRPochtaManager.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
//
// PSRPochtaManager.m
// OSRAPIServices
//
// Created by n.shubenkov on 03/10/14.
// Copyright (c) 2014 n.shubenkov. All rights reserved.
//
#import "PSRPochtaManager.h"
#import "AFNetworking.h"
@implementation PSRPochtaManager
- (instancetype)init
{
self = [super initWithBaseURL:[NSURL URLWithString:@"http://95.128.178.180:5432"]];
if (self){
self.requestSerializer = [AFHTTPRequestSerializer new];
self.responseSerializer = [AFJSONResponseSerializer new];
self.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"text/html",@"application/json"] ];
}
return self;
}
- (void)getOfficesOfCount:(int)count complition:(PSRPochtaCopmlition)complitionBlock
{
PSRPochtaCopmlition copiedComplitionBlock = [complitionBlock copy];
NSMutableDictionary *parameters = [self p_defaultParameters];
int top = 500;//max offices count
int radius = 5000;
NSString *type = @"ALL";
[parameters addEntriesFromDictionary:@{
@"top" :@(top),
@"latitude" :@(57),
@"longitude" :@(37),
@"searchRadius" :@(radius),
@"type" :type
}];
[self GET:@"mobile-api/method/offices.find.nearby"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
copiedComplitionBlock(responseObject, YES);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
copiedComplitionBlock(error, NO);
}];
}
- (AFHTTPRequestOperation *)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];
[request setValue:@"Accept"
forHTTPHeaderField:@"application/json; charset=utf-8"];
[request setValue:@"RussianPost/2.1"
forHTTPHeaderField:@"User-Agent"];
// как хорошо, когда апи почты говорит в чем проблема
//{"code":"1005","desc":"Request is not authorized: Access token was either missing or invalid."}
[request setValue:@"Yi5GaWMTY8x27uEPte0/l9vfrZw="
forHTTPHeaderField:@"MobileApiAccessToken"];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self.operationQueue addOperation:operation];
return operation;
}
- (NSMutableDictionary *)p_defaultParameters
{
NSDateFormatter *formatter1 = [NSDateFormatter new];
formatter1.dateFormat = @"yyyy-MM-dd";
NSDateFormatter *formatter2 = [NSDateFormatter new];
formatter2.dateFormat = @"HH:mm:ss";
NSString *yearMonthday = [formatter1 stringFromDate:[NSDate date]];
NSString *hourMoniteSecond = [formatter2 stringFromDate:[NSDate date]];
NSString *time = [NSString stringWithFormat:@"%@T%@",yearMonthday,hourMoniteSecond];
return [@{
@"currentDateTime" :time,
} mutableCopy];
}
@end