Skip to content

Commit

Permalink
Re-add cainiao, fix typo in name
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Oct 15, 2023
1 parent eb86562 commit ae2b17b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 139 deletions.
105 changes: 105 additions & 0 deletions providers/cainiao/cainiao_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cainiao

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/commonerrors"
)

type CainiaoProvider struct{}

func (pp *CainiaoProvider) GetName() string {
return "cainiao"
}

func (pp *CainiaoProvider) MatchesNumber(trackingNumber string) bool {
return true
}

func (pp *CainiaoProvider) Track(ctx context.Context, trackingNumber string) (*commondata.TrackingData, error) {

req, err := http.NewRequest(
"GET",
"https://global.cainiao.com/global/detail.json?mailNos="+url.QueryEscape(trackingNumber)+"&lang=en-US&language=en-US",
nil,
)
if err != nil {
return nil, fmt.Errorf("failed to create GET request: %w", err)
}
commondata.SetCommonHTTPHeaders(&req.Header)
httpResponse, err := http.DefaultClient.Do(req)

Check failure on line 36 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

response body must be closed (bodyclose)

Check failure on line 36 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

response body must be closed (bodyclose)
if err != nil {
return nil, commonerrors.NewNetworkError(pp.GetName(), req)
}

if httpResponse.StatusCode != 200 {
return nil, commonerrors.NotFoundError
}

var cainiaoResponse CainiaoResponse
err = json.NewDecoder(httpResponse.Body).Decode(&cainiaoResponse)
if err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
if len(cainiaoResponse.Module) == 0 {
return nil, commonerrors.NotFoundError
}
module := cainiaoResponse.Module[0]
if len(module.DetailList) == 0 {
return nil, commonerrors.NotFoundError
}

td := &commondata.TrackingData{
ProviderName: pp.GetName(),
ShipmentNumber: trackingNumber,
TrackingSteps: make([]*commondata.TrackingStep, 0),
Destination: module.DestCountry,
SentFrom: module.OriginCountry,
}

for _, detail := range module.DetailList {

date, _ := time.Parse("2006-01-02 15:04:05", detail.TimeStr)
step := &commondata.TrackingStep{
Datetime: date,
Message: detail.Desc,
Location: detail.Group.NodeDesc,
CommonType: commondata.CommonTrackingStepType_UNKNOWN,
}
td.TrackingSteps = append(td.TrackingSteps, step)
}

// attempt to augement the destination with the city
// requires a separate request, if it fails, it's not a big deal
cityReq, err := http.NewRequest(
"GET",
"https://global.cainiao.com/global/getCity.json?mailNo="+url.QueryEscape(trackingNumber)+"&lang=en-US&language=en-US",
nil,
)
if err != nil {
return td, nil

Check failure on line 86 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

error is not nil (line 80) but it returns nil (nilerr)

Check failure on line 86 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

error is not nil (line 80) but it returns nil (nilerr)
}
commondata.SetCommonHTTPHeaders(&cityReq.Header)
cityResp, err := http.DefaultClient.Do(cityReq)

Check failure on line 89 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

response body must be closed (bodyclose)

Check failure on line 89 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

response body must be closed (bodyclose)
if err != nil {
return td, nil

Check failure on line 91 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

error is not nil (line 89) but it returns nil (nilerr)

Check failure on line 91 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

error is not nil (line 89) but it returns nil (nilerr)
}
if cityResp.StatusCode != 200 {
return td, nil
}
var cityResponse GetCityResponse
err = json.NewDecoder(cityResp.Body).Decode(&cityResponse)
if err != nil || !cityResponse.Success {
return td, nil

Check failure on line 99 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

error is not nil (line 97) but it returns nil (nilerr)

Check failure on line 99 in providers/cainiao/cainiao_provider.go

View workflow job for this annotation

GitHub Actions / build-and-push-image

error is not nil (line 97) but it returns nil (nilerr)
}

td.Destination = cityResponse.Module + ", " + td.Destination

return td, nil
}
80 changes: 80 additions & 0 deletions providers/cainiao/json_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cainiao

type CainiaoResponse struct {
Module []Module `json:"module"`
Success bool `json:"success"`
}

type GetCityResponse struct {
Module string `json:"module"`
Success bool `json:"success"`
}

type ProgressPointList struct {
PointName string `json:"pointName"`
Light bool `json:"light,omitempty"`
Reload bool `json:"reload,omitempty"`
}
type ProcessInfo struct {
ProgressStatus string `json:"progressStatus"`
ProgressRate float64 `json:"progressRate"`
Type string `json:"type"`
ProgressPointList []ProgressPointList `json:"progressPointList"`
}
type GlobalEtaInfo struct {
EtaDesc string `json:"etaDesc"`
DeliveryMinTime int64 `json:"deliveryMinTime"`
DeliveryMaxTime int64 `json:"deliveryMaxTime"`
}
type Group struct {
NodeCode string `json:"nodeCode"`
NodeDesc string `json:"nodeDesc"`
CurrentIconURL string `json:"currentIconUrl"`
HistoryIconURL string `json:"historyIconUrl"`
}
type GlobalCombinedLogisticsTraceDTO struct {
Time int64 `json:"time"`
TimeStr string `json:"timeStr"`
Desc string `json:"desc"`
StanderdDesc string `json:"standerdDesc"`
DescTitle string `json:"descTitle"`
TimeZone string `json:"timeZone"`
ActionCode string `json:"actionCode"`
Group Group `json:"group"`
}
type LatestTrace struct {
Time int64 `json:"time"`
TimeStr string `json:"timeStr"`
Desc string `json:"desc"`
StanderdDesc string `json:"standerdDesc"`
DescTitle string `json:"descTitle"`
TimeZone string `json:"timeZone"`
ActionCode string `json:"actionCode"`
Group Group `json:"group"`
}
type DetailListItem struct {
Time int64 `json:"time"`
TimeStr string `json:"timeStr"`
Desc string `json:"desc"`
StanderdDesc string `json:"standerdDesc"`
DescTitle string `json:"descTitle"`
TimeZone string `json:"timeZone"`
ActionCode string `json:"actionCode"`
Group Group `json:"group,omitempty"`
}
type Module struct {
MailNo string `json:"mailNo"`
OriginCountry string `json:"originCountry"`
DestCountry string `json:"destCountry"`
MailType string `json:"mailType"`
MailTypeDesc string `json:"mailTypeDesc"`
Status string `json:"status"`
StatusDesc string `json:"statusDesc"`
MailNoSource string `json:"mailNoSource"`
ProcessInfo ProcessInfo `json:"processInfo"`
GlobalEtaInfo GlobalEtaInfo `json:"globalEtaInfo"`
GlobalCombinedLogisticsTraceDTO GlobalCombinedLogisticsTraceDTO `json:"globalCombinedLogisticsTraceDTO"`
LatestTrace LatestTrace `json:"latestTrace"`
DetailList []DetailListItem `json:"detailList"`
DaysNumber string `json:"daysNumber"`
}
90 changes: 0 additions & 90 deletions providers/caniao/caniao_provider.go

This file was deleted.

48 changes: 0 additions & 48 deletions providers/caniao/json_schema.go

This file was deleted.

3 changes: 2 additions & 1 deletion providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/providers/cainiao"
"github.com/alufers/paczkobot/providers/deutsche_post"
"github.com/alufers/paczkobot/providers/dhl"
"github.com/alufers/paczkobot/providers/dpdcompl"
Expand All @@ -23,7 +24,7 @@ var AllProviders = []Provider{
&inpost.InpostProvider{},
&pocztapolska.PocztaPolskaProvider{},
&postnl.PostnlProvider{},
// &caniao.CaniaoProvider{},
&cainiao.CainiaoProvider{},
&dpdcompl.DpdComPlProvider{},
&ups.UPSProvider{},
&dhl.DHLProvider{},
Expand Down

0 comments on commit ae2b17b

Please sign in to comment.