Skip to content

Commit 16d9f47

Browse files
committed
CSHARP SDK
1 parent 0f14154 commit 16d9f47

File tree

3 files changed

+363
-2
lines changed

3 files changed

+363
-2
lines changed

Api.cs

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using System.Web;
5+
using System.Net;
6+
using System.IO;
7+
using System.Collections.Generic;
8+
using System.Net.Http;
9+
using System.Threading.Tasks;
10+
namespace Tracker
11+
{
12+
public class Api
13+
{
14+
public string apiKey = "Your Api Key";
15+
16+
private string basePath = "http://api.trackingmore.com";
17+
18+
private string apiVersion = "v3";
19+
20+
private string trackHeader = "Tracking-Api-Key";
21+
22+
private HttpClient httpClient;
23+
24+
public Api(string apiKey)
25+
{
26+
this.apiKey = apiKey;
27+
this.httpClient = new HttpClient();
28+
}
29+
30+
protected string getBaseUrl(string path)
31+
{
32+
return basePath+"/"+apiVersion + "/trackings/"+path;
33+
}
34+
35+
public HttpResponseMessage doRequest(string path,string data="",string method="GET")
36+
{
37+
List<KeyValuePair<string, string>> headers = new List<KeyValuePair<string, string>>();
38+
headers.Add(new KeyValuePair<string, string>(this.trackHeader,this.apiKey));
39+
40+
method = method.ToUpper();
41+
if (method=="GET")
42+
{
43+
return this.Get(this.getBaseUrl(path),headers);
44+
}else if(method=="Put"){
45+
return this.Put(this.getBaseUrl(path),data,headers);
46+
}else if(method=="Delete"){
47+
return this.Delete(this.getBaseUrl(path),data,headers);
48+
}else{
49+
return this.Post(this.getBaseUrl(path),data,headers);
50+
}
51+
}
52+
53+
54+
public HttpResponseMessage Put(string url, string content, List<KeyValuePair<string, string>> headers = null)
55+
{
56+
StringContent stringContent = new StringContent(content, Encoding.UTF8);
57+
if (headers != null && headers.Count > 0)
58+
{
59+
stringContent.Headers.Clear();
60+
foreach (var header in headers)
61+
{
62+
stringContent.Headers.Add(header.Key, header.Value);
63+
}
64+
}
65+
66+
HttpResponseMessage response = httpClient.PutAsync(new Uri(url), stringContent).Result;
67+
return response;
68+
}
69+
public HttpResponseMessage Delete(string url, string content, List<KeyValuePair<string, string>> headers = null)
70+
{
71+
StringContent stringContent = new StringContent(content, Encoding.UTF8);
72+
if (headers != null && headers.Count > 0)
73+
{
74+
stringContent.Headers.Clear();
75+
foreach (var header in headers)
76+
{
77+
stringContent.Headers.Add(header.Key, header.Value);
78+
}
79+
}
80+
81+
HttpResponseMessage response = httpClient.DeleteAsync(new Uri(url)).Result;
82+
return response;
83+
}
84+
85+
public HttpResponseMessage Post(string url, string content, List<KeyValuePair<string, string>> headers = null)
86+
{
87+
StringContent stringContent = new StringContent(content, Encoding.UTF8);
88+
if (headers != null && headers.Count > 0)
89+
{
90+
stringContent.Headers.Clear();
91+
foreach (var header in headers)
92+
{
93+
stringContent.Headers.Add(header.Key, header.Value);
94+
}
95+
}
96+
97+
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), stringContent).Result;
98+
return response;
99+
}
100+
101+
102+
public HttpResponseMessage Get(string url, List<KeyValuePair<string, string>> headers = null)
103+
{
104+
HttpRequestMessage request = new HttpRequestMessage()
105+
{
106+
RequestUri = new Uri(url),
107+
Method = HttpMethod.Get,
108+
};
109+
if (headers != null && headers.Count > 0)
110+
{
111+
request.Headers.Clear();
112+
113+
foreach (var header in headers)
114+
{
115+
request.Headers.Add(header.Key, header.Value);
116+
117+
}
118+
}
119+
120+
HttpResponseMessage response = httpClient.SendAsync(request).Result;
121+
return response;
122+
}
123+
}
124+
}

Program.cs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Linq;
3+
using System.Web;
4+
using System.Net;
5+
using System.IO;
6+
using System.Collections.Generic;
7+
using System.Net.Http;
8+
using System.Threading.Tasks;
9+
namespace Tracker
10+
{
11+
class Tracking
12+
{
13+
static void Main(string[] args)
14+
{
15+
Api api = new Api("Your Api Key");
16+
17+
// Get realtime tracking results of a single tracking
18+
string post = "{\"tracking_number\": \"EA152563254CN\", \"carrier_code\": \"china-ems\"}";
19+
HttpResponseMessage response = api.doRequest("realtime", post, "POST");
20+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
21+
22+
// count
23+
string count = "count?courier=1&limit=100&created_at_min=1521314361&created_at_max=1541314361";
24+
response = api.doRequest(count);
25+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
26+
27+
// // Get tracking results of a tracking or List all trackings
28+
// string get = "get?page=1&limit=100&created_at_min=1521314361&created_at_max=1541314361";
29+
// response = api.doRequest(get);
30+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
31+
32+
// // Update Tracking item
33+
// response = api.doRequest("modifyinfo", post, "PUT");
34+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
35+
36+
// // archive
37+
// response = api.doRequest("archive", post, "POST");
38+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
39+
40+
// // Delete tracking item
41+
// response = api.doRequest("delete?tracking_number=EA152563254CN", "", "DELETE");
42+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
43+
44+
// // create tracking number
45+
// response = api.doRequest("create", post, "POST");
46+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
47+
48+
// // manual update
49+
// response = api.doRequest("manualupdate", post, "POST");
50+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
51+
52+
// // remote tracking
53+
// response = api.doRequest("remote", post, "POST");
54+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
55+
56+
// // Get cost time iterm results
57+
// response = api.doRequest("transittime", post, "POST");
58+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
59+
60+
// // detect a carriers by tracking number
61+
// string post = "{ \"tracking_number\": \"EA152563254CN\" }";
62+
// response = api.doRequest("detect", post, "POST");
63+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
64+
65+
// // get all carriers
66+
// response = api.doRequest("carriers", post, "POST");
67+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
68+
69+
// // Get status number
70+
// string status = "status?tracking_number=EA152563254CN";
71+
// response = api.doRequest(status);
72+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
73+
74+
// // Set number not update
75+
// response = api.doRequest("notupdate", post, "POST");
76+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
77+
78+
// // Modify courier code
79+
// string post = "{\"tracking_number\": \"EA152563254CN\", \"courier_code\": \"china-ems\", \"new_courier_code\": \"china-post\"}";
80+
// response = api.doRequest("modifycourier", post, "PUT")
81+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
82+
83+
// // Get user info
84+
// response = api.doRequest("userinfo");
85+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
86+
87+
// // air real time track
88+
// response = api.doRequest("aircargo", post, "POST");
89+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
90+
}
91+
}
92+
}

README.md

+147-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,147 @@
1-
# tracking-sdk-csharp
2-
Trackingmore API Csharp sdk
1+
Trackingmore-CSHARP
2+
=================
3+
4+
The PHP SDK of Trackingmore API
5+
## Official document
6+
7+
[Document](https://www.trackingmore.com/v3/api-index)
8+
9+
##Init
10+
```
11+
Api api = new Api("Your Api Key");
12+
```
13+
14+
15+
Quick Start
16+
--------------
17+
- Put your ApiKey in the constructor of the Api class
18+
- All returns are in Json format String.
19+
- After instantiating the Api class, you can use its interface methods
20+
- You can set the sandbox of the Api instance to true to turn on the sandbox mode: <code>api.sandbox=true;</code>
21+
- Most Api params receive multiple tracking numbers
22+
23+
**Get a list of the couriers in Trackingmore**
24+
25+
string courier = "courier?lang=en";
26+
response = api.doRequest(courier);
27+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
28+
29+
**Detect which couriers defined in your account match a tracking number**
30+
31+
string post = "{ \"tracking_number\": \"EA152563254CN\" }";
32+
response = api.doRequest("detect", post, "POST");
33+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
34+
35+
36+
**Post trackings to your account**
37+
38+
//Create single tracking numbers
39+
string post = "{\"tracking_number\" => \"RP325552475CN\", \"carrier_code\" => \"china-post\"}";
40+
//Create multiple tracking numbers
41+
string post = "[{\"tracking_number\" => \"RP325552475CN\", \"carrier_code\" => \"china-post\"}",
42+
"{\"tracking_number\" => \"LZ448865302CN\", \"carrier_code\" => \"china-ems\"}]";
43+
response = api.doRequest("create", post, "POST");
44+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
45+
46+
**Summary of Connection API Methods with all the api and Methods**
47+
48+
// Get realtime tracking results of a single tracking
49+
string post = "{\"tracking_number\": \"EA152563254CN\", \"carrier_code\": \"china-ems\"}";
50+
HttpResponseMessage response = api.doRequest("realtime", post, "POST");
51+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
52+
53+
// count
54+
string count = "count?courier=1&limit=100&created_at_min=1521314361&created_at_max=1541314361";
55+
response = api.doRequest(count);
56+
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
57+
58+
// // Get tracking results of a tracking or List all trackings
59+
// string get = "get?page=1&limit=100&created_at_min=1521314361&created_at_max=1541314361";
60+
// response = api.doRequest(get);
61+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
62+
63+
// // Update Tracking item
64+
// response = api.doRequest("modifyinfo", post, "PUT");
65+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
66+
67+
// // archive
68+
// response = api.doRequest("archive", post, "POST");
69+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
70+
71+
// // Delete tracking item
72+
// response = api.doRequest("delete?num=EA152563254CN", "", "DELETE");
73+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
74+
75+
// // create tracking number
76+
// response = api.doRequest("create", post, "POST");
77+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
78+
79+
// // manual update
80+
// response = api.doRequest("manualupdate", post, "POST");
81+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
82+
83+
// // remote tracking
84+
// response = api.doRequest("remote", post, "POST");
85+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
86+
87+
// // Get cost time iterm results
88+
// response = api.doRequest("transittime", post, "POST");
89+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
90+
91+
// // detect a carriers by tracking number
92+
// string post = "{ \"num\": \"EA152563254CN\" }";
93+
// response = api.doRequest("detect", post, "POST");
94+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
95+
96+
// // get all carriers
97+
// response = api.doRequest("carriers", post, "POST");
98+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
99+
100+
// // Get status number
101+
// string status = "status?num=EA152563254CN";
102+
// response = api.doRequest(status);
103+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
104+
105+
// // Set number not update
106+
// response = api.doRequest("notupdate", post, "POST");
107+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
108+
109+
// // Modify courier code
110+
// string post = "{\"num\": \"EA152563254CN\", \"express\": \"china-ems\", \"new_express\": \"china-post\"}";
111+
// response = api.doRequest("modifycourier", post, "PUT")
112+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
113+
114+
// // Get user info
115+
// response = api.doRequest("userinfo");
116+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
117+
118+
// // air real time track
119+
// response = api.doRequest("aircargo", post, "POST");
120+
// Console.WriteLine(response.Content.ReadAsStringAsync().Result);
121+
122+
## Typical Server Responses
123+
124+
We will respond with one of the following status codes.
125+
126+
Code|Type | Message
127+
----|--------------|-------------------------------
128+
200 | <code>Success</code>| Request response is successful
129+
203 | <code>PaymentRequired</code>| API service is only available for paid account Please subscribe paid plan to unlock API services ul
130+
204 | <code>No Content</code>| Request was successful, but no data returned Tracking NO. or target data possibly do not exist
131+
400 | <code>Bad Request</code>| Request type error Please check the API documentation for the request type of this API
132+
401 | <code>Unauthorized</code>| Authentication failed or has no permission Please check and ensure your API Key is correct
133+
403 | <code>Bad Request</code>| Page does not exist Please check and ensure your link is correct ul
134+
404 | <code>Not Found</code>| Page does not exist Please check and ensure your link is correct
135+
408 | <code>Time Out</code>| Request timeout The official website did not return data, please try again later
136+
411 | <code>Bad Request</code>| Specified request parameter length exceeds length limit Please check and ensure that the request parameters are of the required length
137+
412 | <code>Bad Request</code>| Specified request parameter format doesn't meet requirements Please check and ensure that the request parameters are in the required format
138+
413 | <code>Out limited</code>| The number of request parameters exceeds the limit Please check the API documentation for the limit of this API
139+
417 | <code>Bad Request</code>| Missing request parameters or request parameters cannot be parsed Please check and ensure that the request parameters are complete and correctly formatted
140+
421 | <code>Bad Request</code>| Some of required parameters are empty Some couriers need special parameters to track logistics information (Special Couriers)
141+
422 | <code>Bad Request</code>| Unidentifiable courier code Please check and ensure that the courier code are correct(Courier code)
142+
423 | <code>Bad Request</code>| Tracking No. already exists
143+
424 | <code>Bad Request</code>| Tracking No. no exists Please use 「Create trckings」 API first to create trackings
144+
429 | <code>Bad Request</code>| Exceeded API request limits, please try again later Please check the API documentation for the limit of this API
145+
511 | <code>Server Error</code>| Server error Please contact us: [email protected].
146+
512 | <code>Server Error</code>| Server error Please contact us: [email protected].
147+
513 | <code>Server Error</code>| Server error Please contact us: [email protected].

0 commit comments

Comments
 (0)